【发布时间】:2015-12-11 21:22:15
【问题描述】:
我发现使用相同数据的两种交叉验证技术之间的分类性能存在差异。我想知道是否有人可以对此有所了解。
- 方法一:cross_validation.train_test_split
- 方法 2:分层 KFold。
具有相同数据集的两个示例
数据集 5500[n_samples :: Class 1 = 500 ; Class 0 = 5000 ] by 193 个特征
方法 1 [使用 train_test_split 进行随机迭代]
for i in range(0,5):
X_tr, X_te, y_tr, y_te = cross_validation.train_test_split(X_train.values, y_train, test_size=0.2, random_state=i)
clf = RandomForestClassifier(n_estimators=250, max_depth=None, min_samples_split=1, random_state=0, oob_score=True)
y_score = clf.fit(X_tr, y_tr).predict(X_te)
y_prob = clf.fit(X_tr, y_tr).predict_proba(X_te)
cm = confusion_matrix(y_te, y_score)
print cm
fpr, tpr, thresholds = roc_curve(y_te,y_prob[:,1])
roc_auc = auc(fpr, tpr);
print "ROC AUC: ", roc_auc
方法一的结果
Iteration 1 ROC AUC: 0.91
[[998 4]
[ 42 56]]
Iteration 5 ROC AUC: 0.88
[[1000 3]
[ 35 62]]
方法 2 [StratifiedKFold 交叉验证]
cv = StratifiedKFold(y_train, n_folds=5,random_state=None,shuffle=False)
clf = RandomForestClassifier(n_estimators=250, max_depth=None, min_samples_split=1, random_state=None, oob_score=True)
for train, test in cv:
y_score = clf.fit(X_train.values[train], y_train[train]).predict(X_train.values[test])
y_prob = clf.fit(X_train.values[train], y_train[train]).predict_proba(X_train.values[test])
cm = confusion_matrix(y_train[test], y_score)
print cm
fpr, tpr, thresholds = roc_curve(y_train[test],y_prob[:,1])
roc_auc = auc(fpr, tpr);
print "ROC AUC: ", roc_auc
方法2的结果
Fold 1 ROC AUC: 0.76
Fold 1 Confusion Matrix
[[995 5]
[ 92 8]]
Fold 5 ROC AUC: 0.77
Fold 5 Confusion Matrix
[[986 14]
[ 76 23]]
【问题讨论】:
标签: scikit-learn random-forest cross-validation