mvpa2.featsel.rfe.RFE¶
-
class
mvpa2.featsel.rfe.
RFE
(fmeasure, pmeasure, splitter, fselector=FractionTailSelector() fraction=0.050000, update_sensitivity=True, nfeatures_min=0, **kwargs)¶ Recursive feature elimination.
A
FeaturewiseMeasure
is used to compute sensitivity maps given a certain dataset. These sensitivity maps are in turn used to discard unimportant features. For each feature selection the transfer error on some testdatset is computed. This procedure is repeated until a givenStoppingCriterion
is reached.Notes
Available conditional attributes:
calling_time+
: Time (in seconds) it took to call the nodeerrors+
: History of errorshistory+
: Last step # when each feature was still presentnfeatures+
: History of # of features leftraw_results
: Computed results before invoking postproc. Stored only if postproc is not None.sensitivities
: History of sensitivities (might consume too much memorytrained_dataset
: The dataset it has been trained ontrained_nsamples+
: Number of samples it has been trained ontrained_targets+
: Set of unique targets (or any other space) it has been trained on (if present in the dataset trained on)training_time+
: Time (in seconds) it took to train the learner
(Conditional attributes enabled by default suffixed with
+
)References
- Such strategy after
- Guyon, I., Weston, J., Barnhill, S., & Vapnik, V. (2002). Gene selection for cancer classification using support vector machines. Mach. Learn., 46(1-3), 389–422.
- was applied to SVM-based analysis of fMRI data in
- Hanson, S. J. & Halchenko, Y. O. (2008). Brain reading using full brain support vector machines for object recognition: there is no “face identification area”. Neural Computation, 20, 486–503.
Examples
There are multiple possible ways to design an RFE. Here is one example which would rely on a SplitClassifier to extract sensitivities and provide estimate of performance (error)
>>> # Lazy import >>> from mvpa2.suite import * >>> rfesvm_split = SplitClassifier(LinearCSVMC(), OddEvenPartitioner()) >>> # design an RFE feature selection to be used with a classifier >>> rfe = RFE(rfesvm_split.get_sensitivity_analyzer( ... # take sensitivities per each split, L2 norm, mean, abs them ... postproc=ChainMapper([ FxMapper('features', l2_normed), ... FxMapper('samples', np.mean), ... FxMapper('samples', np.abs)])), ... # use the error stored in the confusion matrix of split classifier ... ConfusionBasedError(rfesvm_split, confusion_state='stats'), ... # we just extract error from confusion, so no need to split dataset ... Repeater(2), ... # select 50% of the best on each step ... fselector=FractionTailSelector( ... 0.50, ... mode='select', tail='upper'), ... # and stop whenever error didn't improve for up to 10 steps ... stopping_criterion=NBackHistoryStopCrit(BestDetector(), 10), ... # we just extract it from existing confusion ... train_pmeasure=False, ... # but we do want to update sensitivities on each step ... update_sensitivity=True) >>> clf = FeatureSelectionClassifier( ... LinearCSVMC(), ... # on features selected via RFE ... rfe, ... # custom description ... descr='LinSVM+RFE(splits_avg)' )
Note: If you rely on cross-validation for the StoppingCriterion, make sure that you have at least 3 chunks so that SplitClassifier could have at least 2 chunks to split. Otherwise it can not split more (one chunk could not be splitted).
Attributes
auto_train
Whether the Learner performs automatic trainingwhen called untrained. bestdetector
descr
Description of the object if any fmeasure
force_train
Whether the Learner enforces training upon every call. fselector
is_trained
Whether the Learner is currently trained. nfeatures_min
pass_attr
Which attributes of the dataset or self.ca to pass into result dataset upon call pmeasure
postproc
Node to perform post-processing of results slicearg
space
Processing space name of this node splitter
stopping_criterion
train_pmeasure
update_sensitivity
Methods
__call__
(ds)forward
(data)Map data from input to output space. forward1
(data)Wrapper method to map single samples. generate
(ds)Yield processing results. get_postproc
()Returns the post-processing node or None. get_space
()Query the processing space name of this node. reset
()reverse
(data)Reverse-map data from output back into input space. reverse1
(data)set_postproc
(node)Assigns a post-processing node set_space
(name)Set the processing space name of this node. train
(ds)The default implementation calls _pretrain()
,_train()
, and finally_posttrain()
.untrain
()Reverts changes in the state of this node caused by previous training Initialize recursive feature elimination
Parameters: fmeasure : FeaturewiseMeasure
pmeasure : Measure
used to compute the transfer error of a classifier based on a certain feature set on the test dataset. NOTE: If sensitivity analyzer is based on the same classifier as transfer_error is using, make sure you initialize transfer_error with train=False, otherwise it would train classifier twice without any necessity.
splitter: Splitter
This splitter instance has to generate at least two dataset splits when called with the input dataset. The first split serves as the training dataset and the second as the evaluation dataset.
fselector : Functor
Given a sensitivity map it has to return the ids of those features that should be kept.
update_sensitivity : bool
If False the sensitivity map is only computed once and reused for each iteration. Otherwise the sensitivities are recomputed at each selection step.
nfeatures_min : int
Number of features for RFE to stop if reached.
enable_ca : None or list of str
Names of the conditional attributes which should be enabled in addition to the default ones
disable_ca : None or list of str
Names of the conditional attributes which should be disabled
bestdetector : Functor
Given a list of error values it has to return a boolean that signals whether the latest error value is the total minimum.
stopping_criterion : Functor
Given a list of error values it has to return whether the criterion is fulfilled.
train_pmeasure : bool
Flag whether the
pmeasure
should be trained before computing the error. In general this is required, but if thefmeasure
andpmeasure
share and make use of the same classifier ANDpmeasure
does not really need training, it can be switched off to save CPU cycles.filler : optional
Value to fill empty entries upon reverse operation
auto_train : bool
Flag whether the learner will automatically train itself on the input dataset when called untrained.
force_train : bool
Flag whether the learner will enforce training on the input dataset upon every call.
space : str, optional
Name of the ‘processing space’. The actual meaning of this argument heavily depends on the sub-class implementation. In general, this is a trigger that tells the node to compute and store information about the input data that is “interesting” in the context of the corresponding processing in the output dataset.
pass_attr : str, list of str|tuple, optional
Additional attributes to pass on to an output dataset. Attributes can be taken from all three attribute collections of an input dataset (sa, fa, a – see
Dataset.get_attr()
), or from the collection of conditional attributes (ca) of a node instance. Corresponding collection name prefixes should be used to identify attributes, e.g. ‘ca.null_prob’ for the conditional attribute ‘null_prob’, or ‘fa.stats’ for the feature attribute stats. In addition to a plain attribute identifier it is possible to use a tuple to trigger more complex operations. The first tuple element is the attribute identifier, as described before. The second element is the name of the target attribute collection (sa, fa, or a). The third element is the axis number of a multidimensional array that shall be swapped with the current first axis. The fourth element is a new name that shall be used for an attribute in the output dataset. Example: (‘ca.null_prob’, ‘fa’, 1, ‘pvalues’) will take the conditional attribute ‘null_prob’ and store it as a feature attribute ‘pvalues’, while swapping the first and second axes. Simplified instructions can be given by leaving out consecutive tuple elements starting from the end.postproc : Node instance, optional
Node to perform post-processing of results. This node is applied in
__call__()
to perform a final processing step on the to be result dataset. If None, nothing is done.descr : str
Description of the instance
Attributes
auto_train
Whether the Learner performs automatic trainingwhen called untrained. bestdetector
descr
Description of the object if any fmeasure
force_train
Whether the Learner enforces training upon every call. fselector
is_trained
Whether the Learner is currently trained. nfeatures_min
pass_attr
Which attributes of the dataset or self.ca to pass into result dataset upon call pmeasure
postproc
Node to perform post-processing of results slicearg
space
Processing space name of this node splitter
stopping_criterion
train_pmeasure
update_sensitivity
Methods
__call__
(ds)forward
(data)Map data from input to output space. forward1
(data)Wrapper method to map single samples. generate
(ds)Yield processing results. get_postproc
()Returns the post-processing node or None. get_space
()Query the processing space name of this node. reset
()reverse
(data)Reverse-map data from output back into input space. reverse1
(data)set_postproc
(node)Assigns a post-processing node set_space
(name)Set the processing space name of this node. train
(ds)The default implementation calls _pretrain()
,_train()
, and finally_posttrain()
.untrain
()Reverts changes in the state of this node caused by previous training -
nfeatures_min
¶
-
update_sensitivity
¶