<jupyter_start><jupyter_text>Import Libraries<jupyter_code>from numpy import asarray
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn.metrics import mean_absolute_error
from sklearn import datasets, ensemble
from matplotlib import pyplot<jupyter_output><empty_output><jupyter_text>Transform a time series dataset into a supervised learning datasetThis steps includes converting a naive time series data into a supervised dataset. The number of values to be used as lag for supervised dataset is sent as a parameter to the function.<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Split a univariate dataset into train/test sets<jupyter_code>def train_test_split(data, n_test):
    return data[:-n_test, :], data[-n_test:, :]<jupyter_output><empty_output><jupyter_text>Fit a gradient boost regressor and make a one step prediction<jupyter_code>def gradient_boost_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 = ensemble.GradientBoostingRegressor()
    model.fit(trainX, trainy)
    # make a one-step prediction
    yhat = model.predict([testX])
    return yhat[0]<jupyter_output><empty_output><jupyter_text>Walk-forward validation for univariate data<jupyter_code>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 = gradient_boost_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<jupyter_output><empty_output><jupyter_text>load the dataset<jupyter_code>series = read_csv(
    r'C:\Users\ahtis\OneDrive\Desktop\ARIMA\data\data.csv', header=0, index_col=0)
values = series.values
pyplot.plot(values, label='Original Glucose Data')<jupyter_output><empty_output><jupyter_text>Transform the time series data into supervised learning<jupyter_code>data = values[0:17280]
data = series_to_supervised(data, n_in=1)
data.shape<jupyter_output><empty_output><jupyter_text>Evaluate<jupyter_code>mae, y, yhat = walk_forward_validation(data, 72)
print('MAE: %.3f' % mae)<jupyter_output>>expected=84.0, predicted=82.2
>expected=85.0, predicted=84.9
>expected=86.0, predicted=85.0
>expected=86.0, predicted=86.6
>expected=82.0, predicted=86.6
>expected=82.0, predicted=82.2
>expected=88.0, predicted=82.2
>expected=91.0, predicted=89.1
>expected=96.0, predicted=91.6
>expected=96.0, predicted=96.3
>expected=102.0, predicted=96.3
>expected=105.0, predicted=101.4
>expected=111.0, predicted=105.3
>expected=113.0, predicted=111.1
>expected=111.0, predicted=112.8
>expected=110.0, predicted=111.1
>expected=111.0, predicted=109.5
>expected=110.0, predicted=111.1
>expected=114.0, predicted=109.5
>expected=116.0, predicted=114.2
>expected=112.0, predicted=116.0
>expected=110.0, predicted=111.6
>expected=104.0, predicted=109.5
>expected=102.0, predicted=104.3
>expected=101.0, predicted=101.4
>expected=99.0, predicted=101.4
>expected=99.0, predicted=99.3
>expected=99.0, predicted=99.3
>expected=99.0, predicted=99.3
>expected=99.0, predicted=99.3
>expected=102.0, predicted=99.3
>expecte[...]<jupyter_text>Plot expected vs predicted<jupyter_code>pyplot.plot(y, label='Expected')
pyplot.plot(yhat, label='Predicted')
pyplot.legend()
pyplot.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12281849912739964, 'fraction_numerical': 0.05410122164048865, 'mean_word_length': 4.87067861715749, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '285466', 'license': 'MIT', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1563, 'n_words': 380, 'path': 'TEST - Gradient Boost Regressor (XB Boost) .ipynb', 'repo_name': 'ahtshamzafar1/Time-Series-Data-Analysis', 'size': '4584'}