我在用tf-keras训练神经网络。它是一种多标签分类,其中每个样本属于多个类1,0,1,0..etc。最后一个模型行(只是为了清楚起见)是:
model.add(tf.keras.layers.Dense(9, activation='sigmoid'))#final layer
model.compile(loss='binary_crossentropy', optimizer=optimizer,
metrics=[tf.keras.metrics.BinaryAccuracy(),
tfa.metrics.F1Score(num_classes=9, average='macro',threshold=0.5)])我需要为这些数据生成精确性、召回性和F1分数(我已经在培训期间得到了F1分数)。为此,我使用滑雪板分类报告,但我需要确认,我是正确地使用它在多标签设置。
from sklearn.metrics import classification_report
pred = model.predict(x_test)
pred_one_hot = np.around(pred)#this generates a one hot representation of predictions
print(classification_report(one_hot_ground_truth, pred_one_hot))这很好,我从tensorflow加载项(对于宏F1)获得了每个类的完整报告,包括与F1score度量相匹配的F1score分数。很抱歉,这篇文章太冗长了,但我不确定的是:
在多标签设置的情况下,预测需要一个热编码,这是正确的吗?如果我通过正常预测分数(乙状结肠概率),就会抛出一个错误.
谢谢。
发布于 2021-07-20 11:42:50
将classification_report用于二进制、多类和多标签分类是正确的.
标签不是一个热编码的情况下,多类分类.它们只需是indices或labels。
可以看到,下面这两段代码输出的输出是相同的:
带有索引的示例
from sklearn.metrics import classification_report
import numpy as np
labels = np.array(['A', 'B', 'C'])
y_true = np.array([1, 2, 0, 1, 2, 0])
y_pred = np.array([1, 2, 1, 1, 1, 0])
print(classification_report(y_true, y_pred, target_names=labels))带有标签的示例
from sklearn.metrics import classification_report
import numpy as np
labels = np.array(['A', 'B', 'C'])
y_true = labels[np.array([1, 2, 0, 1, 2, 0])]
y_pred = labels[np.array([1, 2, 1, 1, 1, 0])]
print(classification_report(y_true, y_pred))两次返回
precision recall f1-score support
A 1.00 0.50 0.67 2
B 0.50 1.00 0.67 2
C 1.00 0.50 0.67 2
accuracy 0.67 6
macro avg 0.83 0.67 0.67 6
weighted avg 0.83 0.67 0.67 6在多标签分类的上下文中,classification_report可以在下面的示例中使用:
from sklearn.metrics import classification_report
import numpy as np
labels =['A', 'B', 'C']
y_true = np.array([[1, 0, 1],
[0, 1, 0],
[1, 1, 1]])
y_pred = np.array([[1, 0, 0],
[0, 1, 1],
[1, 1, 1]])
print(classification_report(y_true, y_pred, target_names=labels))https://stackoverflow.com/questions/68374999
复制相似问题