Created
April 10, 2021 17:56
-
-
Save righthandabacus/ede5de159c712144db38573a3d82e6e1 to your computer and use it in GitHub Desktop.
Example of scipy.optimize on least square linear regression
This file contains hidden or 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
import numpy as np | |
import scipy as sp | |
import scipy.optimize as opt | |
# Generate linear regression | |
a = 3 | |
b = 2 | |
n = 1000 | |
np.random.seed(42) | |
X = np.random.normal(loc=0, scale=4, size=(n,1)) | |
y = a + b*X + np.random.randn(n,1) | |
# regression function | |
def regression(X, y): | |
# define the loss function | |
n, d = X.shape | |
X = np.hstack([np.ones((n,1)), X]) | |
def loss(params): | |
residual = y - (X @ params.reshape(-1,1)) | |
loss = np.square(residual).mean() | |
gradient = (-2 * residual * X).mean(axis=0) | |
return loss, gradient | |
# find initial params | |
params0 = np.random.randn(d+1) | |
return opt.minimize(loss, params0, jac=True, method="BFGS") | |
# Run | |
reg = regression(X, y) | |
print(reg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment