时间序列——GridSearchCV&TimeSeriesSplit的调参(xgboost预测sin(x))
Posted hellobigorange
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了时间序列——GridSearchCV&TimeSeriesSplit的调参(xgboost预测sin(x))相关的知识,希望对你有一定的参考价值。
1、 原始时间序列
data = np.sin(np.arange(0, 1000) * 0.05) * 10 + np.random.randn(1000)
2、GridSearchCV&TimeSeriesSplit的调参
# -*- coding: utf-8 -*-
# @Time : 2021/12/21 13:42
# @Author : Orange
# @File : test.py.py
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV, TimeSeriesSplit
import numpy as np
import matplotlib.pyplot as plt
# 以xgboost为例,给出时间序列的GridSearchCV结合TimeSeriesSplit的调参
def train(x_train, y_train):
my_cv = TimeSeriesSplit(n_splits=4).split(x_train)
cv_params = 'n_estimators': [6, 7, 8, 10, 20], 'learning_rate': [0.01, 0.1, 0.3, 1], 'max_depth': [4, 5, 6, 7, 8],
'min_child_weight': [4, 5, 6, 7, 8], 'gamma': [1, 3], 'reg_alpha': [0.1, 0.3]
other_params = 'learning_rate': 0.1, 'n_estimators': 90, 'max_depth': 7, 'min_child_weight': 4, 'seed': 0,
'subsample': 1, 'colsample_bytree': 0.9, 'gamma': 1, 'reg_alpha': 0.1, "lambda": 0.9
model = XGBRegressor(**other_params)
optimized_GBM = GridSearchCV(estimator=model, param_grid=cv_params, scoring='neg_mean_absolute_error', cv=my_cv)
optimized_GBM.fit(np.array(x_train), np.array(y_train))
model = optimized_GBM.best_estimator_
print('参数的最佳取值:0'.format(optimized_GBM.best_params_))
print('最佳模型得分:0'.format(optimized_GBM.best_score_))
return model
if __name__ == '__main__':
data = np.sin(np.arange(0, 1000) * 0.05) * 10 + np.random.randn(1000)
# 取当前时间为第500个数据所在的时刻
# 特征取前500个数据,利用前500个数据预测后500个数据
X = data[:500].reshape(500, 1)
Y = data[500:]
X_train = X[:400]
Y_train = Y[:400]
X_test = X[400:]
Y_test = Y[400:]
model = train(X_train, Y_train)
Y_hat = model.predict(X_test)
plt.figure()
plt.plot(Y_test)
plt.plot(Y_hat)
plt.legend(['Y_test', 'Y_hat'])
plt.show()
输出:参数的最佳取值:‘gamma’: 1, ‘learning_rate’: 0.3, ‘max_depth’: 5, ‘min_child_weight’: 4, ‘n_estimators’: 20, ‘reg_alpha’: 0.3
以上是关于时间序列——GridSearchCV&TimeSeriesSplit的调参(xgboost预测sin(x))的主要内容,如果未能解决你的问题,请参考以下文章
【zt】LogisticRegression 调参 & GridSearchCV
使用 SGD 分类器和 GridsearchCV 寻找***特征