Machine Learning Lab
Machine Learning Lab
Lab
Manual
1. 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%)
Ans:
def bayes_theorem(p_f, p_a, p_f_given_a):
p_a_given_f=(p_f_given_a*p_a)/p_f
return p_a_given_f
p_a=1
p_f=0.2
p_f_given_a=0.03
result=bayes_theorem(p_f, p_a, p_f_given_a)
print("The result is:",(result*100))
Ans:15
distance = []
for group in points:
for feature in points[group]:
freq1 = 0
freq2 = 0
for d in distance:
if d[1] == 0:
freq1 += 1
elif d[1] == 1:
freq2 += 1
def main():
points = {
0: [(1, 12), (2, 5), (3, 6), (3, 10), (3.5, 8), (2, 11), (2, 9), (1, 7)],
1: [(5, 3), (3, 2), (1.5, 9), (7, 2), (6, 1), (3.8, 1), (5.6, 4), (4, 2), (2, 5)]}
p = (1, 12)
k=2
print("The value classified to unknown point is: {} ".format(classifyAPoint(points, p, k)))
if _name_ == '_main_':
main()
4.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 kmeans clustering with 3 means (i.e., 3 centroids)
Output:
array([0])
import numpy as np
import pandas as pd
df = pd.read_csv(r'E:\test\e5.csv')
df.head()
len(df)
print(len(df))
print(df)
#P(A|B) = P(A ∩ B) / P(B)
data=pd.crosstab(df.status,df.risk,normalize=True,margins=True)
print(data)
a=data.medrisk.single
print(a)
b=(np.sum((data.medrisk.single)+(data.medrisk.married)))
print("The conditional Prob is")
print(a/b)
data1=pd.crosstab(df.recreation,df.risk,normalize=True,margins=True)
print(data1)
x=data1.medrisk.golf
y=(np.sum((data1.highrisk.golf)+(data1.medrisk.golf)+(data1.lowrisk.golf)))
print(y)
print("The unconditional Prob is")
print(x/y)
Output:
Unconditional probability of golf: = 0.4
Conditional probability of single given medRisk: = 0.6666666666666667
6. Implement linear regression using python.
OUTPUT:
The Tfidf features of Dataset:
Train test split:
the total number of training data (12,)
the total number of test data (6,)
the accuracy of classifier is 0.6666666666666666
8. Implement an algorithm to demonstrate the significance of genetic algorithm
# genetic algorithm search of the one max optimization problem from
numpy.random import randint
from numpy.random import rand
# objective function
def onemax(x):
return -sum(x)
# tournament selection
def selection(pop, scores, k=3):
# first random selection selection_ix =
randint(len(pop)) for ix in randint(0,
len(pop), k-1):
# check if better (e.g. perform a tournament) if
scores[ix] < scores[selection_ix]:
selection_ix = ix
return pop[selection_ix]
# mutation operator
def mutation(bitstring, r_mut):
for i in range(len(bitstring)):
# check for a mutation if rand() <
r_mut:
# flip the bit
bitstring[i] = 1 - bitstring[i]
genetic algorithm
def genetic_algorithm(objective, n_bits, n_iter, n_pop, r_cross, r_mut):
# initial population of random bitstring
pop = [randint(0, 2, n_bits).tolist() for _ in range(n_pop)]
# keep track of best solution
best, best_eval = 0, objective(pop[0])
# enumerate generations for gen in
range(n_iter):
# evaluate all candidates in the population scores =
[objective(c) for c in pop]
# check for new best solution
for i in range(n_pop):
if scores[i] < best_eval:
best, best_eval = pop[i], scores[i]
print(">%d, new best f(%s) = %.3f" % (gen, pop[i], scores[i]))
# select parents
selected = [selection(pop, scores) for _ in range(n_pop)]
# create the next generation children
= list()
for i in range(0, n_pop, 2):
# get selected parents in pairs p1, p2 =
selected[i], selected[i+1]
# crossover and mutation
for c in crossover(p1, p2, r_cross):
# mutation mutation(c,
r_mut)
# store for next generation
children.append(c)
# replace population
pop = children
return [best, best_eval]
import numpy as np
X = X/np.amax(X,axis=0)
y = y/100
def derivatives_sigmoid(x):
return x * (1 - x)
epoch=7000
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)
EO = y-output
outgrad = derivatives_sigmoid(output)
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
d_hiddenlayer = EH * hiddengrad
OUTPUT:
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
[0.86]
[0.89]]
Predicted Output:
[[0.82425907]
[0.81420906]
[0.82360573]]