keras 混淆矩阵问题;结果仅显示0和1

kq0g1dla  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(569)

我有同样的问题,总是一样的。当我运行一个混淆矩阵的结果显示只有0和1。它不应该这样去,这里的问题。

它应该在0和20之间,但它不是。我做错了什么?我怎么能解决这个问题。
这是我用的代码

from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator

imagegen = ImageDataGenerator()

test_datagen = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
)

test_generator = test_datagen.flow_from_directory(r"C:\Users\User\Desktop\Problem\New_one\Testing_Data",
                                                    class_mode="categorical",
                                                    shuffle=False,
                                                    batch_size=3,
                                                    target_size=(200, 200))

model = keras.models.load_model(r"C:\Users\User\Desktop\Problem\New_one\InceptionV3.h5")

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = {'y_Actual': ["Lablae", "Long", "Maejam(Ceiyng-saen-hnoy)","Maejam(Ceiyng-saen-luang)","Maejam(Hong-pi)","Maejam(Hong-poi)","Maejam(Kan-seiyn-sam)",
                    "Maejam(Kom-rup-nk)","Maejam(Kom-whua-mon-nai-nk-non)","Maejam(Kud-kho-bed)","Maejam(La-kon-klang)","Maejam(La-kon-luang)",
                    "Maejam(La-kon-noy)","Maejam(Lay-kan-sam-aew)","Maejam(Nak-kum)","Maejam(Nk-kum)","Maejam(Nok-nk-kum)","Maejan(Khan-aew-u)",
                    "Muang-nan","Sri-sat-shanalai"],
        'y_Predicted': ["Lablae", "Long", "Maejam(Ceiyng-saen-hnoy)","Maejam(Ceiyng-saen-luang)","Maejam(Hong-pi)","Maejam(Hong-poi)","Maejam(Kan-seiyn-sam)",
                    "Maejam(Kom-rup-nk)","Maejam(Kom-whua-mon-nai-nk-non)","Maejam(Kud-kho-bed)","Maejam(La-kon-klang)","Maejam(La-kon-luang)",
                    "Maejam(La-kon-noy)","Maejam(Lay-kan-sam-aew)","Maejam(Nak-kum)","Maejam(Nk-kum)","Maejam(Nok-nk-kum)","Maejan(Khan-aew-u)",
                    "Muang-nan","Sri-sat-shanalai"]}

df = pd.DataFrame(data, columns=['y_Actual','y_Predicted'])
confusion_matrix = pd.crosstab(df['y_Actual'], df['y_Predicted'], rownames=['Actual'], colnames=['Predicted'], margins = True)

sn.heatmap(confusion_matrix, annot=True)
plt.show()

我在这里承认。我不擅长编程,而且永远也不会。这是我唯一的问题,我需要解决它,这样我才能进步。
另外,我的英语写得不好,所以我可能在这里犯了一些错误。如果我犯了,那么我道歉。

wf82jlnq

wf82jlnq1#

您的预测与真实标签匹配。因此,您执行了100%准确性的完美分类。
请确保从model(即当前未使用的model.predict())获取预测。从具有margins=False的交叉表中排除小计,否则将小计包括在混淆矩阵中。
下面是一个示例,针对具有一个已编辑值(dict中的最后一个)的不太理想的分类,以演示此概念:

import pandas as pd

import seaborn as sn
import matplotlib.pyplot as plt

data = {
    "y_Actual": [
        "Lablae",
        "Long",
        "Maejam(Ceiyng-saen-hnoy)",
        "Maejam(Ceiyng-saen-luang)",
        "Maejam(Hong-pi)",
        "Maejam(Hong-poi)",
        "Maejam(Kan-seiyn-sam)",
        "Maejam(Kom-rup-nk)",
        "Maejam(Kom-whua-mon-nai-nk-non)",
        "Maejam(Kud-kho-bed)",
        "Maejam(La-kon-klang)",
        "Maejam(La-kon-luang)",
        "Maejam(La-kon-noy)",
        "Maejam(Lay-kan-sam-aew)",
        "Maejam(Nak-kum)",
        "Maejam(Nk-kum)",
        "Maejam(Nok-nk-kum)",
        "Maejan(Khan-aew-u)",
        "Muang-nan",
        "Sri-sat-shanalai",
    ],
    "y_Predicted": [
        "Lablae",
        "Long",
        "Maejam(Ceiyng-saen-hnoy)",
        "Maejam(Ceiyng-saen-luang)",
        "Maejam(Hong-pi)",
        "Maejam(Hong-poi)",
        "Maejam(Kan-seiyn-sam)",
        "Maejam(Kom-rup-nk)",
        "Maejam(Kom-whua-mon-nai-nk-non)",
        "Maejam(Kud-kho-bed)",
        "Maejam(La-kon-klang)",
        "Maejam(La-kon-luang)",
        "Maejam(La-kon-noy)",
        "Maejam(Lay-kan-sam-aew)",
        "Maejam(Nak-kum)",
        "Maejam(Nk-kum)",
        "Maejam(Nok-nk-kum)",
        "Maejan(Khan-aew-u)",
        "Sri-sat-shanalai",
        "Sri-sat-shanalai",
    ],
}

df = pd.DataFrame(data, columns=["y_Actual", "y_Predicted"])
confusion_matrix = pd.crosstab(
    df["y_Actual"],
    df["y_Predicted"],
    rownames=["Actual"],
    colnames=["Predicted"],
    margins=False,
)

sn.heatmap(confusion_matrix, annot=True)
plt.show()

相关问题