二次移动平均法预测交通量实例及代码
时间: 2023-06-25 19:06:11 浏览: 27
二次移动平均法是时间序列预测中常用的一种方法,下面是一个简单的交通量预测实例及代码:
假设我们有一段时间内的交通量数据如下:
| 时间 | 交通量 |
| ---- | ------ |
| 1 | 10 |
| 2 | 12 |
| 3 | 15 |
| 4 | 18 |
| 5 | 20 |
| 6 | 22 |
| 7 | 24 |
| 8 | 26 |
| 9 | 28 |
| 10 | 30 |
首先,我们需要进行一次移动平均处理,计算出每个时间点的平均值。这里我们使用窗口大小为3,即对于每个时间点,取其前后两个时间点的交通量进行平均。
| 时间 | 交通量 | 一次移动平均 |
| ---- | ------ | ------------ |
| 1 | 10 | |
| 2 | 12 | 11 |
| 3 | 15 | 13 |
| 4 | 18 | 15 |
| 5 | 20 | 18 |
| 6 | 22 | 20 |
| 7 | 24 | 22 |
| 8 | 26 | 24 |
| 9 | 28 | 26 |
| 10 | 30 | 28 |
然后,我们再对一次移动平均的结果进行一次移动平均处理,得到二次移动平均。这里我们仍使用窗口大小为3。
| 时间 | 交通量 | 一次移动平均 | 二次移动平均 |
| ---- | ------ | ------------ | ------------ |
| 1 | 10 | | |
| 2 | 12 | 11 | |
| 3 | 15 | 13 | 12.33 |
| 4 | 18 | 15 | 14.66 |
| 5 | 20 | 18 | 17.66 |
| 6 | 22 | 20 | 19.33 |
| 7 | 24 | 22 | 21.33 |
| 8 | 26 | 24 | 23.33 |
| 9 | 28 | 26 | 25.33 |
| 10 | 30 | 28 | 27.33 |
最后,我们可以使用二次移动平均的结果进行预测。比如,我们希望预测第11个时间点的交通量,根据二次移动平均的结果,我们可以得到预测值为27.33。
下面是Python代码实现:
```python
# 交通量数据
data = [10, 12, 15, 18, 20, 22, 24, 26, 28, 30]
# 一次移动平均
ma1 = [(data[i-1] + data[i] + data[i+1]) / 3 for i in range(1, len(data)-1)]
# 二次移动平均
ma2 = [(ma1[i-1] + ma1[i] + ma1[i+1]) / 3 for i in range(1, len(ma1)-1)]
# 预测第11个时间点的交通量
predict = ma2[-1]
print('一次移动平均:', ma1)
print('二次移动平均:', ma2)
print('预测值:', predict)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)