我想通过交叉验证从Logistic回归模型预测概率。我知道您可以获取交叉验证分数,但是可以从predict_proba返回值而不是分数吗?

# 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的一部分实现。您可以将'method'字符串参数传递给cross_val_predict方法。文档为here

例子:

proba = cross_val_predict(logreg, X, y, cv=cv, method='predict_proba')

还要注意,这是新的sklearn.model_selection包的一部分,因此您将需要此导入:
from sklearn.model_selection import cross_val_predict

08-19 19:58