coefficientswhich can also be used to capture feature importances by considering the absolute values as we saw in linear models with penalty learn sparse coefficientswhich only use small subset of features this can be viewed as form of feature selection for the model itselfbut can also be used as preprocessing step to select features for another model in contrast to univariate selectionmodel-based selection considers all features at onceand so can capture interactions (if the model can capture themto use model-based feature selectionwe need to use the selectfrommodel transformerin[ ]from sklearn feature_selection import selectfrommodel from sklearn ensemble import randomforestclassifier select selectfrommodelrandomforestclassifier(n_estimators= random_state= )threshold="median"the selectfrommodel class selects all features that have an importance measure of the feature (as provided by the supervised modelgreater than the provided threshold to get comparable result to what we got with univariate feature selectionwe used the median as thresholdso that half of the features will be selected we use random forest classifier with trees to compute the feature importances this is quite complex model and much more powerful than using univariate tests now let' actually fit the modelin[ ]select fit(x_trainy_trainx_train_l select transform(x_trainprint("x_train shape{}format(x_train shape)print("x_train_l shape{}format(x_train_l shape)out[ ]x_train shape( x_train_l shape( againwe can have look at the features that were selected (figure - )in[ ]mask select get_support(visualize the mask -black is truewhite is false plt matshow(mask reshape( - )cmap='gray_r'plt xlabel("sample index"figure - features selected by selectfrommodel using the randomforestclassifier automatic feature selection