如何获得keras模型的准确性,F1,准确性和召回率?


21

我想为我的二进制KerasClassifier模型计算精度,召回率和F1分数,但找不到任何解决方案。

这是我的实际代码:

# Split dataset in train and test data 
X_train, X_test, Y_train, Y_test = train_test_split(normalized_X, Y, test_size=0.3, random_state=seed)

# Build the model
model = Sequential()
model.add(Dense(23, input_dim=45, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))

# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])


tensorboard = TensorBoard(log_dir="logs/{}".format(time.time()))

time_callback = TimeHistory()

# Fit the model
history = model.fit(X_train, Y_train, validation_split=0.3, epochs=200, batch_size=5, verbose=1, callbacks=[tensorboard, time_callback]) 

然后,我根据新的测试数据进行预测,并得到如下的混淆矩阵:

y_pred = model.predict(X_test)
y_pred =(y_pred>0.5)
list(y_pred)

cm = confusion_matrix(Y_test, y_pred)
print(cm)

但是,是否有解决方案来获得准确性分数,F1分数,准确性和召回率?(如果不复杂,也可以使用交叉验证分数,但此答案不是必需的)

感谢您的任何帮助!

Answers:


21

指标已从Keras核心中删除。您需要手动计算它们。他们在2.0版本上删除了它们。这些指标都是全局指标,但是Keras批量工作。结果,这可能会产生误导,而不是有所帮助。

但是,如果您确实需要它们,可以这样做

from keras import backend as K

def recall_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall

def precision_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision

def f1_m(y_true, y_pred):
    precision = precision_m(y_true, y_pred)
    recall = recall_m(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc',f1_m,precision_m, recall_m])

# fit the model
history = model.fit(Xtrain, ytrain, validation_split=0.3, epochs=10, verbose=0)

# evaluate the model
loss, accuracy, f1_score, precision, recall = model.evaluate(Xtest, ytest, verbose=0)

如果它们可能产生误导,那么如何评估Keras模型?
ZelelB

1
由于Keras在每个批次的末尾计算这些指标,因此您可以从“实际”指标中获得不同的结果。另一种方法是在训练和测试中拆分数据集,并使用测试部分来预测结果。然后,由于您知道真实的标签,因此可以计算精度并手动调用。
Tasos

Taso,我是否可以建议您在这个SO问题中重新发布您的答案:如何在keras的神经网络模型中实现精度和召回指标?伊拉克利斯欢呼
声-desertnaut

抱歉,刚刚看到它关闭了:(
desertnaut

知道为什么这对我不起作用吗?很好的训练。
罗德里戈·鲁伊斯

13

您可以使用scikit-learn分类报告。要将标签转换为数字或二进制格式,请查看scikit-learn标签编码器

from sklearn.metrics import classification_report

y_pred = model.predict(x_test, batch_size=64, verbose=1)
y_pred_bool = np.argmax(y_pred, axis=1)

print(classification_report(y_test, y_pred_bool))

这会给您(从scikit-learn示例复制的输出):

             precision  recall   f1-score    support

 class 0       0.50      1.00      0.67         1
 class 1       0.00      0.00      0.00         1
 class 2       1.00      0.67      0.80         3

2
这就是我使用的,简单有效的方法。
马修,

2

您也可以尝试以下所述。

from sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix
y_pred1 = model.predict(X_test)
y_pred = np.argmax(y_pred1, axis=1)

# Print f1, precision, and recall scores
print(precision_score(y_test, y_pred , average="macro"))
print(recall_score(y_test, y_pred , average="macro"))
print(f1_score(y_test, y_pred , average="macro"))

0

试试这个:https : //scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_fscore_support.html,以Y_test和y_pred为参数。


我试过这个:model.recision_recall_fscore_support(Y_test,y_pred,average ='micro')并在执行时得到此错误:AttributeError:'Sequential'对象没有属性'
recision_recall_fscore_support'– ZelelB

您无需指定model.recision_recall_fscore_support(),而只需指定recision_recall_fscore_support(Y_test,y_pred,average ='micro')(不带“模型”。)并确保您具有正确的导入:从sklearn.metrics导入precision_recall_fscore_support)
Viacheslav Komisarenko
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.