3final ML Lab Manual
3final ML Lab Manual
1. Write a LISP program to solve the water-jug problem using heuristic function.
j1=0
j2=0
x=4
y=3
print("initial state=(0,0)")
print("final state=(2,0)")
print("capabilities=(4,3)")
while(j1!=2):
r=int(input("enter rule:"))
if(r==1):
j1=x
elif(r==2):
j2=y
elif(r==3):
j1=0
elif(r==4):
j2=0
elif(r==5):
t=y-j2
j1-=t
j2=y
if(j1<0):
j1=0
elif(r==6):
t=x-j1
j1=x
j2-=t
if(j2<0):
j2=0
elif(r==7):
j2+=j1
j1=0
if(j2<y):
j2=y
elif(r==8):
j1+=j2
j2=0
if(j1>x):
j1=x
print (j1,j2)
1
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Output
initial state=(0,0)
final state=(2,0)
capabilities=(4,3)
enter rule:2
03
enter rule:8
30
enter rule:2
33
enter rule:6
42
enter rule:3
02
enter rule:8
20
2
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
2. Write a program to use of BEST-FIRST SEARCH applied to the eight puzzle problem.
class Solution:
def solve(self, board):
dict = {}
flatten = []
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
dict[flatten] = 0
if flatten == (0, 1, 2, 3, 4, 5, 6, 7, 8):
return 0
return self.get_paths(dict)
def get_paths(self, dict):
cnt = 0
while True:
current_nodes = [x for x in dict if dict[x] == cnt]
if len(current_nodes) == 0:
return -1
for node in current_nodes:
next_moves = self.find_next(node)
for move in next_moves:
if move not in dict:
dict[move] = cnt + 1
if move == (0, 1, 2, 3, 4, 5, 6, 7, 8):
return cnt + 1
cnt += 1
def find_next(self, node):
moves = {
0: [1, 3],
1: [0, 2, 4],
2: [1, 5],
3: [0, 4, 6],
4: [1, 3, 5, 7],
5: [2, 4, 8],
6: [3, 7],
3
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
7: [4, 6, 8],
8: [5, 7],
}
results = []
pos_0 = node.index(0)
for move in moves[pos_0]:
new_node = list(node)
new_node[move], new_node[pos_0] = new_node[pos_0], new_node[move]
results.append(tuple(new_node))
return results
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]
]
print(ob.solve(matrix))
Input
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0] ]
Output 4
4
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
5
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 99,
'D': 1,
'E': 7,
'G': 0,
return H_dist[n
Graph_nodes = {
'A': [('B', 2), ('E', 3)],
'B': [('C', 1),('G', 9)],
'C': None,
'E': [('D', 6)],
'D': [('G', 1)],
}
aStarAlgo('A', 'G')
Output:
6
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
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
a=[]
with open("sports.csv","r") as csvfile:
fdata=csv.reader(csvfile)
for x in fdata:
a.append(x)
print(x)
num_att=len(a[0])-1
s=['0']*num_att
g=['?']*num_att
print(s)
print(g)
temp=[]
for i in range(0,num_att):
s[i]=a[1][i]
print(s)
print('_______')
for i in range(1,len(a)):
if a[i][num_att]=='yes':
for j in range(0,num_att):
if s[j]!=a[i][j]:
s[j]='?'
for j in range(0,num_att):
for k in range(0,len(temp)):
if temp[k][j]!=s[j] and temp[k][j]!='?':
del temp[k]
if a[i][num_att]=='no':
for j in range(0,num_att):
if a[i][j]!=s[j] and s[j]!='?':
g[j]=s[j]
temp.append(g)
g=['?']*num_att
print(s)
7
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
if len(temp)== 0:
print(g)
else:
print(temp)
print('_______')
output
8
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
5. 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.
import numpy as np
import pandas as pd
In[ 2 ]: data=pd.read_csv("sample.csv")
In[ 3 ]: df1=pd.DataFrame(data)
In[ 4 ]: print(df1)
Out[ ]:
In[ 5 ]: f1 = df1['Distance_Feature'].values
In[ 6 ]: f2 = df1['Speeding_Feature'].values
In[ 7 ]: X=np.matrix(list(zip(f1,f2)))
9
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
In[ 8 ]: plt.scatter(f1,f2)
Out[ ]:
<matplotlib.collections.PathCollection at 0x10ab32c8>
plt.xlim([0, 100])
plt.ylim([0, 50])
plt.show()
Out[ ]:
10
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
6. Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to classify a new
sample.
In[ 1 ]: from sklearn.datasets import load_iris
In[ 2 ]: from sklearn import tree
In[ 3 ]: iris = load_iris()
In[ 4 ]: X, y = iris.data, iris.target
In[ 5 ]: clf = tree.DecisionTreeClassifier()
In[ 6 ]: clf = clf.fit(X, y)
In[ 7 ]: tree.plot_tree(clf)
Out[ ]:
11
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
7. Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set. Print
both correct and wrong predictions. Java/Python ML library classes can be used for this problem.
In[ 2 ]: iris=datasets.load_iris()
In[ 3 ]: print(iris.feature_names)
Out[ ]:['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
print("Label", i , "-",str(iris.target_names[i]))
Out[ ]:KNeighborsClassifier(n_neighbors=2)
In[ 8 ]: y_pred=classifier.predict(x_test)
for r in range(0,len(x_test)):
str(y_pred[r]))
12
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
print('Confusion Matrix')
print(confusion_matrix(y_test,y_pred))
print('Accuracy Metrics')
print(classification_report(y_test,y_pred))
[[5 0 0]
[0 4 0]
[0 1 5]]
Accuracy Metrics
precision recall f1-score support
accuracy 0.93 15
macro avg 0.93 0.94 0.93 15
weighted avg 0.95 0.93 0.93 15
13
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
In[ 5 ]: print(wine.feature_names)
In[ 6 ]: print(wine.target_names)
In[ 7 ]: x=pd.DataFrame(wine['data'])
print(x.head())
Out[ ]: 0 1 2 3 4 5 6 7 8 9
10 11 \
0 14.23 1.71 2.43 15.6 127.0 2.80 3.06 0.28 2.29
5.64 1.04 3.92
1 13.20 1.78 2.14 11.2 100.0 2.65 2.76 0.26 1.28
4.38 1.05 3.40
2 13.16 2.36 2.67 18.6 101.0 2.80 3.24 0.30 2.81
5.68 1.03 3.17
3 14.37 1.95 2.50 16.8 113.0 3.85 3.49 0.24 2.18
7.80 0.86 3.45
4 13.24 2.59 2.87 21.0 118.0 2.80 2.69 0.39 1.82
4.32 1.04 2.93
12
0 1065.0
1 1050.0
2 1185.0
3 1480.0
4 735.0
In[ 8 ]: y=print(wine.target)
Out[ ]: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
14
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
Out[ ]:GaussianNB()
In[ 14 ]: y_pred=gnb.predict(x_test)
In[ 15 ]: from sklearn.metrics import classification_report, confusion_matrix
In[ 16 ]: print("Classification Accuracy :" , gnb.score(x_test,y_test))
print('Confusion Matrix')
print(confusion_matrix(y_test,y_pred))
print('Accuracy Metrics')
print(classification_report(y_test,y_pred))
accuracy 0.96 45
macro avg 0.94 0.96 0.95 45
weighted avg 0.96 0.96 0.96 45
15
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Output:
16
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
In[ 5 ]: X_normalized
In[ 7 ]: normalize
Out[ ]:Normalizer()
In[ 8 ]: normalizer.transform(X)
17