簡體   English   中英

Python Scikit-調用sklearn.metrics.precision_recall_curve時輸入形狀錯誤

[英]Python Scikit - bad input shape when calling sklearn.metrics.precision_recall_curve

我正在嘗試為CatBoostClassifier構建PRC(精確調用曲線)。

但是當我調用sklearn.metrics.precision_recall_curve(y_test, y_score)我得到ValueError: bad input shape (11912, 2)

我目前的方法有什么問題? 我需要在此處修復什么以提供正確的形狀?

import sklearn 
from sklearn import metrics 
y_score = model.predict_proba(X_test) 
prc_auc = sklearn.metrics.precision_recall_curve(y_test, y_score)

//這是我建立模型的方式

model = CatBoostClassifier( 
iterations=50, 
random_seed=63, 
learning_rate=0.15, 
custom_loss=['Accuracy', 'Precision', 'Recall', 'AUC']
) 

model.fit( 
X_train, y_train, 
cat_features=cat_features, 
eval_set=(X_test, y_test), 
verbose=10, 
plot=True 
);   

最簡單的答案是CatBoostClassifier.model.predict_proba返回2d數組。 sklearn.model.precision_recall_curve需要一個1d數組(或帶有一列的2d數組)。

CatBoostClassifier的文檔說, predict_proba()返回numpy.array ,並且不提供有關此方法的其他信息。 因此,我現在討厭此軟件包的文檔。

通過一些評論欠佳的代碼,我可以:

    if prediction_type == 'Probability':
        predictions = np.transpose([1 - predictions, predictions])
        return predictions

我猜第0列是類別0的概率,而第1列是類別1的概率。因此,請選擇測試與之匹配的任何事物,並僅使用該列。

prc_auc = sklearn.metrics.precision_recall_curve(y_test, y_score[:, 1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM