写出用GM(1,1)进行预测的python代码
时间: 2024-05-09 18:16:50 浏览: 206
下面是GM(1,1)进行预测的Python代码:
```python
import numpy as np
def GM11(x, predict_num):
"""
:param x:输入的序列,numpy对象
:param predict_num:需要预测的个数
:return:返回预测序列
"""
x1 = x.cumsum() # 一次累加
z1 = (x1[: -1] + x1[1:]) / 2.0 # 紧邻均值
z1 = z1.reshape((len(z1), 1))
B = np.append(-z1, np.ones_like(z1), axis=1)
Y = x[1:].reshape((len(x) - 1, 1))
[[a], [b]] = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Y) # 计算参数
predict = (x[0] - b / a) * np.exp(-a * (np.arange(1, predict_num + 1))) # 预测
return predict
x = np.array([10, 16, 24, 35, 48, 70, 90])
predict_num = 3
predict = GM11(x, predict_num)
print(predict)
```
其中,参数x为输入序列,predict_num为需要预测的个数。最后,预测结果保存在predict变量中。
阅读全文