Pages

Linear Regression : Python

# importing modeules
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn import linear_model
import matplotlib.pyplot as plt

# assigning the data values with labels
x = [1,3,4,6,8,9,11,14]
y = [1,2,4,4,5,7,8,9]


# plotting scattering plot to show how data points will look 
plt.scatter(x,y)
plt.xlabel("x values")
plt.ylabel("y values")
plt.show()




 
 
# fitting linear regression to the data points
x = np.array([1,3,4,6,8,9,11,14]).reshape(-1,1)
y = np.array([1,2,4,4,5,7,8,9])
regr = linear_model.LinearRegression()
i = regr.fit(x,y)

# finding regression coefficients
j = regr.coef_
print("Regression Coefficient : ",j) # Regression Coefficient : [0.63636364] # intercept of the line k = regr.intercept_ print("Intercept : ",k) # Intercept : [0.545454545454545] # now plotting the line obtained y = j*x + k plt.plot(x,y) plt.xlabel("x values") plt.ylabel("y values") plt.show()










# showing how best fit is the line
x = [1,3,4,6,8,9,11,14]
y = [1,2,4,4,5,7,8,9]
reg_line = [((j*i) + k) for i in x]
plt.scatter(x,y,color='#003F72')
plt.plot(x,reg_line,color='r')
plt.xlabel("x values")
plt.ylabel("y values")
plt.legend("Li")
plt.show()

#where "i" is for true data points and "L" is for linear line in the above plot.


Conclusion: So it is clear from the last plot that all data points are not on the line, this shows that we can fit a high degree polynomial to the data to get the best fit.



No comments:

Post a Comment

If you have any doubt, let me know

Email Subscription

Enter your email address:

Delivered by FeedBurner

INSTAGRAM FEED