2016-02-05 42 views
5

我工作在二元分類模型,分類器是樸素貝葉斯。我有一個幾乎平衡的數據集,但是我得到的時候我預測以下錯誤信息:Scikit學習錯誤消息'精度和F分數不明確,並被設置爲0.0在標籤'

UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. 
    'precision', 'predicted', average, warn_for) 

我使用的是CV k折10.測試集gridsearch和預測包含兩個類,所以不理解消息。我正在爲6個其他模型使用相同的數據集,訓練/測試拆分,cv和隨機種子,並且這些工作非常完美。數據被外部攝入到一個數據幀中,隨機化和種子是固定的。然後,樸素貝葉斯分類模型在此代碼片段之前對文件進行分類。

X_train, X_test, y_train, y_test, len_train, len_test = \ 
    train_test_split(data['X'], data['y'], data['len'], test_size=0.4) 
pipeline = Pipeline([ 
    ('classifier', MultinomialNB()) 
]) 

cv=StratifiedKFold(len_train, n_folds=10) 

len_train = len_train.reshape(-1,1) 
len_test = len_test.reshape(-1,1) 

params = [ 
    {'classifier__alpha': [0, 0.0001, 0.001, 0.01]} 

] 

grid = GridSearchCV(
    pipeline, 
    param_grid=params, 
    refit=True, 
    n_jobs=-1, 
    scoring='accuracy', 
    cv=cv, 
) 

nb_fit = grid.fit(len_train, y_train) 

preds = nb_fit.predict(len_test) 

print(confusion_matrix(y_test, preds, labels=['1','0'])) 
print(classification_report(y_test, preds)) 

我被'強迫'python改變系列的形狀,也許這是罪魁禍首?

+0

什麼版本的scikit學習你使用@OAK – Farseer

+0

@Farseer版本0.17?我讀過之前版本中存在的一個錯誤,不確定在這個版本中是否存在。 – OAK

+1

此警告意味着對於某些tp + fp爲零的樣本,精度和f1分數未定義,因此在計算該樣本的精度時會導致0/0。因爲f1分數是精度的函數,所以它也是未定義的,並且庫被設置爲0.0。 – aadel

回答

1

作爲aadel曾評論,當沒有數據點被歸類爲陽性,精度由零分割,因爲它被定義爲TP /(TP + FP)(即,真陽性/真和假陽性)。該庫然後將精度設置爲0,但發出警告,因爲實際上該值未定義。 F1取決於精度,因此也沒有定義。

一旦你意識到這一點,你可以選擇禁用警告:

import warnings 
import sklearn.exceptions 
warnings.filterwarnings("ignore", category=sklearn.exceptions.UndefinedMetricWarning)