Last active
May 11, 2019 21:42
-
-
Save ychennay/b1d102a2a0e520b6eccbfd8079d8be14 to your computer and use it in GitHub Desktop.
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
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