2017-04-04 95 views
1

有没有什么办法来计算f1_score作为字符串的列表,而不管它们的顺序?scikit学习f1_score的字符串列表

f1_score(['a','b','c'],['a','c','b'],average='macro') 

我想这回不是0.33333333333

我知道我可以向量化的标签1,但这种语法会容易得多,在我的情况,因为我处理许多标签

回答

2

什么您需要的是多标记分类任务的f1_score,并且需要形状为[n_samples, n_labels]y_truey_pred的2-d矩阵。您正在提供一个1-d数组。因此它将被视为多类问题,而不是多标签。

official documentation提供了必要的细节。

而对于将要正确打进你需要y_truey_pred标记,指示矩阵转换为documented here

y_true:一维数组状,或标签指示阵列/稀疏矩阵

y_pred:1D阵列状,或标签指示器阵列/稀疏矩阵

所以,你需要改变这样的代码:

from sklearn.preprocessing import MultiLabelBinarizer 
from sklearn.metrics import f1_score 

y_true = [['a','b','c']] 
y_pred = [['a','c','b']] 

binarizer = MultiLabelBinarizer() 

# This should be your original approach 
#binarizer.fit(your actual true output consisting of all labels) 

# In this case, I am considering only the given labels. 
binarizer.fit(y_true) 

f1_score(binarizer.transform(y_true), 
     binarizer.transform(y_pred), 
     average='macro') 

Output: 1.0 

你可以看看MultilabelBinarizer的例子在这里: