我想用不同的参数给不同的分类器打分。
为了加快 LogisticRegression
的速度,我使用 LogisticRegressionCV
(至少快 2 倍)并计划为其他人使用 GridSearchCV
。
但问题是它给了我相等的 C
参数,但不是 AUC ROC
评分。
我会尝试修复许多参数,例如 scorer
、random_state
、solver
、max_iter
、托尔
...
请看例子(真实数据无数据):
测试数据及公共(public)部分:
from sklearn import datasets
boston = datasets.load_boston()
X = boston.data
y = boston.target
y[y <= y.mean()] = 0; y[y > 0] = 1
import numpy as np
from sklearn.cross_validation import KFold
from sklearn.linear_model import LogisticRegression
from sklearn.grid_search import GridSearchCV
from sklearn.linear_model import LogisticRegressionCV
fold = KFold(len(y), n_folds=5, shuffle=True, random_state=777)
网格搜索CV
grid = {
'C': np.power(10.0, np.arange(-10, 10))
, 'solver': ['newton-cg']
}
clf = LogisticRegression(penalty='l2', random_state=777, max_iter=10000, tol=10)
gs = GridSearchCV(clf, grid, scoring='roc_auc', cv=fold)
gs.fit(X, y)
print ('gs.best_score_:', gs.best_score_)
gs.best_score_: 0.939162082194
逻辑回归CV
searchCV = LogisticRegressionCV(
Cs=list(np.power(10.0, np.arange(-10, 10)))
,penalty='l2'
,scoring='roc_auc'
,cv=fold
,random_state=777
,max_iter=10000
,fit_intercept=True
,solver='newton-cg'
,tol=10
)
searchCV.fit(X, y)
print ('Max auc_roc:', searchCV.scores_[1].max())
Max auc_roc: 0.970588235294
求解器 newton-cg
仅用于提供固定值,其他也尝试过。
我忘了什么?
附言在这两种情况下,我还收到警告“/usr/lib64/python3.4/site-packages/sklearn/utils/optimize.py:193: UserWarning: Line Search failed warnings.warn('Line Search failed')"我也听不懂。如果有人也描述它的意思,我会很高兴,但我希望它与我的主要问题无关。
编辑更新
通过@joeln 评论添加max_iter=10000
和tol=10
参数。它不会改变任何数字的结果,但警告消失了。
最佳答案
这是 answer by Tom 的副本在 scikit-learn 问题跟踪器上:
LogisticRegressionCV.scores_
给出所有折叠的分数。
GridSearchCV.best_score_
给出所有折叠的最佳平均分。
要获得相同的结果,您需要更改代码:
print('Max auc_roc:', searchCV.scores_[1].max()) # is wrong
print('Max auc_roc:', searchCV.scores_[1].mean(axis=0).max()) # is correct
同时使用默认的 tol=1e-4
而不是您的 tol=10
,我得到:
('gs.best_score_:', 0.939162082193857)
('Max auc_roc:', 0.93915947999923843)
剩余的(小)差异可能来自 LogisticRegressionCV
中的热启动(这实际上是它比 GridSearchCV
更快的原因)。
关于python - 如何从 LogisticRegressionCV 和 GridSearchCV 获得可比较和可重现的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36271166/