0% found this document useful (0 votes)
45 views

ML Program Output

Uploaded by

Santhiya R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

ML Program Output

Uploaded by

Santhiya R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

EXNO:1

Implementation of FINDS algorithm

import csv

a = []

with open('enjoysport.csv', 'r') as csvfile:

for row in csv.reader(csvfile):

a.append(row)

print(a)

print("\n The total number of training instances are : ",len(a))

num_attribute = len(a[0])-1

print("\n The initial hypothesis is : ")

hypothesis = ['0']*num_attribute

print(hypothesis)

for i in range(0, len(a)):

if a[i][num_attribute] == 'yes':

for j in range(0, num_attribute):

if hypothesis[j] == '0' or hypothesis[j] == a[i][j]:

hypothesis[j] = a[i][j]

else:

hypothesis[j] = '?'

print("\n The hypothesis for the training instance {} is : \n" .format(i+1),hypothesis)

print("\n The Maximally specific hypothesis for the training instance is ")

print(hypothesis)

Output:
[['sky', 'airtemp', 'humidity', 'wind', 'water', 'forcast', 'enjoysport'], ['sunny', 'warm', 'normal', 'strong',
'warm', 'same', 'yes'], ['sunny', 'warm', 'high', 'strong', 'warm', 'same', 'yes'], ['rainy', 'cold', 'high',
'strong', 'warm', 'change', 'no'], ['sunny', 'warm', 'high', 'strong', 'cool', 'change', 'yes']]

The total number of training instances are : 5

The initial hypothesis is :

['0', '0', '0', '0', '0', '0']

The hypothesis for the training instance 1 is :

['0', '0', '0', '0', '0', '0']

The hypothesis for the training instance 2 is :

['sunny', 'warm', 'normal', 'strong', 'warm', 'same']

The hypothesis for the training instance 3 is :

['sunny', 'warm', '?', 'strong', 'warm', 'same']

The hypothesis for the training instance 4 is :

['sunny', 'warm', '?', 'strong', 'warm', 'same']

The hypothesis for the training instance 5 is :

['sunny', 'warm', '?', 'strong', '?', '?']

The Maximally specific hypothesis for the training instance is

['sunny', 'warm', '?', 'strong', '?', '?']

Implementation of FINDS Algorithm


EXNO:2

Building of Artificial Neural Network

import numpy as np

import pandas as pd

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

data = load_iris()

X=data.data

y=data.target

y = pd.get_dummies(y).values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=20, random_state=4)

learning_rate = 0.1

iterations = 10000

N = y_train.size

input_size = 4

hidden_size = 2

output_size = 3

results = pd.DataFrame(columns=["mse", "accuracy"])

np.random.seed(10)

W1 = np.random.normal(scale=0.5, size=(input_size, hidden_size))

W2 = np.random.normal(scale=0.5, size=(hidden_size , output_size))

def sigmoid(x):

return 1 / (1 + np.exp(-x))
def mean_squared_error(y_pred, y_true):

return ((y_pred - y_true)**2).sum() / 2*(y_pred.size)

def accuracy(y_pred, y_true):

acc = y_pred.argmax(axis=1) == y_true.argmax(axis=1)

return acc.mean()

for itr in range(iterations):

Z1 = np.dot(X_train, W1)

A1 = sigmoid(Z1)

Z2 = np.dot(A1, W2)

A2 = sigmoid(Z2)

mse = mean_squared_error(A2, y_train)

acc = accuracy(A2, y_train)

results=results.append({"mse":mse, "accuracy":acc},ignore_index=True )

E1 = A2 - y_train

dW1 = E1 * A2 * (1 - A2)

E2 = np.dot(dW1, W2.T)

dW2 = E2 * A1 * (1 - A1)

W2_update = np.dot(A1.T, dW1) / N

W1_update = np.dot(X_train.T, dW2) / N

W2 = W2 - learning_rate * W2_update

W1 = W1 - learning_rate * W1_update

results.mse.plot(title="Mean Squared Error")

results.accuracy.plot(title="Accuracy")

Z1 = np.dot(X_test, W1)

A3 = sigmoid(Z1)
Z2 = np.dot(A3, W2)

A4 = sigmoid(Z2)

acc = accuracy(A4, y_test)

print("Accuracy: {}".format(acc))

Output:

Accuracy: 0.95
EXNO:3 Implementation of Naive Bayesian Classifier

import pandas as pd

msg=pd.read_csv('naivetext.csv',names=['message','label'])

print('The dimensions of the dataset',msg.shape)

msg['labelnum']=msg.label.map({'pos':1,'neg':0})

X=msg.message

y=msg.labelnum

print(X)

print(y)

from sklearn.model_selection import train_test_split

Xtrain,Xtest,ytrain,ytest=train_test_split(X,y)

print ('\n the total number of Training Data :',ytrain.shape)

print ('\n the total number of Test Data :',ytest.shape)

from sklearn.feature_extraction.text import CountVectorizer

count_vect = CountVectorizer()

Xtrain_dtm = count_vect.fit_transform(Xtrain)

Xtest_dtm=count_vect.transform(Xtest)

print('\n The words or Tokens in the text documents \n')

print(count_vect.get_feature_names())

df=pd.DataFrame(Xtrain_dtm.toarray(),columns=count_vect.get_feature_names())

from sklearn.naive_bayes import MultinomialNB

clf = MultinomialNB().fit(Xtrain_dtm,ytrain)

predicted = clf.predict(Xtest_dtm)

from sklearn import metrics


print('\n Accuracy of the classifier is',metrics.accuracy_score(ytest,predicted))

print('\n Confusion matrix')

print(metrics.confusion_matrix(ytest,predicted))

print('\n The value of Precision', metrics.precision_score(ytest,predicted))

print('\n The value of Recall', metrics.recall_score(ytest,predicted))

Output:

The dimensions of the dataset (18, 2)

0 I love this sandwich

1 This is an amazing place

2 I feel very good about these beers

3 This is my best work

4 What an awesome view

5 I do not like this restaurant

6 I am tired of this stuff

7 I can't deal with this

8 He is my sworn enemy

9 My boss is horrible

10 This is an awesome view

11 I do not like taste of this juice

12 I love to dance

13 I am sick and tired of this place

14 What a great holiday

15 This is a bad locality to stay

16 We will have good fun tomorrow


17 I went to my enemy's house today

Name: message, dtype: object

0 1

1 1

2 1

3 1

4 1

5 0

6 0

7 0

8 0

9 0

10 1

11 0

12 1

13 0

14 1

15 0

16 1

17 0

Name: labelnum, dtype: int64

the total number of Training Data : (13,)

the total number of Test Data : (5,)

The words or Tokens in the text documents

['about', 'am', 'amazing', 'an', 'and', 'awesome', 'bad', 'beers', 'best', 'boss', 'dance', 'do', 'enemy', 'feel',
'fun', 'good', 'great', 'have', 'holiday', 'horrible', 'house', 'is', 'like', 'locality', 'love', 'my', 'not', 'of', 'place',
'restaurant', 'sick', 'stay', 'stuff', 'these', 'this', 'tired', 'to', 'today', 'tomorrow', 'very', 'view', 'we', 'went',
'what', 'will', 'work']

Accuracy of the classifier is 1.0

Confusion matrix

[[3 0]

[0 2]]

The value of Precision 1.0

The value of Recall 1.0

Implementation of Naive Bayesian Classifier


EXNO:4 SVM Classifier

from sklearn import datasets


from sklearn.model_selection import train_test_split
iris = datasets.load_iris()
X = iris.data
y = iris.target
from sklearn.svm import SVC
model = SVC(kernel='linear', C=1E10)
means = []
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
Output :
0.98
With wine dataset

from sklearn import datasets

from sklearn.model_selection import train_test_split

iris = datasets.load_iris()

import pandas as pd

dataset=pd.read_csv('wine.csv')

X=dataset.iloc [:,2:14].values

y=dataset.iloc [:,0].values

from sklearn.svm import SVC

model = SVC(kernel='linear', C=1E10)

means = []

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.33, random_state=42)

model.fit(X_train, y_train)

print(model.score(X_test, y_test))

Output

0.9830508474576272
EXNO:5 K Nearest Neighbour Classifier

from matplotlib import pyplot as plt


import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
data = load_iris()
features = data.data
feature_names = data.feature_names
feature_names = data.feature_names
target = data.target
target_names = data.target_names
labels = target_names[target]
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=1)
kf = KFold(n_splits=5, shuffle=True)
means = []
X=features
y=target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42)
classifier.fit(X_train, y_train)
print(classifier.score(X_test, y_test))

Output :
0.98

With wine dataset

from matplotlib import pyplot as plt

import numpy as np

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.model_selection import KFold

data = load_iris()
features = data.data

feature_names = data.feature_names

feature_names = data.feature_names

target = data.target

target_names = data.target_names

labels = target_names[target]

from sklearn.neighbors import KNeighborsClassifier

classifier = KNeighborsClassifier(n_neighbors=1)

kf = KFold(n_splits=5, shuffle=True)

means = []

import pandas as pd

dataset=pd.read_csv('wine.csv')

X=dataset.iloc [:,2:14].values

y=dataset.iloc [:,0].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

classifier.fit(X_train, y_train)

print(classifier.score(X_test, y_test))

Output

0.7627118644067796

With cancer dataset


import pandas as pd
dataset = pd.read_csv('lung cancer.csv')
x = dataset.iloc[:,2:6].values
y = dataset.iloc[:,6].values
from sklearn.modal_selection import train_test.split
from sklearn.model_selection import KFold
from sklearn.neighbors import KNeighbors classifier
classifier = KNeighbors classifier (n.neighbours = 1)
Kf = KFold(n_splits = 5,shuffle = True)
means = []
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.33,random_state=0.2)
classifier fit(x_train,y_train)
print(classifier score(x_test,y_test))

OUTPUT:

0.9424242424242424
EXNO:6 K Means Segmentation
import cv2

import numpy as np

import matplotlib.pyplot as plt

image = cv2.imread(“image.jpg”)

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

pixel_values = image.reshape((-1, 3))

pixel_values = np.float32(pixel_values)

print(pixel_values.shape)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)

k=3

_, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

centers = np.uint8(centers)

labels = labels.flatten()

segmented_image = centers[labels.flatten()]

segmented_image = segmented_image.reshape(image.shape)

plt.imshow(image)

plt.show()

plt.imshow(segmented_image)

plt.show()

np.set_printoptions(threshold=np.inf)

print(labels)

Output:

(139657,3)
EXNO:7 Linear Regression

from IPython import get_ipython

get_ipython().magic('reset -sf')

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

dataset = pd.read_csv('Position_Salaries.csv')

X = dataset.iloc[:, 1:2].values

y = dataset.iloc[:, 2].values

from sklearn.tree import DecisionTreeRegressor

regressor = DecisionTreeRegressor(random_state=0)

regressor.fit(X,y)

n=np.array([6.5]).reshape(1, 1)

y_pred = regressor.predict(n)

plt.scatter(X, y, color = 'red')

plt.plot(X, regressor.predict(X), color = 'blue')

plt.title('Regression Model')

plt.xlabel('Position level')

plt.ylabel('Salary')

plt.show()

X_grid = np.arange(min(X), max(X), 0.01)

X_grid = X_grid.reshape((len(X_grid), 1))

plt.scatter(X, y, color = 'red')

plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')


plt.title('Example of Decision Regression Model')

plt.xlabel('Position level')

plt.ylabel('Salary')

plt.show()

print(y_pred)

Output :

[150000.]

Implementation of Linear Regression

Linear Regression
EXNO:8 Implementation of Dimensionality Reduction Algorithm (PCA)

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

dataset = pd.read_csv(‘wine.csv’)

X = dataset.iloc[:, 1:13].values

y = dataset.iloc[:, 0].values

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()

X_train = sc.fit_transform(X_train)

X_test = sc.transform(X_test)

from sklearn.decomposition import PCA

from sklearn.linear_model import LogisticRegression

pca = PCA(n_components = 2)

X1_train = pca.fit_transform(X_train)

X1_test = pca.transform(X_test)

print(X.shape)

print(X1_train.shape)

variance = pca.explained_variance_ratio_

classifier = LogisticRegression(random_state = 0)

classifier.fit(X1_train, y_train)

y_pred = classifier.predict(X1_test)
print(classifier.score(X1_test, y_test))

classifier.fit(X_train, y_train)

y1_pred = classifier.predict(X_test)

print(classifier.score(X_test, y_test))

print(np.shape(X_train))

print(np.shape(X1_train))

plt.figure(figsize=(8,6))

plt.scatter(X1_train[:,0],X1_train[:,1],s=10,c=y_train,cmap=’rainbow’)

plt.xlabel(‘First principal component’)

plt.ylabel(‘Second Principal Component’)

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test, y_pred)

Output :

(178, 12)

(142, 2)

0.9444444444444444

0.9444444444444444

(142, 12)

(142, 2)
EXNO:9 Implementation of Random Forest Classifier

from matplotlib import pyplot as plt

import numpy as np

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.model_selection import KFold

data = load_iris()

features = data.data

feature_names = data.feature_names

feature_names = data.feature_names

target = data.target

target_names = data.target_names

labels =target_names[target]

from sklearn.ensemble import RandomForestClassifier

classifier = RandomForestClassifier(n_estimators=100)

kf = Kfold(n_splits=5, shuffle=True)

X=features

y=target

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.33, random_state=42)

classifier.fit(X_train, y_train)

print(classifier.score(X_test, y_test))

Output :
0.98

EXNO:10 Implementation of Deep Learning Algorithm

Program for digit classification :

import tensorflow
import numpy as np
from matplotlib import pyplot as plt
from keras.datasets import mnist
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()
from keras import models
from keras import layers
network=models.Sequential()
network.add(layers.Dense(512,activation='relu',input_shape=(28*28,)))
network.add(layers.Dense(10,activation='softmax'))
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
train_images=train_images.reshape((60000,28*28))
train_images=train_images.astype('float32')/255
test_images=test_images.reshape((10000,28*28))
test_images=test_images.astype('float32')/255
from tensorflow.keras.utils import to_categorical
train_labels=to_categorical(train_labels)
test_labels=to_categorical(test_labels)
network.fit(train_images,train_labels,
validation_data=(test_images,test_labels),epochs=5,batch_size=128)
(train_images,train_labels),(test_images,test_images)=mnist.load_data()
x=range(1,10)
for n in x:
plt.subplot(9,1,n)
plt.imshow(train_images[n],cmap=plt.get_cmap('gray'))
plt.show()

Output :

Epoch 1/5

Accuracy=0.9276 val_accuracy=0.9633

Epoch 2/5
Accuracy=0.9696 val_accuracy=0.9738

Epoch 3/5

Accuracy=0.9747 val_accuracy=0.9772

Epoch 4/5

Accuracy=0.9849 val_accuracy=0.9767

Epoch 5/5

Accuracy=0.9885 val_accuracy=0.9793

You might also like