I am using grid search to find the optimal parameters for 2 models. I have to build one model with entire dataset and another model with reduced dataset (required to keep the folds same for both the models). So in case of second model, a list of datapoints will be omitted/ removed from the same fold which has been used for first model (one with entire dataset). Following is my code:
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)
For second GridSearch (line #19), I am getting error:
for train, test in self.cv:
> TypeError: cannot unpack non-iterable numpy.int32 object
What am I doing wrong here with cv=rkf_new_indices and how can I fix this?