Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to extract the number of features from a model after I had fitted this model to my data.

I have looked through the model's directory and found ways to get the number only for specific models (e.g. looking at the dimensions of support vectors for SVM), but I didn't find a general way I could use for any type of a model.

Say I have my dataset of instances and corresponding classes

X, y    # dataset

and use an arbitrary model from the scikit-learn library to fit this data

model.fit(X,y)

Later I want to use this model to find the dimensions of the original dataset, something in the way of

model.n_features_

Is there a quick and general way to do this?

like image 885
Vavrinec Avatar asked Mar 05 '23 06:03

Vavrinec


1 Answers

There is no single common attribute for all classifier in Sklearn.

I would recommend the following:

For any sklearn.linear_model/sklearn.svm.svc, you can use the following approach.

>>> clf.coef_.shape[-1]
    

For any tree based models (DecisionTreeClassifier/RandomForestClassifier/GradientBoostingClassifier), you can use

>>> clf.n_features_

Update:

New in version 1.0.

n_features_in_: int

Number of features seen during fit.

feature_names_in_:

Names of features seen during fit. Defined only when X has feature names that are all strings.

like image 52
Venkatachalam Avatar answered May 01 '23 00:05

Venkatachalam