def recursive_feature_elimination(self, nfeat=None, step=1, inplace=False):
"""A method to implement recursive feature elimination on the model.
Note that CV is not performed in this function. The method will
continue to eliminate some features (specified by step parameter)
at each step until the specified number of features are reached.
Parameters
__________
nfeat : int or None, default=None
The num of top features to select. If None, half of the features
are selected.
step : int or float, default=1
If int, then step corresponds to the number of features to remove
at each iteration.
If float and within (0.0, 1.0), then step corresponds to the
percentage (rounded down) of features to remove at each
iteration.
If float and greater than one, then integral part will be
considered as an integer input
inplace : bool, default=False
If True, the predictors of the class are modified to those
selected by the RFE procedure.
Returns
_______
selected : A series object containing the selected features as
index and their rank in selection as values
"""
rfe = RFE(self.alg, n_features_to_select=nfeat, step=step)
rfe.fit(
self.datablock.train[self.predictors],
self.datablock.train[self.datablock.target]
)
ranks = pd.Series(rfe.ranking_, index=self.predictors)
selected = ranks.loc[rfe.support_]
if inplace:
self.set_predictors(selected.index.tolist())
return selected
评论列表
文章目录