'StackingClassifier' object has no attribute 'classes_'

15 Views Asked by At
from sklearn.linear_model import LogisticRegression
from mlxtend.classifier import StackingClassifier
from sklearn.model_selection import cross_val_score
lr = LogisticRegression()
names = ['LGBMClassifier', 'RandomForestClassifier', 'DecisionTreeClassifier', 'KNeighborsClassifier', 'CatBoostClassifier']
sclf = StackingClassifier(classifiers=[models[name] for name in names],
                          use_probas=True,average_probas=True,meta_classifier=lr)

scores = cross_val_score(sclf, feature, y, cv=3, scoring='accuracy')
print(f'Accuracy:{scores.mean()} +/- {scores.std()}')

models have been trained and their accuracies are about 0.75

train code:

from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score
kf = KFold(n_splits=5,shuffle=True,random_state=0)
scores_all = []
for name in models:
    best_score = -1
    scores = []
    model = models[name]
    for index,(train_index, test_index) in enumerate(kf.split(feature)):
        train_x, train_y = feature.iloc[train_index,:], y[train_index]
        val_x, val_y = feature.iloc[test_index,:], y[test_index]
        
        model.fit(train_x, train_y)
        score = accuracy_score(val_y, model.predict(val_x))
        scores.append(score)
        if score > best_score:
            models[name] = model
        
        print(f'epoch:{index + 1}, model:{name}, best_score:{scores[-1]}')
    scores_all.append(scores)

but problem is AttributeError: 'StackingClassifier' object has no attribute 'classes_'

how to fix it? i print scores scores are NAN

0

There are 0 best solutions below