我训练了一个二元分类模型,得到了 98% 的测试准确率和 99% 的训练准确率。
今天我想计算混淆矩阵并使用下面的代码来计算它们。
model = load_model("model.h5")
testGenerator = ImageDataGenerator(rotation_range=5,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=False,
fill_mode='nearest'
)
testData = testGenerator.flow_from_directory(
'Location',
target_size=(74,448),
batch_size=15,
class_mode='binary',
shuffle=False
)
proba = model.predict_generator(testData,steps=3000//15)
y_true = np.array([0] * 1482 + [1] * 1482 )
y_pred = proba > 0.5
print(confusion_matrix(y_true, y_pred))
Run Code Online (Sandbox Code Playgroud)
我收到了这个混淆矩阵:
正如 sklearn 所说:
这说明这里的假阴性和假阳性如此之高。既然我有 98% 的测试准确率,那怎么可能呢?此外,我多次使用该模型来生成预测(使用 model.predict() 函数)并手动检查它们。但每次它都给了我正确的分类。
任何想法如何获得准确的结果?