How to get a list of attributes from a fitted model in Scikit learn?


bohemia

Is there any way to get a list of features (attributes) from a used model (or a whole table of used training data) in Scikit-learn? I am using some preprocessing like feature selection and I want to know the selected features and removed features. For example, I use random forest classifier and recursive feature elimination.

Paul Edward

The mask of the selected features is stored in the '_support' attribute of the RFE object.

Check out the documentation here : http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html#sklearn.feature_selection.RFE

Here is an example:

from sklearn.datasets import make_friedman1
from sklearn.feature_selection import RFE
from sklearn.svm import SVR

# load a dataset
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)

estimator = SVR(kernel="linear")
selector = RFE(estimator, 5, step=1)
X_new = selector.fit_transform(X, y)

print selector.support_ 
print selector.ranking_

will show:

array([ True,  True,  True,  True,  True,
      False, False, False, False, False], dtype=bool)
array([1, 1, 1, 1, 1, 6, 4, 3, 2, 5]) 

Note that if you want to use the random forest classifier in your RFE model, you will get this error:

AttributeError: 'RandomForestClassifier' object has no attribute 'coef_'

I found a working environment in this thread: Recursive feature elimination on Random Forest using scikit-learn

You have to override the RandomForestClassifier class like this:

class RandomForestClassifierWithCoef(RandomForestClassifier):
    def fit(self, *args, **kwargs):
        super(RandomForestClassifierWithCoef, self).fit(*args, **kwargs)
        self.coef_ = self.feature_importances_

Hope it helps you :)

Related


How to get number of features from fitted scikit-learn model?

Wavlin I am trying to extract the number of features from the model after fitting the model to the data. I browsed the catalog of models and found ways to get only a specific model number (e.g. looking at the dimensionality of the SVM support vector), but I di

How to get number of features from fitted scikit-learn model?

Wavlin I am trying to extract the number of features from the model after fitting the model to the data. I browsed the catalog of models and found ways to get only a specific model number (e.g. looking at the dimensionality of the SVM support vector), but I di

How to get number of features from fitted scikit-learn model?

Wavlin I am trying to extract the number of features from the model after fitting the model to the data. I browsed the catalog of models and found ways to get only a specific model number (e.g. looking at the dimensionality of the SVM support vector), but I di

How to get a table of paired p-values from a fitted model

new arrival_R I have a data dtbelow and I fit a model to this data. I want to get a table of pairwise p-values for all possible pairs (all levels) in the model. data: dt <- structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,

How to get a table of paired p-values from a fitted model

new arrival_R I have a data dtbelow and I fit a model to this data. I want to get a table of pairwise p-values for all possible pairs (all levels) in the model. data: dt <- structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,

How to get a table of paired p-values from a fitted model

new arrival_R I have a data dtbelow and I fit a model to this data. I want to get a table of pairwise p-values for all possible pairs (all levels) in the model. data: dt <- structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,

How to get a table of paired p-values from a fitted model

new arrival_R I have a data dtbelow and I fit a model to this data. I want to get a table of pairwise p-values for all possible pairs (all levels) in the model. data: dt <- structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,

How to get a table of paired p-values from a fitted model

new arrival_R I have a data dtbelow and I fit a model to this data. I want to get a table of pairwise p-values for all possible pairs (all levels) in the model. data: dt <- structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,

Get model properties from scikit-learn pipeline

lmart999: I usually PCAload like this: pca = PCA(n_components=2) X_t = pca.fit(X).transform(X) loadings = pca.components_ If I PCAuse the scikit-learntubing to run... from sklearn.pipeline import Pipeline pipeline = Pipeline(steps=[ ('scaling',StandardSca

Get model properties from scikit-learn pipeline

lmart999: I usually PCAload like this: pca = PCA(n_components=2) X_t = pca.fit(X).transform(X) loadings = pca.components_ If I PCAuse the scikit-learntubing to run... from sklearn.pipeline import Pipeline pipeline = Pipeline(steps=[ ('scaling',StandardSca

Get model properties from scikit-learn pipeline

lmart999 I usually PCAload like this: pca = PCA(n_components=2) X_t = pca.fit(X).transform(X) loadings = pca.components_ If I PCAuse the scikit-learntubing to run... from sklearn.pipeline import Pipeline pipeline = Pipeline(steps=[ ('scaling',StandardScal

Get model properties from scikit-learn pipeline

lmart999 I usually PCAload like this: pca = PCA(n_components=2) X_t = pca.fit(X).transform(X) loadings = pca.components_ If I PCAuse the scikit-learntubing to run... from sklearn.pipeline import Pipeline pipeline = Pipeline(steps=[ ('scaling',StandardScal

Get model properties from scikit-learn pipeline

lmart999: I usually PCAload like this: pca = PCA(n_components=2) X_t = pca.fit(X).transform(X) loadings = pca.components_ If I PCAuse the scikit-learntubing to run... from sklearn.pipeline import Pipeline pipeline = Pipeline(steps=[ ('scaling',StandardSca

How to use scikit learn model from C#

Nadjib Bendaoud I have learned many models using scikit and I want to make predictions on these models through a C# program, is there any API that can help me to do this? Michael Tannenbaum As far as I know, it is not possible to load sklearn models directly i

How to use scikit learn model from C#

Nadjib Bendaoud I have learned many models using scikit and I want to make predictions on these models through a C# program, is there any API that can help me to do this? Michael Tannenbaum As far as I know, it is not possible to load sklearn models directly i

How to use scikit learn model from C#

Nadjib Bendaoud I have learned many models using scikit and I want to make predictions on these models through a C# program, is there any API that can help me to do this? Michael Tannenbaum As far as I know, it is not possible to load sklearn models directly i

LassoCV model inherited from scikit-learn

Marcus K I'm trying to extend scikit-learn's RidgeCV model using inheritance: from sklearn.linear_model import RidgeCV, LassoCV class Extended(RidgeCV): def __init__(self, *args, **kwargs): super(Extended, self).__init__(*args, **kwargs) def