混乱矩阵和分类报告

问题描述 投票:0回答:1

我成功地创建了一个简单的1D CNN进行分类,有3个类。在训练过程中,我将模型和权重保存到yaml和h5文件中。然后,在测试过程中,我成功地加载了模型和权重,并将其用于实时分类,通过返回类作为输出。但是,我也用测试数据来测试我的模型,我想把它看成一个混淆矩阵。这是我做的代码。

from keras.models import model_from_yaml
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix
import os
from numpy import array
import numpy as np
import pandas as pd

# load YAML and create model
yaml_file = open('a32.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)

# load weights into new model
loaded_model.load_weights("a32.h5")
print("Loaded model from disk")

loaded_model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer='adam',
    metrics=(['accuracy'])
)

#Load data
test_data=pd.read_csv('data/ccpp/t2datn.csv',header=None)
test=test_data.iloc[:,0:2]

#Normalized test set
scaler=StandardScaler().fit(test)
x_test=scaler.transform(test)
y=np.expand_dims(x_test,axis=2)

#Make a prediction
predictions = loaded_model.predict(y)
ynew = loaded_model.predict_classes(y)

yp = np.argmax(predictions, axis=1)
#print(yp)
print("Confusion Matrix")
print(confusion_matrix(ynew, yp))
print("Classification Report")
target_names = ['Too High', 'Normal', 'Too Low']
print(classification_report(ynew,yp, target_names=target_names))

但我总是得到100%分类的输出结果。但是,当我评估测试数据时,准确率只有80%左右。你能告诉我混淆矩阵的代码哪一部分是错误的吗?

输出。

Confusion Matrix
[[1967    0    0]
 [   0 3252    0]
 [   0    0 1159]]
Classification Report
              precision    recall  f1-score   support

    Too High       1.00      1.00      1.00      1967
      Normal       1.00      1.00      1.00      3252
     Too Low       1.00      1.00      1.00      1159

    accuracy                           1.00      6378
   macro avg       1.00      1.00      1.00      6378
weighted avg       1.00      1.00      1.00      6378
python keras classification confusion-matrix
1个回答
0
投票

ynewyp这两个都是你从模型中得到的预测。predict_classes()predict() 分别。

而confusion_matrix()和classification_report()则取输入为--。confusion_matrix(y_true, y_pred) 哪儿 y_true 是目标班级和 y_pred 在模型的预测中。

你应该输入你的目标而不是 ynew.


0
投票

在您的分类报告中,您比较的是 ynewyp 基本上是一样的,但计算方式不同。你应该比较 ynew 附带 y_groundtruth.

© www.soinside.com 2019 - 2024. All rights reserved.