Skip to content

Commit

Permalink
implementation of lin reg model algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSomen committed Jul 5, 2018
1 parent 92b8ed7 commit 09b4a0d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Kaggle/LinearRegression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#%%
''' this is a implementation of the Linear regression model
from scratch without using any library '''
import numpy as np
X = 2* np.random.rand(100,1)
X

#%%
#the dependent equation i.e Y's value depends on Y
Y = 4 + 3 * X + np.random.randn(100, 1)
Y
#%%
#creates 100 X 1 shape matrix with value 1
np.ones((100,1))
#%%
X_b = np.c_[np.ones((100, 1)), X]
# add x0 = 1 to each instance
X_b

#%%
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(Y)
theta_best

#%%
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new] # add x0 = 1 to each instance
y_predict = X_new_b.dot(theta_best)
y_predict

#%%
import matplotlib.pyplot as plt
plt.plot(X_new, y_predict, "r-")
plt.plot(X, Y, "g.")
plt.axis([0, 2, 0, 15])
plt.show()

#%%
#The equivalent implementation of the above Algorithm
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, Y)
lin_reg.intercept_, lin_reg.coef_
lin_reg.predict(X_new)

0 comments on commit 09b4a0d

Please sign in to comment.