Stepwise Regression in Python Stepwise Regression in Python python python

Stepwise Regression in Python


Trevor Smith and I wrote a little forward selection function for linear regression with statsmodels: http://planspace.org/20150423-forward_selection_with_statsmodels/ You could easily modify it to minimize a p-value, or select based on beta p-values with just a little more work.


You may try mlxtend which got various selection methods.

from mlxtend.feature_selection import SequentialFeatureSelector as sfsclf = LinearRegression()# Build step forward feature selectionsfs1 = sfs(clf,k_features = 10,forward=True,floating=False, scoring='r2',cv=5)# Perform SFFSsfs1 = sfs1.fit(X_train, y_train)


You can make forward-backward selection based on statsmodels.api.OLS model, as shown in this answer.

However, this answer describes why you should not use stepwise selection for econometric models in the first place.