我正在使用 Scikit-Learn 来解决这个分类问题。该数据集有 3 个特征和 600 个带标签的数据点。
首先我使用Nearest Neighbor了分类器。我没有使用交叉验证,而是手动运行拟合 5 次,每次都将数据集 (80-20) 重新拆分为训练集和测试集。结果平均分是0.61
clf = KNeighborsClassifier(4)
score = 0
for i in range(5):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf.fit(X_train, y_train)
score += clf.score(X_test, y_test)
print(scores / 5.0)
然而,当我进行交叉验证时,平均得分仅为0.45.
clf = KNeighborsClassifier(4)
scores = cross_val_score(clf, X, y, cv=5)
scores.mean()
为什么交叉验证产生的分数明显低于手动重采样?
我也试过Random Forest分类器。这次使用Grid Search来调整参数:
param_grid = {
'bootstrap': [True],
'max_depth': [8, 10, 12],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [8, 10, 12],
'n_estimators': [62, 64, 66, 68, 70]
}
clf_ = RandomForestClassifier()
grid_search = GridSearchCV(estimator = clf, param_grid = param_grid,
cv = 5, n_jobs = -1, verbose = 2)
grid_search.fit(X, y)
grid_search.best_params_, grid_search.best_score_
最好的分数原来是0.508使用以下参数
({'bootstrap': True,
'max_depth': 10,
'min_samples_leaf': 4,
'min_samples_split': 10,
'n_estimators': 64},
0.5081967213114754)
我继续对整个600个数据点进行预测,准确度很高0.7688。
best_grid = grid_search.best_estimator_
y_pred = best_grid.predict(X)
accuracy_score(y, y_pred)
我知道.best_score_是“best_estimator 的平均交叉验证分数”。但我不明白为什么它看起来比整个集合的预测准确度低得多。