Last active
February 22, 2023 03:58
-
-
Save Eyongkevin/a709f9ebefa0e2b79b9f50ed62d9a512 to your computer and use it in GitHub Desktop.
Python implementation of linear regression base on derived formular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def standRegres(xArr, yArr): | |
""" | |
Find weight given the data and the predicted value | |
Parameters | |
---------- | |
xArr : List | |
Our data | |
yArr: List | |
our predicted value | |
Returns | |
------- | |
ws : float | |
regression weight | |
""" | |
# convert arrays to matrices | |
xMat = mat(xArr); yMat = mat(yArr).T | |
# compute xTx and check if determinate is zero | |
xTx = xMat.T*xMat | |
if linalg.det(xTx) == 0.0: | |
print("This matrix is singular, cannot do inverse") | |
return | |
ws = xTx.I * (xMat.T * yMat) | |
return ws |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment