使用 Scikit-Learn GridSearchCV 与 PredefinedSplit 进行交叉验证 - 交叉验证结果令人怀疑
Using Scikit-Learn GridSearchCV for cross validation with PredefinedSplit - Suspiciously good cross validation results
我想使用 scikit-learn 的 GridSearchCV
执行网格搜索并使用预定义的开发和验证拆分(1 折交叉验证)计算交叉验证错误。
恐怕我做错了什么,因为我的验证准确率高得令人怀疑。我认为我出错的地方:我将我的训练数据分成开发和验证集,在开发集上进行培训并在验证集上记录交叉验证分数。我的准确性可能会被夸大,因为我实际上是在混合开发和验证集上进行训练,然后在验证集上进行测试。我不确定我是否正确使用了 scikit-learn 的 PredefinedSplit
模块。详情如下:
在 之后,我做了以下事情:
import numpy as np
from sklearn.model_selection import train_test_split, PredefinedSplit
from sklearn.grid_search import GridSearchCV
# I split up my data into training and test sets.
X_train, X_test, y_train, y_test = train_test_split(
data[training_features], data[training_response], test_size=0.2, random_state=550)
# sanity check - dimensions of training and test splits
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
# dimensions of X_train and x_test are (323430, 26) and (323430,1) respectively
# dimensions of X_test and y_test are (80858, 26) and (80858, 1)
''' Now, I define indices for a pre-defined split.
this is a 323430 dimensional array, where the indices for the development
set are set to -1, and the indices for the validation set are set to 0.'''
validation_idx = np.repeat(-1, y_train.shape)
np.random.seed(550)
validation_idx[np.random.choice(validation_idx.shape[0],
int(round(.2*validation_idx.shape[0])), replace = False)] = 0
# Now, create a list which contains a single tuple of two elements,
# which are arrays containing the indices for the development and
# validation sets, respectively.
validation_split = list(PredefinedSplit(validation_idx).split())
# sanity check
print(len(validation_split[0][0])) # outputs 258744
print(len(validation_split[0][0]))/float(validation_idx.shape[0])) # outputs .8
print(validation_idx.shape[0] == y_train.shape[0]) # True
print(set(validation_split[0][0]).intersection(set(validation_split[0][1]))) # set([])
现在,我 运行 使用 GridSearchCV
进行网格搜索。我的意图是,对于网格上的每个参数组合,模型将适合 在开发集 上,并且在应用结果估计器时将记录交叉验证分数 到验证集。
# a vanilla XGboost model
model1 = XGBClassifier()
# create a parameter grid for the number of trees and depth of trees
n_estimators = range(300, 1100, 100)
max_depth = [8, 10]
param_grid = dict(max_depth=max_depth, n_estimators=n_estimators)
# A grid search.
# NOTE: I'm passing a PredefinedSplit object as an argument to the `cv` parameter.
grid_search = GridSearchCV(model1, param_grid,
scoring='neg_log_loss',
n_jobs=-1,
cv=validation_split,
verbose=1)
现在,这是为我举起红旗的地方。我使用 gridsearch 找到的最佳估计器来查找验证集的准确性。非常高 - 0.89207865689639176
。更糟糕的是,它 几乎与我在数据开发集(我刚刚训练过的数据集)上使用分类器得到的准确度相同 - 0.89295597192591902
。 但是 - 当我在真实测试集上使用分类器时,我得到的准确率要低得多,大约 .78
:
# accurracy score on the validation set. This yields .89207865
accuracy_score(y_pred =
grid_result2.predict(X_train.iloc[validation_split[0][1]]),
y_true=y_train[validation_split[0][1]])
# accuracy score when applied to the development set. This yields .8929559
accuracy_score(y_pred =
grid_result2.predict(X_train.iloc[validation_split[0][0]]),
y_true=y_train[validation_split[0][0]])
# finally, the score when applied to the test set. This yields .783
accuracy_score(y_pred = grid_result2.predict(X_test), y_true = y_test)
对我来说,模型应用于开发和验证数据集时的准确性与应用于测试集时准确性的显着损失之间几乎完全一致,这清楚地表明我正在对验证数据进行训练偶然,因此我的交叉验证分数不能代表模型的真实准确性。
我似乎找不到哪里出错了 - 主要是因为我不知道 GridSearchCV
在收到一个 PredefinedSplit
对象作为参数时在幕后做了什么cv
参数。
知道我哪里出错了吗?如果您需要更多 details/elaboration,请告诉我。代码也在 this notebook on github.
谢谢!
您需要设置refit=False
(不是默认选项),否则网格搜索完成后会在整个数据集(忽略cv)上重新拟合估计器。
是的,验证数据存在数据泄漏问题。您需要为 GridSearchCV
设置 refit = False
,它不会重新拟合包括训练和验证数据在内的整个数据。
我想使用 scikit-learn 的 GridSearchCV
执行网格搜索并使用预定义的开发和验证拆分(1 折交叉验证)计算交叉验证错误。
恐怕我做错了什么,因为我的验证准确率高得令人怀疑。我认为我出错的地方:我将我的训练数据分成开发和验证集,在开发集上进行培训并在验证集上记录交叉验证分数。我的准确性可能会被夸大,因为我实际上是在混合开发和验证集上进行训练,然后在验证集上进行测试。我不确定我是否正确使用了 scikit-learn 的 PredefinedSplit
模块。详情如下:
在
import numpy as np
from sklearn.model_selection import train_test_split, PredefinedSplit
from sklearn.grid_search import GridSearchCV
# I split up my data into training and test sets.
X_train, X_test, y_train, y_test = train_test_split(
data[training_features], data[training_response], test_size=0.2, random_state=550)
# sanity check - dimensions of training and test splits
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
# dimensions of X_train and x_test are (323430, 26) and (323430,1) respectively
# dimensions of X_test and y_test are (80858, 26) and (80858, 1)
''' Now, I define indices for a pre-defined split.
this is a 323430 dimensional array, where the indices for the development
set are set to -1, and the indices for the validation set are set to 0.'''
validation_idx = np.repeat(-1, y_train.shape)
np.random.seed(550)
validation_idx[np.random.choice(validation_idx.shape[0],
int(round(.2*validation_idx.shape[0])), replace = False)] = 0
# Now, create a list which contains a single tuple of two elements,
# which are arrays containing the indices for the development and
# validation sets, respectively.
validation_split = list(PredefinedSplit(validation_idx).split())
# sanity check
print(len(validation_split[0][0])) # outputs 258744
print(len(validation_split[0][0]))/float(validation_idx.shape[0])) # outputs .8
print(validation_idx.shape[0] == y_train.shape[0]) # True
print(set(validation_split[0][0]).intersection(set(validation_split[0][1]))) # set([])
现在,我 运行 使用 GridSearchCV
进行网格搜索。我的意图是,对于网格上的每个参数组合,模型将适合 在开发集 上,并且在应用结果估计器时将记录交叉验证分数 到验证集。
# a vanilla XGboost model
model1 = XGBClassifier()
# create a parameter grid for the number of trees and depth of trees
n_estimators = range(300, 1100, 100)
max_depth = [8, 10]
param_grid = dict(max_depth=max_depth, n_estimators=n_estimators)
# A grid search.
# NOTE: I'm passing a PredefinedSplit object as an argument to the `cv` parameter.
grid_search = GridSearchCV(model1, param_grid,
scoring='neg_log_loss',
n_jobs=-1,
cv=validation_split,
verbose=1)
现在,这是为我举起红旗的地方。我使用 gridsearch 找到的最佳估计器来查找验证集的准确性。非常高 - 0.89207865689639176
。更糟糕的是,它 几乎与我在数据开发集(我刚刚训练过的数据集)上使用分类器得到的准确度相同 - 0.89295597192591902
。 但是 - 当我在真实测试集上使用分类器时,我得到的准确率要低得多,大约 .78
:
# accurracy score on the validation set. This yields .89207865
accuracy_score(y_pred =
grid_result2.predict(X_train.iloc[validation_split[0][1]]),
y_true=y_train[validation_split[0][1]])
# accuracy score when applied to the development set. This yields .8929559
accuracy_score(y_pred =
grid_result2.predict(X_train.iloc[validation_split[0][0]]),
y_true=y_train[validation_split[0][0]])
# finally, the score when applied to the test set. This yields .783
accuracy_score(y_pred = grid_result2.predict(X_test), y_true = y_test)
对我来说,模型应用于开发和验证数据集时的准确性与应用于测试集时准确性的显着损失之间几乎完全一致,这清楚地表明我正在对验证数据进行训练偶然,因此我的交叉验证分数不能代表模型的真实准确性。
我似乎找不到哪里出错了 - 主要是因为我不知道 GridSearchCV
在收到一个 PredefinedSplit
对象作为参数时在幕后做了什么cv
参数。
知道我哪里出错了吗?如果您需要更多 details/elaboration,请告诉我。代码也在 this notebook on github.
谢谢!
您需要设置refit=False
(不是默认选项),否则网格搜索完成后会在整个数据集(忽略cv)上重新拟合估计器。
是的,验证数据存在数据泄漏问题。您需要为 GridSearchCV
设置 refit = False
,它不会重新拟合包括训练和验证数据在内的整个数据。