问题描述
在这个简化的例子中,我用 GridSearchCV 训练了一个学习者.我想在对全集 X 进行预测时返回最佳学习器的混淆矩阵.
In this simplified example, I've trained a learner with GridSearchCV. I would like to return the confusion matrix of the best learner when predicting on the full set X.
lr_pipeline = Pipeline([('clf', LogisticRegression())])
lr_parameters = {}
lr_gs = GridSearchCV(lr_pipeline, lr_parameters, n_jobs=-1)
lr_gs = lr_gs.fit(X,y)
print lr_gs.confusion_matrix # Would like to be able to do this
谢谢
推荐答案
您首先需要在 GridSerarchCV
中使用最佳估计器进行预测.一个常用的方法是 GridSearchCV.decision_function()
,但是对于你的例子,decision_function
从 LogisticRegression
返回类概率并且不适用于 confusion_matrix.相反,使用 lr_gs
找到最佳估计器并使用该估计器预测标签.
You will first need to predict using best estimator in your GridSerarchCV
. A common method to use is GridSearchCV.decision_function()
, But for your example, decision_function
returns class probabilities from LogisticRegression
and does not work with confusion_matrix
. Instead, find best estimator using lr_gs
and predict the labels using that estimator.
y_pred = lr_gs.best_estimator_.predict(X)
最后,在真实和预测的y
from sklearn.metrics import confusion_matrix
print confusion_matrix(y, y_pred)
这篇关于Sci-kit:使用 GridSearchCV 时获得估计器混淆矩阵的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!