【问题标题】:sklearn train_test_split: where to add average=None when we get: "Target is multiclass but average='binary'... error?sklearn train_test_split:当我们得到:“目标是多类但平均值='二进制'......错误时,在哪里添加平均值=无?
【发布时间】:2020-11-08 23:08:22
【问题描述】:

我知道我应该在某处添加average=None,但我真的不知道,目标变量是一组数字:

from sklearn.model_selection import train_test_split

trainset, testset = train_test_split(df, test_size=0.2, random_state=0)

def preprocessing(df):
    
    
    X = df.drop('log_price', axis=1)
    y = df['log_price'] 
    
    print(y.value_counts())
    
    return X, y

X_train, y_train = preprocessing(trainset)

X_test, y_test = preprocessing(testset)



from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import learning_curve

def evaluation(model):
    
    model.fit(X_train, y_train)
    ypred = model.predict(X_test)
    
    print(confusion_matrix(y_test, ypred))
    print(classification_report(y_test, ypred))
    
    N, train_score, val_score = learning_curve(model, X_train, y_train,
                                              cv=4, scoring='f1',
                                                train_sizes=np.linspace(0.1, 1, 10))
    
    
    plt.figure(figsize=(12, 8))
    plt.plot(N, train_score.mean(axis=1), label='train score')
    plt.plot(N, val_score.mean(axis=1), label='validation score')
    plt.legend()



from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.svm import SVC 
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.preprocessing import PolynomialFeatures, StandardScaler

preprocessor = make_pipeline(PolynomialFeatures(2, include_bias=False), SelectKBest(f_classif, k=10))

KNN = make_pipeline(preprocessor, StandardScaler(), KNeighborsClassifier())

dict_of_models = {
                  'KNN': KNN
                 }


for name, model in dict_of_models.items():
    print(name)
    evaluation(model)

我收到此错误:

ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].

谢谢。

【问题讨论】:

  • 请提供错误回溯。第一个猜测:如果要设置其选项,则需要提供learning_curvescoring 参数而不是字符串f1
  • Traceback: pastebin.com/yhPRtmAH 我无法将它添加到我的原始帖子中,因为它太长了。请注意,他的代码与二进制目标完美配合。如果我删除 learning_curve 我不会收到错误...
  • 查看链接中的混淆矩阵...您在此处使用分类而不是回归是否有特殊原因?

标签: python pandas scikit-learn


【解决方案1】:

出现错误是因为您使用'f1' 作为learning_curve 中的评分参数。这仅适用于二进制目标。但是,正如错误消息所示,您的根本问题是 multiclass 问题。因此,您需要另一种具有适当平均策略的评分方法。可以在here 中找到预定义值。一个使用'f1_macro'的例子:

N, train_score, val_score = learning_curve(model, X_train, y_train,
                                           cv=4, 
                                           scoring='f1_macro', # <-- change here
                                           train_sizes=np.linspace(0.1, 1, 10)
)

作为macro 平均工作原理的参考:

计算每个标签的指标并找到它们的未加权平均值。这没有考虑标签不平衡。

可以通过提供的链接找到更多选项。我不知道有一个等效于 None 的方法,它返回每个班级的分数。

【讨论】:

    猜你喜欢
    • 2019-09-12
    • 2019-02-15
    • 2015-01-13
    • 2021-12-05
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多