简体   繁体   English

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

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

With Keras Sequential Model Prediction,用 Keras 顺序 Model 预测,

To get Class Labels we can do要获得 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).

so所以

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

With the first class labels if i create confusion matrix, i get使用前 class 个标签,如果我创建混淆矩阵,我得到

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

But with the second class labels with the confusion matrix, i get 0 for True Positive and False Positive但是对于带有混淆矩阵的第二个 class 标签,我得到 0 表示真阳性和假阳性

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

May I know whats my issue here?我可以知道我的问题是什么吗?

The confusion matrix returns 2 rows/columns, which leads me to believe that you have two classes.混淆矩阵返回 2 行/列,这让我相信你有两个类。 The warning specifically says that you should use this line for binary classification, which is what you're doing:该警告特别指出您应该使用此行进行二进制分类,这就是您正在做的事情:

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

Please use instead:* np.argmax(model.predict(x), axis=-1) , if your model does multi-class classification (eg if it uses a softmax last-layer activation).* (model.predict(x) > 0.5).astype("int32") , if your model does binary classification (eg if it uses a sigmoid last-layer activation).请改用:* np.argmax(model.predict(x), axis=-1) ,如果您的 model 进行多类分类(例如,如果它使用softmax最后一层激活)。* (model.predict(x) > 0.5).astype("int32") ,如果您的 model 进行二进制分类(例如,如果它使用sigmoid最后一层激活)。

The error is that you used np.argmax(model.predict(X), axis=-1) on a 1D output and so this always returns the same column (because there's only one, so the maximum value will be in that column).错误是您在 1D output 上使用了np.argmax(model.predict(X), axis=-1) ,因此这总是返回同一列(因为只有一个,所以最大值将在该列中) . That explains that all your predicted values are in the same column in your confusion matrix.这说明您的所有预测值都在混淆矩阵的同一列中。

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

相关问题 从不同版本的 tf.keras 加载保存的模型(从 tf 2.3.0 到 tf 1.12) - Loading the saved models from tf.keras in different versions (From tf 2.3.0 to tf 1.12) 为 keras model 生成混淆矩阵 - 情绪分析 - Generating confusion matrix for keras model - Sentiment analysis 获取 keras model 中 3 个类的混淆矩阵 - Get confusion matrix for 3 classes in keras model 当以不同格式输入相同数据时,`tf.keras.model.evaluate()`会提供不同的结果 - `tf.keras.model.evaluate()` provides different results when fed the same data in different formats tf.keras `predict()` 得到不同的结果 - tf.keras `predict()` gets different results 使用不同tf设备的keras训练模型 - keras trained model using different tf device keras model.fit() 和 TF tape.gradient() 给出不同的结果 - keras model.fit() and TF tape.gradient() giving different results 加载 keras model 的结果不同 - Results of loaded keras model are different 带有 Keras Model 的混淆矩阵:一个问题,也许有人知道如何为这个 model 做混淆矩阵吗? - Confusion matrix with a Keras Model: A question, perhaps does anybody know how to do a confusion matrix for this model? sklearn.model_selection.cross_val_score 与对混淆矩阵进行手动计算的结果不同 - sklearn.model_selection.cross_val_score has different results from a manual calculation done on a confusion matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM