【发布时间】:2019-01-11 14:58:29
【问题描述】:
我训练了一个二元分类模型,得到了 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))
我收到了这个混淆矩阵:
正如 sklearn 所说:
这里说的假阴性和假阳性是如此之高。既然我有 98% 的测试准确率,这怎么可能呢?此外,我多次使用该模型生成预测(使用 model.predict() 函数)并手动检查它们。但每次它都给了我正确的分类。
任何想法如何获得准确的结果?
【问题讨论】:
-
你的真实数据真的像你的 y_true 变量一样分布吗?
-
@CupinaCoffee 我已经设置了
shuffle=false来做到这一点。 -
好的。检查来自 soumendra 的评论 github.com/keras-team/keras/issues/3477
-
@CupinaCoffee 谢谢。我之前看过那篇文章,但他使用了 train_generator.class_indices,因为我已经训练了模型,所以我没有。
-
听起来您的初始模型可能在训练期间过度拟合。你能描述一下你训练模型的过程吗?
标签: python tensorflow scikit-learn keras confusion-matrix