Skip to content

Instantly share code, notes, and snippets.

@ychennay
Last active May 11, 2019 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ychennay/b1d102a2a0e520b6eccbfd8079d8be14 to your computer and use it in GitHub Desktop.
Save ychennay/b1d102a2a0e520b6eccbfd8079d8be14 to your computer and use it in GitHub Desktop.
import statsmodels.api as sm
import numpy as np
from sklearn.linear_model import LinearRegression
# get original NumPy implementation
β = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y).flatten()
# get sklearn's LinearRegression implementation weights
lr = LinearRegression()
sklearn_coefficients = lr.fit(X,y).coef_.flatten()
# get statsmodel implementation weights
result = sm.OLS(y, X).fit()
sm_coefficients = result.params.values
# zip up all the results into one consolidated dataframe to inspect
coefficients_df = pd.DataFrame(list(zip(features, sklearn_coefficients, sm_coefficients, β)),
columns=["feature", "sklearn", "statsmodel", "numpy"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment