[英]Labels size different from target_names: Tensorflow Multi-Input Regression converting to Classification
[英]classification_report: labels and target_names
我有以下分類報告的輸出:
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
數據集中的真實標簽是s
和p
。
問題:我怎么知道哪個標簽是“0”,哪個是“1”? 或者:如何以正確的順序通過labels=
或target_names=
分配標簽?
如無特別說明,將按字母順序排列。 所以很可能是:
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
所以不需要做任何事情。 但是,如果您更改了標簽,則可以將它們傳遞到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
如果您使用 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)
您可以使用分類器的classes_
屬性來獲取標簽列表,並且它們是數組索引的。
classes_ :形狀數組 = [n_classes] 或此類數組的列表
類標簽(單輸出問題),或類標簽數組列表(多輸出問題)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.