ML Lab
ML Lab
x = 0
y = 0
m = 4
n = 3
print("Initial State = (0,0)")
print("Capacities = (4,3)")
print("Goal State = (2,y)")
while(x != 2):
r = int(input("Enter the rule: "))
if(r == 1):
x = m
elif(r == 2):
y = n
elif(r == 3):
x = 0
elif(r == 4):
y = 0
elif(r == 5):
t = n - y
y = n
x -= t
elif(r == 6):
t = m - x
x = m
y -= t
elif(r == 7):
y += x
x = 0
elif(r == 8):
x += y
y = 0
else:
print("Invalid Rule")
break
print(x, y)
# 3)Implement and demonstrate the FIND-S algorithm for finding the most
specific hypothesis based on a given set of training data samples. Read the
training data from a .CSV file.
import csv
num_attribute = 6
a = []
with open('enjoysport.csv', 'r') as file:
reader = csv.reader(file)
a = list(reader)
hypothesis = a[0][:-1]
for i in a:
if i[-1] == 'yes':
for j in range(num_attribute):
if i[j] != hypothesis[j]:
hypothesis[j] = '?'
print(hypothesis)
print("\nThe Maximally Specific Hypothesis for a given Training Examples :\n")
print(hypothesis)
# 4) For a given set of training data examples stored in a .CSV file, implement
and demonstrate the Candidate-Elimination algorithm to output a description of
the set of all hypotheses consistent with the training examples.
import csv
with open("trainingdata.csv") as f:
csv_file = csv.reader(f)
data = list(csv_file)
s = data[1][:-1]
print(s)
g = [['?' for i in range(len(s))] for j in range(len(s))]
print(g)
for i in data:
if i[-1] == "yes":
for j in range(len(s)):
if i[j] != s[j]:
s[j] = '?'
g[j][j] = '?'
def derivatives_sigmoid(x):
return x * (1 - x)
epoch = 5
lr = 0.1
inputlayer_neurons = 2
hiddenlayer_neurons = 3
output_neurons = 1
wh = np.random.uniform(size=(inputlayer_neurons, hiddenlayer_neurons))
bh = np.random.uniform(size=(1, hiddenlayer_neurons))
wout = np.random.uniform(size=(hiddenlayer_neurons, output_neurons))
bout = np.random.uniform(size=(1, output_neurons))
for i in range(epoch):
hinp1 = np.dot(X, wh)
hinp = hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1 = np.dot(hlayer_act, wout)
outinp = outinp1+bout
output = sigmoid(outinp)
EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO * outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
d_hiddenlayer = EH * hiddengrad
wout += hlayer_act.T.dot(d_output) * lr
wh += X.T.dot(d_hiddenlayer) * lr
x = msg.message
y = msg.labelnum
count_vect = CountVectorizer()
xtrain_dtm = count_vect.fit_transform(xtrain)
xtest_dtm = count_vect.transform(xtest)
clf = MultinomialNB()
clf.fit(xtrain_dtm, ytrain)
predicted = clf.predict(xtest_dtm)
##########VIVA##############
# The (train_test_split) function is for splitting a single dataset for two
different purposes: training and testing. The training subset is for building
your model. The testing subset is for using the model on unknown data to
evaluate the performance of the model.
# CountVectorizer is a great tool provided by the scikit-learn library in
Python. It is used to transform a given text into a vector on the basis of the
frequency (count) of each word that occurs in the entire text.
data = pd.read_csv('heart1.csv')
infer = VariableElimination(model)
##################VIVA##########################
# .fit() basically is used to train model. Later this model can be used to make
predictions
#using .predict()
# 10) Apply EM algorithm to cluster a set of data stored in a .CSV file. Use
the same data set for clustering using k-Means algorithm. Compare the results
of these two algorithms and comment on the quality of clustering. You can add
Java/Python ML library classes/API in the program.
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.mixture import GaussianMixture
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dataset = load_iris()
# print(dataset)
X = pd.DataFrame(dataset.data)
X.columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']
y = pd.DataFrame(dataset.target)
y.columns = ['Targets']
# print(X)
plt.figure(figsize=(14, 7))
colormap = np.array(['red', 'lime', 'black'])
# REAL PLOT
plt.subplot(1, 3, 1)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets])
plt.title('Real')
# K-PLOT
plt.subplot(1, 3, 2)
model = KMeans(n_clusters=3)
model.fit(X)
predY = np.choose(model.labels_, [0, 1, 2])
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[predY])
plt.title('KMeans')
# GMM PLOT
scaler = StandardScaler()
scaler.fit(X)
xsa = scaler.transform(X)
xs = pd.DataFrame(xsa, columns=X.columns)
gmm = GaussianMixture(n_components=3)
gmm.fit(xs)
y_cluster_gmm = gmm.predict(xs)
plt.subplot(1, 3, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_cluster_gmm])
plt.title('GMM Classification')
plt.show()
iris = datasets.load_iris()
x = iris.data
y = iris.target
classifier = KNeighborsClassifier()
classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test)
print('Confusion Matrix')
print(confusion_matrix(y_test, y_pred))
print('Accuracy Metrics')
print(classification_report(y_test, y_pred))