Loading

3-时间序列转监督学习数据

import pandas as pd
import datetime

# 加载数据
def parser(x):
    return datetime.datetime.strptime(x, '%Y/%m/%d')

ser = pd.read_csv('../LSTM系列/LSTM单变量1/data_set/shampoo-sales.csv', 
                header=0, parse_dates=[0], index_col=0, date_parser=parser).squeeze('columns')

def timeseries_to_supervised(data, lag=1):
    df = pd.DataFrame(data)
    columns = [df.shift(i) for i in range(1, lag+1)]
    columns.append(df)
    df = pd.concat(columns, axis=1)
    df.fillna(0, inplace=True)
    return df

X = ser.values
supervised = timeseries_to_supervised(X, 1)
print(supervised.head())
posted @ 2023-02-08 23:48  lotuslaw  阅读(17)  评论(0编辑  收藏  举报