2017-08-25 76 views
6

我已經使用sklearn執行了GaussianNB分類。我嘗試使用以下代碼來計算所述度量:多類分類的sklearn指標

  print accuracy_score(y_test, y_pred) 
     print precision_score(y_test, y_pred) 

精度分數是工作正常,但精度分數計算被示出作爲錯誤:

ValueError: Target is multiclass but average='binary'. Please choose another average setting.

作爲目標是多類,可以我有指標分數精確度,召回率等?

回答

10

函數調用precision_score(y_test, y_pred)相當於precision_score(y_test, y_pred, pos_label=1, average='binary')。 文檔(http://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html)告訴我們:

'binary':

Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.

所以問題是,你的標籤不是二元的,但可能是一個熱編碼。幸運的是,還有其他應該與您的數據的工作選擇:

precision_score(y_test, y_pred, average=None)將返回每個等級的精度分數,而

precision_score(y_test, y_pred, average='micro')將返回總比TP /(TP + FP)

如果您選擇另一average選項而不是binary,則pos_label參數將被忽略。