Skip to content

Instantly share code, notes, and snippets.

@jeremy9959
Created April 1, 2022 14:56
Show Gist options
  • Save jeremy9959/a7624af4479dfcbef7190c5a6bc77857 to your computer and use it in GitHub Desktop.
Save jeremy9959/a7624af4479dfcbef7190c5a6bc77857 to your computer and use it in GitHub Desktop.
ridge regression
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