Skip to content

Instantly share code, notes, and snippets.

@xiaowei1234
Created March 4, 2022 17:02
Show Gist options
  • Save xiaowei1234/08dbd72695e6d263546c973a9a5a330b to your computer and use it in GitHub Desktop.
Save xiaowei1234/08dbd72695e6d263546c973a9a5a330b to your computer and use it in GitHub Desktop.
A comparison of GLM vs OLS lasso penalty regression
import numpy as np
from sklearn.linear_model import PoissonRegressor, Lasso
X_array = np.asarray([[1, 2], [1, 3], [1, 4], [1, 3]])
y = np.asarray([2, 2, 3, 2])
Preg_alpha_1 = PoissonRegressor(alpha=1., fit_intercept=False).fit(X_array, y)
print('alpha 1', Preg_alpha_1.coef_)
Preg_alpha_2 = PoissonRegressor(alpha=2., fit_intercept=False).fit(X_array/2., y)
print('alpha 2', Preg_alpha_2.coef_)
Lreg_alpha_1 = Lasso(alpha=1., fit_intercept=False).fit(X_array, y)
print('alpha 1 Lasso OLS', Lreg_alpha_1.coef_)
Lreg_alpha_2 = Lasso(alpha=2., fit_intercept=False).fit(X_array/2., y)
print('alpha 2 Lasso OLS', Lreg_alpha_2.coef_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment