Linear Regression 2
Linear Regression 2
ipynb - Colaboratory
Choose Files No file chosen Upload widget is only available when the cell has been
executed in the current browser session. Please rerun this cell to enable.
Saving Salary_Data.csv to Salary_Data.csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv('Salary_Data.csv')
print(data.shape)
(30, 2)
X= [ 1.1 1.3 1.5 2. 2.2 2.9 3. 3.2 3.2 3.7 3.9 4. 4. 4.1
4.5 4.9 5.1 5.3 5.9 6. 6.8 7.1 7.9 8.2 8.7 9. 9.5 9.6
10.3 10.5]
Y= [ 39343 46205 37731 43525 39891 56642 60150 54445 64445 57189
63218 55794 56957 57081 61111 67938 66029 83088 81363 93940
91738 98273 101302 113812 109431 105582 116969 112635 122391 121872]
x_mean=np.mean(X)
y_mean=np.mean(Y)
print('x_mean=',x_mean)
print('y_mean=',y_mean)
x_mean= 5.3133333333333335
y_mean= 76003.0
N=len(X)
n=0
d=0
for i in range(N):
n+=(X[i]-x_mean)*(Y[i]-y_mean)
d+=(X[i]-x_mean)**2
m=n/d
print('slope,m=',m)
c=(y_mean-m*(x_mean))
print('intercept,c=',c)
slope,m= 9449.962321455077
intercept,c= 25792.20019866869
min_X=np.min(X)-100
max_X=np.max(X)+100
x=np.linspace(min_X,max_X,100)
y=m*x+c
plt.plot(x,y,color='red')
https://colab.research.google.com/drive/1nXFYBPJtYmX5UWKkucIUeYGVQAkX27ay?authuser=0#scrollTo=B_Rdgx-4uzVF&printMode=true 1/3
11/11/23, 11:12 AM Untitled0.ipynb - Colaboratory
plt.scatter(X,Y,color='blue')
plt.xlabel('YEARS EXPERIENCE')
plt.ylabel('SALARY')
plt.legend()
plt.show()
WARNING:matplotlib.legend:No artists with labels found to put in legend. Note that artists whos
for i in range(N):
a=0
b=0
a+=(y[i]-Y[i])**2
b+=(Y[i]-y_mean)**2
r=1-(a/b)
print('r:',r)
output r: -95.68627472396004
https://colab.research.google.com/drive/1nXFYBPJtYmX5UWKkucIUeYGVQAkX27ay?authuser=0#scrollTo=B_Rdgx-4uzVF&printMode=true 2/3
11/11/23, 11:12 AM Untitled0.ipynb - Colaboratory
https://colab.research.google.com/drive/1nXFYBPJtYmX5UWKkucIUeYGVQAkX27ay?authuser=0#scrollTo=B_Rdgx-4uzVF&printMode=true 3/3