【问题标题】:Warning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter警告:精度和 F 分数定义不明确,在没有预测样本的标签中设置为 0.0。使用 `zero_division` 参数
【发布时间】:2021-10-02 16:27:51
【问题描述】:

谁能帮我解决这个错误:

警告:精度和 F 分数定义不明确,在没有预测样本的标签中设置为 0.0。使用zero_division 参数来控制此行为。 _warn_prf(平均值,修饰符,msg_start,len(结果))

当我添加亚当的调整参数时出现错误。

#tunning parameter from keras.optimizers import Adam
optimize = Adam(learning_rate=0.00001,beta_1=0.9,beta_2=0.99)
model.compile(optimizer=optimize,loss='categorical_crossentropy', metrics=['accuracy'])

有人理解这段代码的错误吗?

from sklearn.metrics import confusion_matrix, classification_report
prediksi = model.predict(test_data_generator)
y_pred = np.argmax(prediksi, axis=1)
print(confusion_matrix(test_data_generator.classes,y_pred))
print(classification_report(test_data_generator.classes,y_pred))

我也尝试过使用labels=np.unique(y_pred),但结果没有显示出准确度的值

【问题讨论】:

  • 欢迎来到 StackOverflow!您会注意到这实际上不是错误,而是警告。它也不会发生在 Adam 行中,而是发生在 classification_report 行中——这是因为分类报告计算 F1。

标签: python scikit-learn jupyter-notebook


【解决方案1】:

出现此警告是因为 y_true 包含您的预测 (y_pred) 中不存在的标签,如下例所示:

import numpy as np
from sklearn.metrics import confusion_matrix, classification_report

y_pred = np.ones(10,)
y_true = np.ones(10,)
y_true[0]=0
print(confusion_matrix(y_true,y_pred))
print(classification_report(y_true,y_pred))

您可以通过设置classification_report argumentzero_division=1 来消除此警告。

但这并不明智,因为它表明你的分类器有问题。

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 2019-09-22
    • 2018-09-25
    • 2017-08-27
    • 2018-09-29
    • 2016-05-15
    • 2018-08-05
    • 2019-09-30
    • 2019-06-06
    相关资源
    最近更新 更多