<jupyter_start><jupyter_text>Time series to supervisedBefore machine learning can be used, time series forecasting problems must be re-framed as supervised learning problems. From a sequence to pairs of input and output sequences as described at https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/<jupyter_code>import boto3
import io
from sagemaker import get_execution_role

role = get_execution_role()
bucket ='sagemaker-data-energy-demand'
import numpy as np 
import pandas as pd 
from datetime import datetime, timedelta

import warnings
warnings.filterwarnings('ignore')
def series_to_supervised(data,  col_names, n_in=1, n_out=1, dropnan=True):
    """
    Frame a time series as a supervised learning dataset.
    Arguments:
     data: Sequence of observations as a list or NumPy array.
     n_in: Number of lag observations as input (X).
     n_out: Number of observations as output (y).
     dropnan: Boolean whether or not to drop rows with NaN values.
    Returns:
     Pandas DataFrame of series framed for supervised learning.
    """
    n_vars = 1 if type(data) is list else data.shape[1]
    df = pd.DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('%s(t-%d)' % (col_names[j], i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
    if i == 0:
        names += [('%s(t)' % (col_names[j])) for j in range(n_vars)]
    else:
        names += [('%s(t+%d)' % (col_names[j], i)) for j in range(n_vars)]
    # put it all together
    agg = pd.concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
        agg.dropna(inplace=True)
    return agg
CITY = 'LosAngeles'
df_file = 'dataframes/%s_selectedfeatures.csv' % CITY

df_location = 's3://{}/{}'.format(bucket, df_file)
df = pd.read_csv(df_location, index_col ='datetime')
len(list(df.columns))
cols = list(df.columns)
cols.remove('demand')
cols.insert(0,'demand')
df = df[cols]

values = df.values
# ensure all data is float
values = values.astype('float32')
# frame as supervised learning
reframed = series_to_supervised(values, df.columns, 1, 1)
#reframed.drop(reframed.columns[[0,15,16,17,18,19,20,21,22,23,24,26,27]], axis=1, inplace=True)
reframed.drop(reframed.columns[[15,16,17,18,19,20,21,22,23,24,26,27,28,29,30,31,32,33,34,35,36,37,38]], axis=1, inplace=True)
reframed.columns
reframed = reframed.drop('demand(t-1)', axis=1)
cols = list(reframed.columns)
cols.remove('demand(t)')
cols.insert(0,'demand(t)')
reframed = reframed[cols]
reframed.index = df.index[1:]
reframed.head()
# save as csv file to continue in another notebook
s3_resource = boto3.resource('s3')

reframed_buffer = io.StringIO()
reframed_key = 'dataframes/%s_reframed.csv' % CITY
reframed.to_csv(reframed_buffer, compression=None)

s3_resource.Object(bucket, reframed_key).put(Body=reframed_buffer.getvalue())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13688337144723947, 'fraction_numerical': 0.03234237177393009, 'mean_word_length': 4.448398576512456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '328102', 'license': 'Apache-2.0', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1070, 'n_words': 280, 'path': '3_TS2SUPERVISED.ipynb', 'repo_name': 'olavocarvlho/sagemaker-energy-demand', 'size': '3061'}