Last active
October 24, 2020 14:10
-
-
Save finlytics-hub/4d9ede3166e89eae70fee335c00ecbdc to your computer and use it in GitHub Desktop.
Linear Regression - Normal Equation
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
# generate some random numbers (independent variable) | |
X = 5 * np.random.rand(500,1) | |
# calculate linearly related (plus some noise) target variable in the form y = 10 + 2x + noise | |
y = 10 + 2 * X + np.random.randn(500,1) | |
# add ones to X for each observation (X0) | |
X_2d = np.c_[np.ones((500, 1)), X] | |
# calculate theta that minimizes MSE through Normal Equation | |
theta_best = np.linalg.inv(X_2d.T.dot(X_2d)).dot(X_2d.T).dot(y) | |
theta_best |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment