<p>Time-series data changes its waveform over time, but it may increase or decrease over time. Such gradual, non-periodic changes are sometimes referred to as trends.
Data with a trend changes the mean, variance, and other statistics of the data over time, and as a result is more difficult to predict.
On this page, we will try to remove the trend component from time series data using python.</p>
df["曜日"]=df["日付"].dt.weekdaydf["年初からの日数%14"]=df["日付"].dt.dayofyear%14df["年初からの日数%28"]=df["日付"].dt.dayofyear%28defget_trend(timeseries,deg=3,trainN=0):"""Create a trend line for time-series data
Args:
timeseries(pd.Series) : Time series data
deg(int) : Degree of polynomial
trainN(int): Number of data used to estimate the coefficients of the polynomial
Returns:
pd.Series: Time series data corresponding to trends
"""iftrainN==0:trainN=len(timeseries)x=list(range(len(timeseries)))y=timeseries.valuescoef=np.polyfit(x[:trainN],y[:trainN],deg)trend=np.poly1d(coef)(x)returnpd.Series(data=trend,index=timeseries.index)trainN=500df["Trend"]=get_trend(df["y"],trainN=trainN,deg=2)plt.figure(figsize=(10,5))sns.lineplot(x=df["日付"],y=df["y"])sns.lineplot(x=df["日付"],y=df["Trend"])
XGBoost does not know that data changes slowly between training and test data.
Therefore, the more you predict the future, the more your predictions will be off down the road.
For XGBoost to forecast well, the y distribution of the training and test data must be close.
We first remove the portion corresponding to the trend from the observed values and then predict the values without the trend.
The XGBoost prediction is then added to the XGBoost prediction to obtain the final prediction.