Skip to content

Instantly share code, notes, and snippets.

@Ayushijain09
Created July 15, 2020 14:08
Show Gist options
  • Save Ayushijain09/09ddb8c31f22415915ec09819a914c16 to your computer and use it in GitHub Desktop.
Save Ayushijain09/09ddb8c31f22415915ec09819a914c16 to your computer and use it in GitHub Desktop.
Sequential Forward Selection
from mlxtend.feature_selection import SequentialFeatureSelector
sfs = SequentialFeatureSelector(LinearRegression(), # cv = k-fold cross validation, floating is another extension of SFS, not used here
k_features=10,
forward=True,
floating=False,
scoring='accuracy',
cv=2)
sfs = sfs.fit(X_train, y_train)
selected_features = x_train.columns[list(sfs.k_feature_idx_)]
print(selected_features)
# print the selected features.
selected_features = x_train.columns[list(sfs.k_feature_idx_)]
print(selected_features)
# final prediction score.
print(sfs.k_score_)
# transform to the newly selected features.
x_train_new = sfs.transform(X_train)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment