Created
April 1, 2022 14:56
-
-
Save jeremy9959/a7624af4479dfcbef7190c5a6bc77857 to your computer and use it in GitHub Desktop.
ridge regression
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
from sklearn.linear_model import Ridge | |
# generate data | |
x=np.random.random(n_points) | |
y=np.sin(2*np.pi*x)+np.random.normal(0,0.1,n_points) | |
# make a ridge object with alpha=whatever you want | |
clf = Ridge(alpha=0) | |
# fit the ridge object to the data | |
clf.fit(x.reshape(-1,1),y) | |
print("Slope={}, intercept={}".format(clf.coef_,clf.intercept_)) | |
# use the fitted result to calculate predicted y values | |
clf.predict(x.reshape(-1,1)) | |
# make a plot of the predicted values and the original data | |
g=figure() | |
g.line(x=x,y=clf.predict(x.reshape(-1,1))) | |
g.scatter(x=x,y=y,color='red') | |
show(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment