Sci-kit: What's the easiest way to get the con

2020-07-06 08:37发布

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

Thanks

1条回答
【Aperson】
2楼-- · 2020-07-06 09:34

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)

Finally, use sklearn's confusion_matrix on real and predicted y

from sklearn.metrics import confusion_matrix
print confusion_matrix(y, y_pred)
查看更多
登录 后发表回答