繁体   English   中英

混淆矩阵在 Keras model tf==2.3.0 中产生不同的结果

[英]Confusion Matrix produces different results in Keras model tf==2.3.0

用 Keras 顺序 Model 预测,

要获得 Class 标签,我们可以做

yhat_classes1 = Keras_model.predict_classes(predictors)[:, 0] #this shows deprecated warning in tf==2.3.0

WARNING:tensorflow:From <ipython-input-54-226ad21ffae4>:1: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.
Instructions for updating:
Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).

所以

yhat_classes2 = np.argmax(Keras_model.predict(predictors), axis=-1)

使用前 class 个标签,如果我创建混淆矩阵,我得到

matrix = confusion_matrix(actual_y, yhat_classes1)
 [[108579   8674]
 [  1205  24086]]

但是对于带有混淆矩阵的第二个 class 标签,我得到 0 表示真阳性和假阳性

matrix = confusion_matrix(actual_y, yhat_classes2)
 [[117253      0]
 [ 25291      0]]

我可以知道我的问题是什么吗?

混淆矩阵返回 2 行/列,这让我相信你有两个类。 该警告特别指出您应该使用此行进行二进制分类,这就是您正在做的事情:

(model.predict(x) > 0.5).astype("int32")

请改用:* np.argmax(model.predict(x), axis=-1) ,如果您的 model 进行多类分类(例如,如果它使用softmax最后一层激活)。* (model.predict(x) > 0.5).astype("int32") ,如果您的 model 进行二进制分类(例如,如果它使用sigmoid最后一层激活)。

错误是您在 1D output 上使用了np.argmax(model.predict(X), axis=-1) ,因此这总是返回同一列(因为只有一个,所以最大值将在该列中) . 这说明您的所有预测值都在混淆矩阵的同一列中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM