Python 使用sklearn对减少的交叉验证数据集执行网格搜索的有效方法

Python 使用sklearn对减少的交叉验证数据集执行网格搜索的有效方法,python,machine-learning,data-science,cross-validation,hyperparameters,Python,Machine Learning,Data Science,Cross Validation,Hyperparameters,我使用网格搜索来寻找两个模型的最佳参数。我必须用整个数据集构建一个模型,用缩减的数据集构建另一个模型(需要保持两个模型的折叠相同)。因此,对于第二个模型,数据点列表将从用于第一个模型的同一折叠中省略/删除(一个包含整个数据集)。以下是我的代码: rkf = RepeatedKFold(n_splits=2, n_repeats=5, random_state=24) rkf_new_indices = [] for train_idx, test_idx in rkf.split(x):

我使用网格搜索来寻找两个模型的最佳参数。我必须用整个数据集构建一个模型,用缩减的数据集构建另一个模型(需要保持两个模型的折叠相同)。因此,对于第二个模型,数据点列表将从用于第一个模型的同一折叠中省略/删除(一个包含整个数据集)。以下是我的代码:

rkf = RepeatedKFold(n_splits=2, n_repeats=5, random_state=24)
rkf_new_indices = []
for train_idx, test_idx in rkf.split(x):
    Model1x_train, Model1x_test = x[train_idx], x[test_idx]
    Model1y_train, Model1y_test = y[train_idx], y[test_idx]
    temp_list1 = train_idx.copy()
    temp_list2 = test_idx.copy()
    Model2trn_idx = remove_datapoints(temp_list1, out_list)
    Model2tst_idx = remove_datapoints(temp_list2, out_list)
    Model2train_idx = list(Model2trn_idx)
    Model2test_idx = list(Model2tst_idx)
    rkf_new_indices = np.append(Model2train_idx, Model2test_idx)
param_grid = [{'C': [1, 10, 100, 1000], 'kernel': ['linear']}, {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},]
svr_model = SVR()
# define search for model with entire dataset
BASE_SVR = GridSearchCV(svr_model, param_grid, scoring='neg_mean_absolute_error', n_jobs=-1, cv=rkf, return_train_score=True)
BASE_SVR_grid_results = BASE_SVR.fit(x, y)

# define search for model with reduced dataset
New_SVR = GridSearchCV(svr_model, param_grid, scoring='neg_mean_absolute_error', n_jobs=-1, cv=rkf_new_indices, return_train_score=True)
#         ^^^^^^^^^^^^ raises TypeError
New_SVR_grid_results = New_SVR.fit(x, y)
对于第二个GridSearch(第19行),我得到一个错误:

for train, test in self.cv:

> TypeError: cannot unpack non-iterable numpy.int32 object

我在这里用
cv=rkf\u new\u索引做错了什么?我如何解决这个问题?

当您在段下方运行时,拆分的输出是

rkf_new_indices = []
for train_idx, test_idx in rkf.split([8,8,8,8,8,8,8,8,8]):
    print(train_idx, test_idx)
    rkf_new_indices = np.append(train_idx, test_idx)

[0 1 2 3] [4 5 6 7 8]
[4 5 6 7 8] [0 1 2 3]
[2 3 4 7] [0 1 5 6 8]
[0 1 5 6 8] [2 3 4 7]
[1 3 7 8] [0 2 4 5 6]
[0 2 4 5 6] [1 3 7 8]
[1 4 7 8] [0 2 3 5 6]
[0 2 3 5 6] [1 4 7 8]
[1 2 6 7] [0 3 4 5 8]
[0 3 4 5 8] [1 2 6 7]
但是,
rkf\u new\u index=np.append(train\u idx,test\u idx)
仅获取最后一个实例:

array([0, 3, 4, 5, 8, 1, 2, 6, 7])
您可以尝试
rkf\u new\u index.append((train\u idx,test\u idx))
以成对方式获取所有索引:

[(array([0, 1, 2, 3]), array([4, 5, 6, 7, 8])),
 (array([4, 5, 6, 7, 8]), array([0, 1, 2, 3])),
 (array([2, 3, 4, 7]), array([0, 1, 5, 6, 8])),
 (array([0, 1, 5, 6, 8]), array([2, 3, 4, 7])),
 (array([1, 3, 7, 8]), array([0, 2, 4, 5, 6])),
 (array([0, 2, 4, 5, 6]), array([1, 3, 7, 8])),
 (array([1, 4, 7, 8]), array([0, 2, 3, 5, 6])),
 (array([0, 2, 3, 5, 6]), array([1, 4, 7, 8])),
 (array([1, 2, 6, 7]), array([0, 3, 4, 5, 8])),
 (array([0, 3, 4, 5, 8]), array([1, 2, 6, 7]))]

什么是删除数据点的实现?这是否回答了您的问题@SaedSayedAhmed remove_datapoints只是从列表1(数据集的整个索引)中删除了我不希望(数据点的索引存储在列表2中)用于新的_SVR模型的那些索引。此函数的定义为:def remove_datapoints(list1,list2):return set(list1.flat)-set(list2.flat)rkf_new_index.append(np.append(train_idx,test_idx)也不起作用。它产生错误:new_SVR=GridSearchCV(svr_模型,参数网格,评分='neg_mean_absolute_error',n_jobs=-1,cv=rkf_new_index.split,return_train_score=True)AttributeError:“list”对象没有属性“split”如果在没有
的情况下应用
cv=rkf\u new\u索引
。split
?会产生错误:对于train,在self中测试。cv:ValueError:要解压缩的值太多(预期为2个)也许cv正在寻找成对的数组。你能试试我编辑的答案吗?是的,我明白你的意思。我根据你的答案以及新的训练、测试索引编辑了它。它成功了。非常感谢你的帮助。