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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With