用于交叉验证模型的predict_proba [英] predict_proba for a cross-validated model
本文介绍了用于交叉验证模型的predict_proba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过交叉验证从Logistic回归模型预测概率.我知道您可以获取交叉验证分数,但是可以从predict_proba返回值而不是分数吗?
I would like to predict the probability from Logistic Regression model with cross-validation. I know you can get the cross-validation scores, but is it possible to return the values from predict_proba instead of the scores?
# imports
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import (StratifiedKFold, cross_val_score,
train_test_split)
from sklearn import datasets
# setup data
iris = datasets.load_iris()
X = iris.data
y = iris.target
# setup model
cv = StratifiedKFold(y, 10)
logreg = LogisticRegression()
# cross-validation scores
scores = cross_val_score(logreg, X, y, cv=cv)
# predict probabilities
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y)
logreg.fit(Xtrain, ytrain)
proba = logreg.predict_proba(Xtest)
推荐答案
现在已作为scikit-learn版本0.18的一部分实现.您可以将方法"字符串参数传递给cross_val_predict方法.文档位于此处.
This is now implemented as part of scikit-learn version 0.18. You can pass a 'method' string parameter to the cross_val_predict method. Documentation is here.
示例:
proba = cross_val_predict(logreg, X, y, cv=cv, method='predict_proba')
还要注意,这是新的sklearn.model_selection包的一部分,因此您将需要此导入:
Also note that this is part of the new sklearn.model_selection package so you will need this import:
from sklearn.model_selection import cross_val_predict
这篇关于用于交叉验证模型的predict_proba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文