将 GridSearchCV 与 TimeSeriesSplit 结合使用



我有一些代码可以使用TimeSeriesSplit来拆分我的数据。对于每个拆分,我将使用ParametersGrid并遍历每个参数组合,记录最佳参数集并使用它来预测我的X_test。您可以在帖子底部看到此部分的代码

我知道GridSearchCV会为我做很多工作。我想知道我是否使用以下代码,我的数据被拆分为哪里 X_trainX_testy_trainy_test?使用带有TimeSeriesSplit GridSearchCV是否会在幕后处理这个问题,如果是这样,这段代码会完成与我在这篇文章底部的原始代码相同的事情吗?另外,我现在已经尝试了GridSearchCV方法,已经快 30 分钟没有完成 - 我有正确的语法吗?

X = data.iloc[:, 0:8]
y = data.iloc[:, 8:9]
parameters = [
    {'kernel': ['rbf'],
     'gamma': [.01],
     'C': [1, 10, 100]}]
gsc = GridSearchCV(SVR(), param_grid=parameters, scoring='neg_mean_absolute_error', 
                   cv=TimeSeriesSplit(n_splits=2))
gsc.fit(X,y)
means = gsc.cv_results_['mean_test_score']
for mean in means:
    print(mean)
print('end')

原始代码如下:

# Create the time series split generator
tscv = TimeSeriesSplit(n_splits=3)
for train_index, test_index in tqdm(tscv.split(X)):
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
# scale the data set
scaler_X = StandardScaler()
scaler_y = StandardScaler()
scaler_X.fit(X_train)
scaler_y.fit(y_train)
X_train, X_test = scaler_X.transform(X_train), scaler_X.transform(X_test)
y_train, y_test = scaler_y.transform(y_train), scaler_y.transform(y_test)

# optimization area - set params
parameters = [
    {'kernel': ['rbf'],
     'gamma': [.01],
     'C': [ 1,10,100,500,1000]}]

regressor = SVR()
# loop through each of the parameters and find the best set
for e, g in enumerate(ParameterGrid(parameters)):
    regressor.set_params(**g)
    regressor.fit(X_train, y_train.ravel())
    score = metrics.mean_absolute_error(regressor.predict(X_train), y_train.ravel())
    if e == 0:
        best_score = score
        best_params = g
    elif score < best_score:
        best_score = score
        best_params = g

# refit the model with the best set of params
regressor.set_params(**best_params)
regressor.fit(X_train, y_train.ravel())

您需要稍微修改代码。

gsc = GridSearchCV(SVR(), param_grid=parameters, scoring='neg_mean_absolute_error', 
                   cv=TimeSeriesSplit(n_splits=2).split(X))

而且,您可以考虑添加verbose参数以查看正在运行的输出。

最新更新