Skip to content

Instantly share code, notes, and snippets.

@Eyongkevin
Last active February 22, 2023 03:58
Show Gist options
  • Save Eyongkevin/a709f9ebefa0e2b79b9f50ed62d9a512 to your computer and use it in GitHub Desktop.
Save Eyongkevin/a709f9ebefa0e2b79b9f50ed62d9a512 to your computer and use it in GitHub Desktop.
Python implementation of linear regression base on derived formular
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