# 【时间序列】如何用差分让时间序列变得平稳-Python实践

Python机器学习基础教程 专栏收录该内容
16 篇文章 4 订阅

# 1 概述

• 平稳和不平稳时间序列之间的对比，以及如何使用差分变换使序列平稳。
• 如何应用差分变换从序列中删除线性趋势。
• 如何应用差异变换从序列中删除季节性信号。

# 4 差分转换

difference(t) = observation(t) - observation(t-1)


inverted(t) = differenced(t) + observation(t-1)


# create a differenced series
def difference(dataset, interval=1):
diff = list()
for i in range(interval, len(dataset)):
value = dataset[i] - dataset[i - interval]
diff.append(value)
return diff

# invert differenced forecast
def inverse_difference(last_ob, value):
return value + last_ob


## 4.1 线性增长的趋势

# define a dataset with a linear trend
data = [i+1 for i in range(20)]
print(data)

# difference the dataset
diff = difference(data)
print(diff)

# invert the difference
inverted = [inverse_difference(data[i], diff[i]) for i in range(len(diff))]
print(inverted)

plt.plot(data)
plt.show()

plt.plot(diff)
plt.show()

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]


## 4.2 季节性趋势

# difference the dataset
diff = difference(data, 360)
plt.plot(diff)
plt.show()


• 4
点赞
• 0
评论
• 15
收藏
• 一键三连
• 扫一扫，分享海报

04-06 1823
03-07
06-30 8650
12-04 6万+
04-23 1万+
05-04 1226
05-11 1万+
11-17 2363
07-11 3万+
06-01 5261
07-04 2150
04-28 3万+
03-16 8569