AIML Lab Manual
AIML Lab Manual
* Visit https://vtuconnect.in for more info. For any queries or questions wrt our
platform contact us at: support@vtuconnect.in
Download & Share VTU Connect App Now From Google Play Store
Program-1
Implement A* Search Algorithm
Algorithm:
1. Place the starting node list in “OPEN” list.
2. Check if the OPEN list is empty or not
If list is empty then,
return failure and stop
3. Select the node from “OPEN” set which has the smallest value of evaluation f.
If node n is goal node then,
Return success and stop
4. Expand node n and generate all of its successors and put n to the CLOSED list
For each successor ‘n’ check whether its already there in OPEN and CLOSED list.
If not compute f value and place into OPEN
Else if node n is already there in OPEN and CLOSE then it should be attached to back
pointer which should reflect lowest g(n) value.
5. Return Step 2.
Program:
#!/usr/bin/env python
#coding utf-8
#ln[1]:
def aStarAlgo(start_node,stop_node):
open_set=set(start_node)
closed_set=set()
g={}
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
parents={}
g[start_node]=0
parents[start_node]=start_node
while len(open_set)>0:
n=None
for v in open_set:
if n==None or g[v]+heuristic(v)<g[n]+heuristic(n):
n=v
if n==stop_node or Graph_nodes[n]==None:
pass
else:
for(m,weight) in get_neighbors(n):
if m not in open_set and m not in closed_set:
open_set.add(m)
parents[m]=n
g[m]=g[n]+weight
else:
if g[m]>g[n]+weight:
g[m]=g[n]+weight
parents[m]=n
if m in closed_set:
closed_set.remove(m)
open_set.add(m)
if n==None:
print("path does not exist")
return None
if n==stop_node:
path=[]
while parents[n]!=n:
path.append(n)
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
n=parents[n]
path.append(start_node)
path.reverse()
print('path found:{}'.format(path))
return path
open_set.remove(n)
closed_set.add(n)
print("path doesnot exist")
return None
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
def heuristic(n):
H_dist={
'S':7,
'A':6,
'B':2,
'C':1,
'D':0
}
return H_dist[n]
Graph_nodes={
'S':[('A',1),('B',4)],
'A':[('B',2),('C',5),('D',12)],
'B':[('C',2)],
'C':[('D',3)],
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
}
aStarAlgo('S','D')
Output:
path found:['S', 'A', 'B', 'C', 'D']
['S', 'A', 'B', 'C', 'D']
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 2
Implement AO* Algorithm
Algorithm:
1. Let G be a graph with only starting node INIT.
2. Repeat the following until INIT is labeled as SOLVED or h(INIT)>FUTILITY.
Select an unexpanded node from the most promising path from INIT (call it NODE).
Generate SUCCESSORS of NODE.
If ther are none,
Set h(NODE)=FUTILITY(i.e NODE is unsolvable)
Otherwise,
For each SUCCESSORS that is not an ancestor of NODE
Do the following:
1. Add SUCCESSOR toG.
2. If SUCCESSOR is a terminal node,
label it SOLVED and set h(SUCCESSOR)=0
3. If SUCCESSOR is not a terminal node,
Compute its h value
Propagate the newly discovered information up the graph by doing the following:
1. Let S be set of SOLVED node or nodes whose h values have been changed
and need to have values propagated back to their parents.
2. Initialize S to node, until S is empty.
3. Repeat the following:
Remove a node from S and call it CURRENT.
Program:
class Graph:
def __init__(self, graph, heuristicNodeList, startNode):
self.graph = graph
self.H=heuristicNodeList
self.start=startNode
self.parent={}
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
self.status={}
self.solutionGraph={}
def printSolution(self):
print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START
NODE:",self.start)
print("------------------------------------------------------------")
print(self.solutionGraph)
print("------------------------------------------------------------")
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
minimumCost=0
costToChildNodeListDict={}
costToChildNodeListDict[minimumCost]=[]
flag=True
for nodeInfoTupleList in self.getNeighbors(v): # iterate over all the set of child node/s
cost=0
nodeList=[]
for c, weight in nodeInfoTupleList:
cost=cost+self.getHeuristicNodeValue(c)+weight
nodeList.append(c)
def aoStar(self, v, backTracking): # AO* algorithm for a start node and backTracking
status flag
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
if solved==True: # if the Minimum Cost nodes of v are solved, set the current
node status as solved(-1)
self.setStatus(v,-1)
self.solutionGraph[v]=childNodeList # update the solution graph with the solved
nodes which may be a part of solution
if v!=self.start: # check the current node is the start node for backtracking the
current node value
self.aoStar(self.parent[v], True) # backtracking the current node value with
backtracking status set to true
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1, 'T': 3}
graph1 = {
'A': [[('B', 1), ('C', 1)], [('D', 1)]],
'B': [[('G', 1)], [('H', 1)]],
'C': [[('J', 1)]],
'D': [[('E', 1), ('F', 1)]],
'G': [[('I', 1)]]
}
G1= Graph(graph1, h1, 'A')
G1.applyAOStar()
G1.printSolution()
Output:
HEURISTIC VALUES : {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1, 'T':
3}
SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1, '
T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : B
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1, '
T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1, '
T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : G
-----------------------------------------------------------------------------------------
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J': 1, '
T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : B
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 10, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J': 1, '
T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J': 1, '
T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : I
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 0, 'J': 1, '
T': 3}
SOLUTION GRAPH : {'I': []}
PROCESSING NODE : G
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1, '
T': 3}
SOLUTION GRAPH : {'I': [], 'G': ['I']}
PROCESSING NODE : B
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 12, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1, '
T': 3}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1, 'T':
3}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : C
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1, 'T':
3}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1, 'T':
3}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : J
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 0, 'T':
3}
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': []}
PROCESSING NODE : C
-----------------------------------------------------------------------------------------
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 1, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 0, 'T':
3}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J']}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A
------------------------------------------------------------
{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A': ['B', 'C']}
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 3
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.
Algorithm:
File:Enjoysport.csv
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
Program:
import numpy as np
import pandas as pd
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
data=pd.read_csv("enjoysport.csv")
concepts=np.array(data.iloc[:,0:-1])
print("\nInstances are;\n",concepts)
target=np.array(data.iloc[:,-1])
print("\nTarget value are:",target)
def learn(concepts,target):
specific_h=concepts[0].copy()
print("\n Initialization of specific_h and general_h")
print("\n Specific Boundary:",specific_h)
general_h=[["?" for i in range(len(specific_h))] for i in range(len(specific_h))]
print("\n Generic Boundary:",general_h)
s_final,g_final=learn(concepts,target)
print("Final Specific_h:",s_final,sep="\n")
print("Final General_h:",g_final,sep="\n")
Output:
Instances are;
[['sunny' 'warm' 'normal' 'strong' 'warm' 'same']
['sunny' 'warm' 'high' 'strong' 'warm' 'same']
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Generic Boundary: [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?
', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Instance is Positive
Specific Boundary after 1 Instance is ['sunny' 'warm' 'normal' 'strong' 'warm' 'same']
Generic Boundary after 1 Instance is [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Instance is Positive
Specific Boundary after 2 Instance is ['sunny' 'warm' '?' 'strong' 'warm' 'same']
Generic Boundary after 2 Instance is [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Instance is Positive
Specific Boundary after 4 Instance is ['sunny' 'warm' '?' 'strong' '?' '?']
Generic Boundary after 4 Instance is [['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?', '?', '?'], ['?',
'?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Final Specific_h:
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 4
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.
Algorithm:
Otherwise Begin,
A ← the attribute from Attributes that best classifies Examples
The decision attribute for Root ← A
For each possible value, vi, of A,
Add a new tree branch below Root, corresponding to the test A = vi
Let Examples vi, be the subset of Examples that have value vi for A
If Examples vi , is empty
Then below this new branch add a leaf node with
label = most common value of Target_attribute in Examples
else
below this new branch add the subtree
ID3(Examples vi,Targe_tattribute,Attribute – {A}))
5.End
6.Return Root
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Files:
id3_test_1.csv
outlook temparture humidity wind
rain cool normal strong
sunny mild normal strong
id.csv
Outlook Temperature Humidity Wind Answer
Program:
import math
import csv
def load_csv(filename):
lines=csv.reader(open(filename,'r'));
dataset=list(lines)
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
headers=dataset.pop(0)
return dataset,headers
class Node:
def __init__(self,attribute):
self.attribute=attribute
self.children=[]
self.answer=""
def subtables(data,col,delete):
dic={}
coldata=[row[col] for row in data]
attr=list(set(coldata))
for k in attr:
dic[k]=[]
for y in range(len(data)):
key=data[y][col]
if delete:
del data[y][col]
dic[key].append(data[y])
return attr,dic
def entropy(S):
attr=list(set(S))
if len(attr)==1:
return 0
counts=[0,0]
for i in range(2):
counts[i]=sum([1 for x in S if attr[i]==x])/(len(S)*1.0)
sums=0
for cnt in counts:
sums+=-1*cnt*math.log(cnt,2)
return sums
def compute_gain(data,col):
attValues,dic=subtables(data,col,delete=False)
total_entropy=entropy([row[-1] for row in data])
for x in range(len(attValues)):
ratio=len(dic[attValues[x]])/(len(data)*1.0)
entro=entropy([row[-1] for row in dic[attValues[x]]])
total_entropy-=ratio*entro
return total_entropy
def build_tree(data,features):
lastcol=[row[-1] for row in data]
if(len(set(lastcol)))==1:
node=Node("")
node.answer=lastcol[0]
return node
n=len(data[0])-1
gains=[compute_gain(data,col) for col in range(n)]
split=gains.index(max(gains))
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
node=Node(features[split])
fea=features[:split]+features[split+1:]
attr,dic=subtables(data,split,delete=True)
for x in range(len(attr)):
child=build_tree(dic[attr[x]],fea)
node.children.append((attr[x],child))
return node
def print_tree(node,level):
if node.answer!="":
print(" "*level,node.answer)
return
print(" "*level,node.answer)
for value,n in node.children:
print(""*(level+1),value)
print_tree(n,level+2)
def classify(node,x_test,features):
if node.answer!="":
print(node.answer)
return
pos=features.index(node.attribute)
for value,n in node.children:
if x_test[pos]==value:
classify(n,x_test,features)
dataset,features=load_csv("id3.csv")
node = build_tree(dataset, features) # Build decision tree
print ("The decision tree for the dataset using ID3 algorithm is ")
print_tree(node, 0)
testdata, features =load_csv("id3_test_1.csv")
for xtest in testdata:
print("The test instance xtest")
print("The predicted labe1 ", end="" )
classify(node, xtest,features)
Output:
overcast
yes
sunny
normal
yes
high
no
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
rain
weak
yes
strong
no
The test instance xtest
The predicted labe1 no
The test instance xtest
The predicted labe1 yes
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 5
Build a Artificial Neural Network by implementing the Backpropagation algorithm and test
the same using appropriate data sets.
Algorithm:
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program:
import numpy as np
X=np.array(([2,9],[1,5],[3,6]),dtype=float)
y=np.array(([92],[86],[89]),dtype=float)
x=x/np.amax(X,axis=0)
y=y/100
def sigmoid(x):
return 1/(1+np.exp(-x))
def sigmoid_grad(x):
return x*(1-x)
epoch=1000
eta=0.2
input_neurons=2
hidden_neurons=3
output_neurons=1
wh=np.random.uniform(size=(input_neurons,hidden_neurons))
bh=np.random.uniform(size=(1,hidden_neurons))
wout=np.random.uniform(size=(hidden_neurons,output_neurons))
bout=np.random.uniform(size=(1,output_neurons))
for i in range(epoch):
h_ip=np.dot(X,wh)+bh
h_act=sigmoid(h_ip)
o_ip=np.dot(h_act,wout)+bout
output=sigmoid(o_ip)
Eo=y-output
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
outgrad=sigmoid_grad(output)
d_output=Eo*outgrad
Eh=d_output.dot(wout.T)
hiddengrad=sigmoid_grad(h_act)
d_hidden=Eh*hiddengrad
wout+=h_act.T.dot(d_output)*eta
why=X.T.dot(d_hidden)*eta
print("Normalized input:\n"+str(X))
print("Actual output:\n"+str(y))
print("Predicted Output:\n",output)
Output:
Normalized input:
[[2. 9.]
[1. 5.]
[3. 6.]]
Actual output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.89224518]
[0.88542877]
[0.89224316]]
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 6
Write a program to implement the navier Bayesian classifier for a sample training data set
stored as a .csv file. Compute the accuracy of the classifier,considering few test data sets.
Explanation:
Where,
P(h|D) is the probability of hypothesis h given the data D. This is called the posterior
probability.
P(D|h) is the probability of data d given that the hypothesis h was true.
P(h) is the probability of hypothesis h being true. This is called the prior probability of h.
P(D) is the probability of the data. This is called the prior probability of D
After calculating the posterior probability for a number of different hypotheses h, and is
interested in finding the most probable hypothesis h ∈ H given the observed data D. Any
such maximally probable hypothesis is called a maximum a posteriori (MAP) hypothesis.
Bayes theorem to calculate the posterior probability of each candidate hypothesis is hMAP is
a MAP hypothesis provided.
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
This means that in addition to the probabilities for each class, we must also store the mean
and standard deviations for each input variable for each class.
Files:
naviedata.csv:
1 85 66 29 0 26.6 0.351 31 0
1 89 66 23 94 28.1 0.167 21 0
… … … … … … … … …
Program:
import csv
import random
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
import math
def loadcsv(filename):
lines = csv.reader(open(filename, "r"));
dataset = list(lines)
for i in range(len(dataset)):
#converting strings into numbers for processing
dataset[i] = [float(x) for x in dataset[i]]
return dataset
def separatebyclass(dataset):
separated = {} #dictionary of classes 1 and 0
#creates a dictionary of classes 1 and 0 where the values are
#the instances belonging to each class
for i in range(len(dataset)):
vector = dataset[i]
if (vector[-1] not in separated):
separated[vector[-1]] = []
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
separated[vector[-1]].append(vector)
return separated
def mean(numbers):
return sum(numbers)/float(len(numbers))
def stdev(numbers):
avg = mean(numbers)
variance = sum([pow(x-avg,2) for x in numbers])/float(len(numbers)-1)
return math.sqrt(variance)
def summarizebyclass(dataset):
separated = separatebyclass(dataset);
#print(separated)
summaries = {}
for classvalue, instances in separated.items():
#for key,value in dic.items()
#summaries is a dic of tuples(mean,std) for each class value
summaries[classvalue] = summarize(instances) #summarize is used to cal to
mean and std
return summaries
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
if testset[i][-1] == predictions[i]:
correct += 1
return (correct/float(len(testset))) * 100.0
def main():
filename = 'naivedata.csv'
splitratio = 0.67
dataset = loadcsv(filename);
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 7
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
Program:
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
import sklearn.metrics as sm
import pandas as pd
import numpy as np
iris=datasets.load_iris()
X=pd.DataFrame(iris.data)
X.columns=['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y=pd.DataFrame(iris.target)
y.columns=['Targets']
model=KMeans(n_clusters=3)
model.fit(X)
plt.figure(figsize=(14,7))
colormap=np.array(['red','lime','black'])
plt.subplot(1,3,1)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y.Targets],s=40)
plt.title('Real Clasification')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
plt.subplot(1,3,2)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[model.labels_],s=40)
plt.title('K Mean Classification')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
print('The accuracy score of K-Mean:',sm.accuracy_score(y,model.labels_))
print('The Confusion matrix of K-Mean:',sm.confusion_matrix(y,model.labels_))
y_gmm=gmm.predict(xs)
plt.subplot(1,3,3)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_gmm],s=40)
plt.title('GMM Classification')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 8
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.
Algorithm:
Program:
from sklearn.datasets import load_iris
iris=load_iris()
print("Features Names:",iris.feature_names,"Iris Data:",iris.data,"Target
Names:",iris.target_names,"Target:",iris.target)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(iris.data,iris.target,test_size=.25)
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
clf.fit(X_train,y_train)
print("Accuracy=",clf.score(X_test,y_test))
print("Predicted Data")
print(clf.predict(X_test))
prediction=clf.predict(X_test)
print("Test Data:")
print(y_test)
diff=prediction-y_test
print("Result is")
print(diff)
print("Total no of samples misclassified=",sum(abs(diff)))
Output:
Features Names: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Iris Data: [[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
[5.4 3.7 1.5 0.2]
[4.8 3.4 1.6 0.2]
[4.8 3. 1.4 0.1]
[4.3 3. 1.1 0.1]
[5.8 4. 1.2 0.2]
[5.7 4.4 1.5 0.4]
[5.4 3.9 1.3 0.4]
[5.1 3.5 1.4 0.3]
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Program 9
Implement the non-parametric Locally Weighted Regression Algorithm in order to fit data
points. Select appropriate data set for your experiment and draw graphs.
Algorithm:
Locally Weighted Regression Algorithm:
1. Read the Given data Sample to X and the curve (linear or non linear) to Y
5. Prediction = x0*β
CSV Files:
tips.csv
total_bill tip sex smoker day time size
… … … … … … …
Program:
import matplotlib.pyplot as plt
import pandas as pd
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
import numpy as np
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
one = np.mat(np.ones(m))
X = np.hstack((one.T,mbill.T))
#set k here
ypred = localWeightRegression(X,mtip,0.5)
SortIndex = X[:,1].argsort(0)
xsort = X[SortIndex][:,0]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(bill,tip, color='green')
ax.plot(xsort[:,1],ypred[SortIndex], color = 'red', linewidth=5)
plt.xlabel('Total bill')
plt.ylabel('Tip')
plt.show();
Output:
Download & Share VTU Connect App Now From Google Play Store