Note: I have already looked at Difference between RFE and SelectFromModel in Scikit-Learn post and my query is differnt from that post
Expectation:
SelectFromModel
and RFE
have similar/comparable performance in the model built using their recommendations.
Doubt: Is there any known use-case where RFE will fare better? As a student of data science (just starting learning) its a weird observation for me
Code:
# RecursiveFeatureElimination_ExtraTreesClassifier
from sklearn.feature_selection import RFE
from sklearn.ensemble import ExtraTreesClassifier
rfe_selector = RFE(estimator=ExtraTreesClassifier(), n_features_to_select=20, step=10)
rfe_selector.fit(x_raw, y_raw)
[x[0] for x in pandas.Series(rfe_selector.support_, index=x_raw.columns.values).items() if x[1]]
# returns
['loan_amnt','funded_amnt','funded_amnt_inv','term','int_rate','installment','grade','sub_grade','dti','initial_list_status','out_prncp','out_prncp_inv','total_pymnt','total_pymnt_inv','total_rec_prncp','total_rec_int','recoveries','collection_recovery_fee','last_pymnt_amnt','next_pymnt_d']
# SelectFromModel_ExtraTreesClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
selector = SelectFromModel(ExtraTreesClassifier(n_estimators=100), max_features=20)
selector.fit(x_raw, y_raw)
[x[0] for x in pandas.Series(selector.get_support(), index=x_raw.columns.values).items() if x[1]]
# prints
['loan_amnt','funded_amnt','funded_amnt_inv','term','installment','out_prncp','out_prncp_inv','total_pymnt','total_pymnt_inv','total_rec_prncp','total_rec_int','recoveries','collection_recovery_fee','last_pymnt_d','last_pymnt_amnt','next_pymnt_d']
Code for Model train and test
# internal code to select what variables I want
x_train, y_train, x_test, y_test = get_train_test(var_set_type=4)
model = ExtraTreesClassifier()
model.fit(x_train, y_train)
# then just print the confusion matrix
ExtraTreesClassifier Model from SelectFromModel variables
ExtraTreesClassifier Model from RFE variables
My confusion matrix is powered by this Open Source project: DTrimarchi10 / confusion_matrix
SelectFromModel
has ended up with only 16 features. What happens if you let RFE keep eliminating? $\endgroup$