Machine Learning Lab
Machine Learning Lab
import numpy as np
from matplotlib import pyplot as plt
l1 = np.array([2,1, 3, 4, 5])
l2=[3,4,7,2,1]
plt.plot(l1)
plt.plot(l2)
plt.title("Linear Regression")
plt.xlabel("l1")
plt.ylabel("l2")
plt.show()
# printing original lists
print ("Original list 1 : " + str(l1))
print ("Original list 2 : " + str(l2))
n=len(l1)
for i in l1:
s1=sum(l1)
for j in l2:
s2=sum(l2)
# using naive method to
# Multiplying two lists
s3 = []
for i in range(0, len(l1)):
s3.append(l1[i] * l2[i])
s4=sum(s3)
# printing resultant list
print ("sum of x : " + str(s1))
print ("sum of y : " + str(s2))
print ("sum of xy : " + str(s4))
s5 = []
for i in range(0, len(l1)):
s5.append(l1[i] * l1[i])
s6=sum(s5)
# printing resultant list
print ("sum of x square : " + str(s6))
e=n*(s4)-(s1)*(s2)
f=n*(s6)-(s1)*(s1)
b=e/f
a=(s2-(b*s1))/n
print ("value of b : " + str(b))
print ("value of a : " + str(a))
string=input("enter the no.:")
print(string)
no=int(string)
y=a+b*(no)
print("value of y:",str(y))
plt.plot(l1)
plt.plot(l2)
plt.title("Linear Regression")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
# for finding y cap
conv = int(b)
y1 = b * l1 + a
print('y cap values',str(y1))
fault = sum(y1)
print('Fault:', str(fault))
#finding y-y cap
s7 = []
for z in range(0, len(l2)):
s7= [l2z - y1z for l2z, y1z in zip(l2, y1)]
cap=sum(s7)
print("summation of fault:",str(cap))\
2. K Nearest Neighbour Model
import math
print("\n\n")
print("Distances:", distances)
print("\n\n")
for i in range(len(distances)):
print(f"The Distance for age={age[i]}, sal={sal[i]} is: {distances[i]}")
print("\n\n")
a=min(distances)
print("The least distance is : ",a)
b=distances.index(a)
print("The user entered age is:",str(x1))
print("The user entered salary is:",str(y1))
c=qjob[b]
print("The predicted value of quit job is:",str(c))