Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FranciscusRenatus/663956907a4b88718c205c4acc5d1fdf to your computer and use it in GitHub Desktop.
Save FranciscusRenatus/663956907a4b88718c205c4acc5d1fdf to your computer and use it in GitHub Desktop.
# Building the optimal model using backward elimination
import statsmodels.formula.api as sm
#Appending the contant as first col of the dataset for readability.
X = np.append(arr = np.ones((50, 1)).astype(int), values = X, axis = 1)
#X_optimized initialized with all features.
X_opt = X[:, [0,1,2,3,4,5]]
#Fitting using OLS
regressor_OLS = sm.OLS(endog = y, exog= X_opt).fit()
#Summary for results, any feature who can be eliminated ?
regressor_OLS.summary()
#Eliminated feature 2, refitting..
X_opt = X[:, [0,1,3,4,5]]
regressor_OLS = sm.OLS(endog = y, exog= X_opt).fit()
regressor_OLS.summary()
#Eliminated feature 1, refitting..
X_opt = X[:, [0,3,4,5]]
regressor_OLS = sm.OLS(endog = y, exog= X_opt).fit()
regressor_OLS.summary()
#Eliminated feature 4, refitting..
X_opt = X[:, [0,3,5]]
regressor_OLS = sm.OLS(endog = y, exog= X_opt).fit()
regressor_OLS.summary()
#Eliminated feature 5, refitting..
X_opt = X[:, [0,3]]
regressor_OLS = sm.OLS(endog = y, exog= X_opt).fit()
regressor_OLS.summary()
#Finished, optimal model found.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment