Skip to content

Instantly share code, notes, and snippets.

@finlytics-hub
Last active October 24, 2020 14:10
Show Gist options
  • Save finlytics-hub/4d9ede3166e89eae70fee335c00ecbdc to your computer and use it in GitHub Desktop.
Save finlytics-hub/4d9ede3166e89eae70fee335c00ecbdc to your computer and use it in GitHub Desktop.
Linear Regression - Normal Equation
# 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