0% found this document useful (0 votes)
10 views6 pages

Machine Learning Lab

The document discusses two machine learning models - Linear Regression and K Nearest Neighbour. It includes code to build a linear regression model to predict output values and find the correlation between two lists. It also includes code for KNN classification to predict a job status based on age and salary inputs.

Uploaded by

TEJASWINI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Machine Learning Lab

The document discusses two machine learning models - Linear Regression and K Nearest Neighbour. It includes code to build a linear regression model to predict output values and find the correlation between two lists. It also includes code for KNN classification to predict a job status based on age and salary inputs.

Uploaded by

TEJASWINI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MACHINE LEARNING LAB

1. Linear Regression Model

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

age = [20 , 50, 40, 30, 60, 35, 45]


sal = [25000, 50000, 35000, 30000, 70000, 30000, 55000]
qjob = ['yes','No','No','Yes','Yes','No','Yes']

x1 = int(input("Enter the age: "))


y1 = int(input("Enter the salary: "))

print("\n\n")

distances = [math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) for x2, y2 in zip(age, sal)]

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))

You might also like