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

ML LAB Rec

Uploaded by

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

ML LAB Rec

Uploaded by

A0554
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1. Find General Hypothesis for following data (S Algorithm).

# S Alorithm (Convert Specific hypothesis to General hypothesis)


instances=[["morning","sunny","warm","yes","mild","strong"],
["evening","rainy","cold","no","mild","normal"],
["morning","sunny","moderate","yes","normal","normal"],
["evening","sunny","cold","yes","high","strong"]]
target=["yes","no","yes","yes"]
h=["null","null","null","null"] #more specific
for i in range(len(instances)):
if(target[i]=="yes"):
if(h[0]=="null"):
h=instances[i].copy()
else:
for j in range(len(instances[i])):
if(h[j]!=instances[i][j]):
h[j]="?"
print("General hypothesis is")
print (h)
OUTPUT:

2. Write program for candidate elimination in python.


instance=[['sunny','warm','normal','strong','warm','same'],
['sunny','warm','high','strong','warm','same'],
['rainy','cold','high','strong','warm','change'],
['sunny','warm','high','strong','cool','change']]
target=['yes','yes','no','yes']
sh=[len(instance)]
sh=['null']*len(instance[0]) #sh=['null', 'null', 'null', 'null', 'null', 'null'] gh=[['?','?','?','?','?','?'],
['?','?','?','?','?','?'],
['?','?','?','?','?','?'],['?','?','?','?','?','?'],
['?','?','?','?','?','?'],['?','?','?','?','?','?']]
i=0
while(i<len(instance)):
if(target[i]=='yes'):
if(sh[0]=='null'):
sh=instance[i].copy()
else
: for x in range(len(sh)):
if(sh[x]!=instance[i][x]):
sh[x]='?'
else:
for x in range(len(instance[0])):
if (instance[i][x]!=sh[x]):
gh[x][x]=sh[x]
else:
gh[x][x]='?'
i+=1
for x in range (len(instance[0])):
if(gh[x][x]!=sh[x]):
gh[x][x]='?'
print("Final Output:") print("\
nSpecific Hypothesis")
print(sh)
print("\nGeneral hypothesis")
for a in gh:
flag=0
for s in a:
if(s!='?'):
flag=1
break
if(flag==1):
print(a)
OUTPUT:

3. The probability that it is Friday and that a student is absent is 3 %. Since there are 5 school days in
a week, the probability that it is Friday is 20 %. What is theprobability that a student is absent given
that today is Friday? Apply Baye’s rule in python to get the result. (Ans: 15%)

paf=float(input("Probability that it is friday and student is absent = "))


pf=float(input("Probability that it is friday = "))
print("Probability that a student is absent given that today is Friday = ",paf/pf)
OUTPUT:

4. Extract the data from database using python.


(create a csv file, upload it in Jupyter notebook)
Import pandas as pd
k=pd.read_csv(“file1.csv”)
k.head()
#k.tail()
#k.ioc[1:2]
#k[“name”]
#k[[“year”,”units”]]
#k.iloc[0,2]
#k.iloc[0]
OUTPUT:

5. Implement k-nearest neighbours classification using python.

import math
def classify(points,p,k):
distance=[]
for g in points:
for xy in points[g]:
eucdis=math.sqrt((xy[0]-p[0])**2+(xy[1]-p[1])**2)
distance.append((eucdis,g))
distance=sorted(distance)[:k]
f1,f2=0,0
for d in distance:
if d[1]==0:
f1+=1
elif d[1]==1:
f2+=1
return 0 if f1>f2 else 1
points={1:[(1,1),(2,2),(3,1)] , 0:[(5,3),(4,4),(6,5)]}
p=(1,2)
k=3
ans=classify(points,p,k)
print("The value classified to unknown pointer is: {}".format(ans))
OUTPUT:

6. Given the following data, which specify classifications for nine combinations of VAR1 and VAR2
predict a classification for a case where VAR1=0.906 and VAR2=0.606, using the result of k-means
clustering with 3 means (i.e., 3 centroids)
VAR1 VAR2 CLASS
1.713 1.586 0
0.180 1.786 1
0.353 1.240 1
0.940 1.566 0
1.486 0.759 1
1.266 1.106 0
1.540 0.419 1
0.459 1.799 1
0.773 0.186 1

from sklearn.cluster import KMeans


import numpy as np
X = np.array([[1.713,1.586], [0.180,1.786], [0.353,1.240], [0.940,1.566], [1.486,0.759], [1.266,1.106],
[1.540,0.419], [0.459,1.799],[0.773,0.186]])
y=np.array([0,1,1,0,1,0,1,1,1])
kmeans = KMeans(n_clusters=3, random_state=0).fit(X,y)
print("The input data is ")
print("VAR1 \t VAR2 \t CLASS")
i=0
for val in X: print(val[0],"\
t",val[1],"\t",y[i]) i+=1
print("="*20)
# To get test data from the user
print("The Test data to predict ")
test_data = []
VAR1 = float(input("Enter Value for VAR1 :"))
VAR2 = float(input("Enter Value for VAR2 :"))
test_data.append(VAR1)
test_data.append(VAR2)
print("="*20)
print("The predicted Class is : ",kmeans.predict([test_data]))
OUTPUT:

7. Implement Naïve Bayes theorem to classify the English


text. import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

msglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Label'])


print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)

count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())
clf = MultinomialNB()
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data
prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest,
prediction)) print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is
Positive") else:
print("Statement is Negative")

OUTPUT:

8. Implement an algorithm to demonstrate the significance of genetic algorithm.

import random
def func(x,y,z):
return 6*x**3+9*y**2+90*z-25
def fitness(x,y,z):
ans=func(x,y,z)
if(ans==0):
return 9999
else:
return abs(1/ans)

solutions=[]
for s in range(1000):
solutions.append((random.uniform(0,1000),random.uniform(0,1000),random.uniform(0,1000)))

ranksol=[]
for s in solutions:
ranksol.append((fitness(s[0],s[1],s[2]),s))
ranksol.sort()
ranksol.reverse()

print("gen best solution")


print(ranksol[0])

bestsol=ranksol[:10]
elements=[]
for s in bestsol:
elements.append(s[1][0])
elements.append(s[1][1])
elements.append(s[1][2])

NewGen=[]
for _ in range(1000):
e1=random.choice(elements)*random.uniform(0.9,1.01)
e2=random.choice(elements)*random.uniform(0.99,1.01)
e3=random.choice(elements)*random.uniform(0.99,1.01)
NewGen.append((e1,e2,e3))
solutions=NewGen
print("Top 10 best Solutions")
for x in bestsol:
print(x)

OUTPUT:

9. The following training examples map descriptions of individuals onto high, medium
and low credit-worthiness.
medium skiing design single twenties no -> highRisk
high golf trading married forties yes -> lowRisk
low speedway transport married thirties yes -> medRisk
medium football banking single thirties yes -> lowRisk
high flying media married fifties yes -> highRisk
low football security single twenties no -> medRisk
medium golf media single thirties yes -> medRisk
medium golf transport married forties yes -> lowRisk
high skiing banking single thirties yes -> highRisk
low golf unemployed married forties yes -> highRisk
Input attributes are (from left to right) income, recreation, job, status, age-group, home-owner.
Find the unconditional probability of `golf' and the conditional probability of `single' given
`medRisk' in the dataset?

#Unconditional Probability of golf


tr=10
golf=4
print("Unconditional probability of Golf is=",golf/tr)
#Conditional Probability of Single given midrisk
samr=2
mr=3
psamr=samr/tr
pmr=mr/tr
print("Probability of midrisk with single=",psamr)
print("Probability of midrisk=",pmr)
print("Conditional probability of single given midrisk=",psamr/pmr)
OUTPUT:

10. Write a Python program to implement Perceptron Algorithm.

x_input=[0.1,0.5,0.2]
w_weights=[0.4,0.3,0.6]
threshold=0.5
def stop(weight_sum):
if(weight_sum>threshold):
return 1
else:
return 0
def perceptron():
weight_sum=0
for x,w in zip(x_input,w_weights):
weight_sum += x*w
return stop(weight_sum)
print("Output: ",perceptron())
OUTPUT:
Output: 0

11. Write a Python program to implement Decision tree.

import pandas as pd
from sklearn import tree
iris=pd.read_csv("data.csv")
dic={"sunny":1,"overc":2,"rain":3}
iris["outlook"]=iris["outlook"].map(dic)
dic2={"hot":1,"mild":2,"cool":3}
iris["temp"]=iris["temp"].map(dic2)
dic3={"high":1,"normal":2}
iris["humidity"]=iris["humidity"].map(dic3)
dic4={"weak":1,"strong":2}
iris["wind"]=iris["wind"].map(dic4)
dic5={"yes":1,"no":0}
iris["playtennis"]=iris["playtennis"].map(dic5)
x=iris.iloc[:,:-1]
y=iris.iloc[:,-1]
clf=tree.DecisionTreeClassifier().fit(x,y)
tree.plot_tree(clf)

OUTPUT:

You might also like