我正在尝试为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
返回一个二维数组;而sklearn.model.precision_recall_curve
需要一个一维数组(或一个只有一列的二维数组)。
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])