Chr*_*her 5 python classification scikit-learn
我有以下分类报告的输出:
precision recall f1-score support
0 0.6772 0.5214 0.5892 491
1 0.8688 0.9273 0.8971 1678
avg / total 0.8254 0.8354 0.8274 2169
Run Code Online (Sandbox Code Playgroud)
数据集中的真实标签是s和p。
问题:我怎么知道哪个标签是“0”,哪个是“1”?或者:我如何通过labels=或target_names=以正确的顺序分配标签?
Viv*_*mar 13
如无特别说明,将按字母顺序排列。所以很可能是:
0 -> 'p'
1 -> 's'
无论如何,如果您传递实际标签,它们应该按原样显示。例如:
y_true = ['p', 's', 'p', 's', 'p']
y_pred = ['p', 'p', 's', 's', 'p']
print(classification_report(y_true, y_pred))
Output:
precision recall f1-score support
p 0.67 0.67 0.67 3
s 0.50 0.50 0.50 2
avg / total 0.60 0.60 0.60 5
Run Code Online (Sandbox Code Playgroud)
所以不需要做任何事情。但是,如果您更改了标签,则可以将它们传递到target_names参数中以显示在报告中。
假设您已将 'p' 转换为 0,将 's' 转换为 1,那么您的代码变为:
y_true = [0, 1, 0, 1, 0]
y_pred = [0, 0, 1, 1, 0]
# Without the target_names
print(classification_report(y_true, y_pred))
0 0.67 0.67 0.67 3
1 0.50 0.50 0.50 2
avg / total 0.60 0.60 0.60 5
#With target_names
print(classification_report(y_true, y_pred, target_names=['p', 's']))
p 0.67 0.67 0.67 3
s 0.50 0.50 0.50 2
avg / total 0.60 0.60 0.60 5
Run Code Online (Sandbox Code Playgroud)
如果您使用 sklearn.preprocess.LabelEncoder 对原始标签进行编码,则可以使用inverse_transform获取原始标签
target_strings = label_encoder.inverse_transform(np.arange(num_classes))
metrics.classification_report(dev_gold, dev_predicted, target_names=target_strings)
Run Code Online (Sandbox Code Playgroud)
您可以使用classes_分类器的属性来获取标签列表,它们是数组索引的。
classes_ :形状数组 = [n_classes] 或此类数组的列表
类标签(单输出问题)或类标签数组列表(多输出问题)。
| 归档时间: |
|
| 查看次数: |
8238 次 |
| 最近记录: |