【发布时间】:2020-08-21 04:59:36
【问题描述】:
我成功创建了一个简单的 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