Loading

2-时间序列转换成稳定数据

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 difference(dataset, interval=1):
    diff = []
    for i in range(interval, len(dataset)):
        value = dataset[i] - dataset[i-interval]
        diff.append(value)
    return pd.Series(diff)

# 差分逆变换
def inverse_difference(history, yhat, interval=1):
    return yhat + history[-interval]

differenced = difference(ser, 1)
print(differenced.head())

inverted = []
for i in range(len(differenced)):
    value = inverse_difference(ser, differenced[i], len(ser)-i)
    inverted.append(value)
inverted = pd.Series(inverted)
print(inverted.head())
posted @ 2023-02-08 23:48  lotuslaw  阅读(33)  评论(0编辑  收藏  举报