Skip to content

Instantly share code, notes, and snippets.

@bharddwaj
Created November 10, 2019 00:02
Show Gist options
  • Save bharddwaj/260f20ed0db05ccc220e8cdd4664bc46 to your computer and use it in GitHub Desktop.
Save bharddwaj/260f20ed0db05ccc220e8cdd4664bc46 to your computer and use it in GitHub Desktop.
Linear Regression from scratch using Linear Algebra concept
def LinearRegression(x,y,round_digits):
'''
x: feature vector (Preferably Numpy Array)
y: dependent vector (Preferably Numpy Array)
round_digits: number of digits you want to round the final equation to
This function returns the Ordinary Least Squares Regression model.
'''
c = np.ones((len(x[0]),))
A = []
A.append(c)
for col_vec in x:
A.append(col_vec)
A = np.asmatrix(A)
# print(A[0])
b = np.asarray(y)
#messed up some dimensions
A_t = A
A = A.transpose()
# print(A.shape)
ab = np.matmul(A_t,b)
aa = (np.matmul(A_t,A))
inverse_aa = np.linalg.inv(aa)
hat = np.matmul(inverse_aa,ab.transpose())
hat = np.asarray(hat)
final_result = f"y = {round(hat[0][0],round_digits)}"
index = 1
for i in hat[1:]:
final_result += f" +{round(i[0],round_digits)}x{index}"
index+=1
return final_result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment