首页 文章

评估scikit-learn GridSearchCV中交叉验证分数的均值,stddev

提问于
浏览
2

我正在使用Python 2.7和scikit-learn来做一些机器学习 . 我正在使用gridsearch来确定我的数据集和随机森林分类器的最佳超参数 . 我使用留一法交叉验证和ROC曲线下的面积作为评估每组超参数的度量标准 . 我的代码运行,但我对clf.grid_scores_的输出感到有点困惑 . 根据我的理解,应该在所有数据折叠中评估每组超参数,以查看使用在所有其他折叠上训练的模型预测剩余折叠的程度 . 这将为您提供每个折叠的AUROC . 然后,Gridsearch应报告每组超参数的所有折叠的均值和标准差 . 使用.grid_scores_,我们可以查看每组超参数的auroc的mean,stddev和raw值 .

My question is why the reported mean and stddev of the cross validation scores are not equivalent to actually taking the .mean() and .std() of the reported auroc values across all the folds?

The Code:

from sklearn import cross_validation, grid_search
from sklearn.ensemble import RandomForestClassifier

lol = cross_validation.LeaveOneLabelOut(group_labels)
rf = RandomForestClassifier(random_state=42, n_jobs=96)

parameters = {'min_samples_leaf':[500,1000],
              'n_estimators': [100],
              'criterion': ['entropy',],
              'max_features': ['sqrt']
             }

clf = grid_search.GridSearchCV(rf, parameters, scoring='roc_auc', cv=lol)
clf.fit(train_features, train_labels)

for params, mean_score, scores in clf.grid_scores_:
    print("%0.3f (+/-%0.3f) for %r" % (scores.mean(), scores.std(), params))
print

for g in clf.grid_scores_: print g
print

print clf.best_score_
print clf.best_estimator_

The Output:

0.603 (+/-0.108) for {'max_features': 'sqrt', 'n_estimators': 100, 'criterion': 'entropy', 'min_samples_leaf': 500}
0.601 (+/-0.108) for {'max_features': 'sqrt', 'n_estimators': 100, 'criterion': 'entropy', 'min_samples_leaf': 1000}

mean: 0.60004, std: 0.10774, params: {'max_features': 'sqrt', 'n_estimators': 100, 'criterion': 'entropy', 'min_samples_leaf': 500}
mean: 0.59705, std: 0.10821, params: {'max_features': 'sqrt', 'n_estimators': 100, 'criterion': 'entropy', 'min_samples_leaf': 1000}

0.600042993354
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='entropy',
            max_depth=None, max_features='sqrt', max_leaf_nodes=None,
            min_samples_leaf=500, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=96,
            oob_score=False, random_state=42, verbose=0, warm_start=False)

为什么我将第一个分类器的平均值计算为0.603并将gridsearch报告为0.60004? (以及对第二种意思的类似分歧?)我觉得要么我错过了一些重要的东西,这些东西可以帮助我找到最好的超文本集,或者sklearn中有一个错误 .

1 回答

  • 3

    起初我也很困惑所以我看了source code . 这两行将阐明如何计算交叉验证错误:

    this_score *= this_n_test_samples 
    n_test_samples += this_n_test_samples
    

    当网格搜索计算平均值时,它是加权平均值 . 您的 LeaveOneLabelOut CV很可能不 balancer ,即每个标签的样本数量不同 . 要计算平均验证分数,您需要将每个分数乘以折叠所包含的总样本的比例,然后将所有分数相加 .

相关问题