【问题标题】:f-score: ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targetsf-score:ValueError:分类指标无法处理多标签指标和连续多输出目标的混合
【发布时间】:2019-10-22 20:02:36
【问题描述】:

我正在尝试为我的模型所做的预测计算微 F 度量。我使用带有 Keras 和 Tensorflow 的 word2vec 向量训练了模型。我使用 scikit 库来计算 mirco F 度量。

但该函数会抛出此消息:

ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets

另外,我做的预测对吗?我在x_train(wordVectors)y_train(resultVectors) 上训练了模型,并在x_testy_test 上进行了验证。

现在我对x_test 进行了预测,并想使用y_test 评估预测。到目前为止我做得对吗?

预测数组如下所示:

[[ 1.7533608e-02  5.8055294e+01  2.2185498e-03 ... -1.2394511e-03
   1.0454212e+00 -1.6698670e-03]
 [ 1.7539740e-02  5.8173992e+01  2.1747553e-03 ... -1.2764656e-03
   1.0475068e+00 -1.6941782e-03]
 [ 1.7591618e-02  5.8222389e+01  2.2053251e-03 ... -1.2856000e-03
   1.0484750e+00 -1.6668942e-03] ...

真实值如下所示:

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]...

我已经尝试将两个数组都转换为二进制值(使用np.argmax(..., axis=1))。然后没有错误,我得到了大约 0.59 的微 F 测量值……这太高了,所以我认为我做错了。 我的问题是是否有另一种转换数据的方法?我可以将预测转换为多标签指标值吗?

model = load_model('model.h5')
prediction = model.predict(x_test)

prediction_binary = np.argmax(prediction, axis=1)
y_test_binary = np.argmax(y_test, axis=1)

print(f1_score(y_test_binary, prediction_binary, average='micro'))

我希望输出为

【问题讨论】:

    标签: python keras model evaluation measure


    【解决方案1】:

    问题在于,您仅在输出向量的最大值预测的标签上计算指标,而测试向量只有一个值。

    确实,np.argmax 只返回一个值,即使向量有多个最小值。 例如np.argmax([0,0,1,0,1,1]) 将只返回 2。

    由于您的问题包含多标签分类问题,您希望您的输入可能被分类为多个类别。为此,您必须将分类器的输出向量转换为与测试向量相同的形状。

    你可以这样做:

    prediction_int = np.zeroes_like(prediction)
    prediction_int[prediction > 0.5] = 1
    

    【讨论】:

    • 感谢您的解释!这有很大帮助。我猜你的代码可以完成这项工作。我现在得到一个 0.02 的微 F 测量值。这很可能是真的。
    • 使用更方便prediction_int = np.rint(prediction)
    猜你喜欢
    • 2018-08-05
    • 2021-12-20
    • 2021-04-14
    • 2020-05-10
    • 2018-12-03
    • 2019-10-23
    • 2019-06-29
    • 2021-05-14
    相关资源
    最近更新 更多