0
点赞
收藏
分享

微信扫一扫

时间序列预测-女性出生数量预测


1 数据集构建

原始数据为:

时间序列预测-女性出生数量预测_深度学习


然后通过滑窗来构造多个X,如下图所示,第一列为是将原始值往后移6个时间步,其他列依次类推。

时间序列预测-女性出生数量预测_sed_02


我们去除空值之后,最后数据集为:

时间序列预测-女性出生数量预测_sed_03

这里的X就是前六列特征,最后一列为y是预测值

预测女性未来出生数量
每日女性出生数据集,即三年内的每月出生数。

下载链接:https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.csv

完整代码

from numpy import asarray
from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from sklearn.metrics import mean_absolute_error
from xgboost import XGBRegressor


# transform a time series dataset into a supervised learning dataset
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols = list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
    # put it all together
    agg = concat(cols, axis=1)
    # drop rows with NaN values

    if dropnan:
        agg.dropna(inplace=True)
        return agg.values


series = read_csv('data/daily-total-female-births.csv', header=0, index_col=0)
values = series.values
data = series_to_supervised(values, n_in=6)


def train_test_split(data, n_test):
    return data[:-n_test, :], data[-n_test:, :]


def xgboost_forecast(train, testX):
    # transform list into array
    train = asarray(train)
    # split into input and output columns
    trainX, trainy = train[:, :-1], train[:, -1]
    # fit model
    model = XGBRegressor(objective='reg:squarederror', n_estimators=1000)
    model.fit(trainX, trainy)
    # make a one-step prediction
    yhat = model.predict(asarray([testX]))
    return yhat[0]


# walk-forward validation for univariate data
def walk_forward_validation(data, n_test):
    predictions = list()
    # split dataset
    train, test = train_test_split(data, n_test)
    # seed history with training dataset
    history = [x for x in train]
    # step over each time-step in the test set
    for i in range(len(test)):
        # split test row into input and output columns
        testX, testy = test[i, :-1], test[i, -1]
        # fit model on history and make a prediction
        yhat = xgboost_forecast(history, testX)
        # store forecast in list of predictions
        predictions.append(yhat)
        # add actual observation to history for the next loop
        history.append(test[i])
        # summarize progress
        print('>expected=%.1f, predicted=%.1f' % (testy, yhat))
    # estimate prediction error
    error = mean_absolute_error(test[:, -1], predictions)
    return error, test[:, -1], predictions


# %%

# transform the time series data into supervised learning
data = series_to_supervised(values, n_in=6)
# evaluate
mae, y, yhat = walk_forward_validation(data, 12)

时间序列预测-女性出生数量预测_数据集_04


举报

相关推荐

0 条评论