I want to use SelectFromModel
for selecting the best features for my model. However, I get an error when I want to define classification model.
For example (see the code below) this code works, it also works for decision tree, random forest and logistic regression:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import RFE, SelectFromModel
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
df_data = pd.read_csv('data.csv', sep = ' ', header=None)
df_target = pd.read_csv('target.csv', names=['output'])
x = full_df.iloc[:,:-1]
y = full_df.iloc[:,-1]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0)
print(x_train.shape)
clf = SVC(kernel = 'linear').fit(x_train, y_train)
model = SelectFromModel(clf, prefit=True)
print(model.transform(x_train).shape)
But when I try to use different classifier, for example:
clf = SVC(kernel = 'poly').fit(x_train, y_train)
clf = SVC(kernel = 'sigmoid').fit(x_train, y_train)
clf = SVC(kernel = 'rbf').fit(x_train, y_train)
It gives me the error:
ValueError: The underlying estimator SVC has no `coef_` or `feature_importances_` attribute. Either pass a fitted estimator to SelectFromModel or call fit before calling transform.
Why it gives me this error, my classifiers are all on the same place, and they are fitted?