文章图片
数学建模——灰色预测模型Python代码 “”"
Spyder Editor
This is a temporary script file.
“”"
import numpy as np
import math
history_data = https://www.it610.com/article/[724.57,746.62,778.27,800.8,827.75,871.1,912.37,954.28,995.01,1037.2]
n = len(history_data)
X0 = np.array(history_data)
#累加生成
history_data_agg = [sum(history_data[0:i+1]) for i in range(n)]
X1 = np.array(history_data_agg)
#计算数据矩阵B和数据向量Y
B = np.zeros([n-1,2])
Y = np.zeros([n-1,1])
for i in range(0,n-1):
B[i][0] = -0.5*(X1[i] + X1[i+1])
B[i][1] = 1
Y[i][0] = X0[i+1]
#计算GM(1,1)微分方程的参数a和u
#A = np.zeros([2,1])
A = np.linalg.inv(B.T.dot(B)).dot(B.T).dot(Y)
a = A[0][0]
u = A[1][0]
#建立灰色预测模型
XX0 = np.zeros(n)
XX0[0] = X0[0]
for i in range(1,n):
XX0[i] = (X0[0] - u/a)*(1-math.exp(a))math.exp(-a(i));
#模型精度的后验差检验
e = 0 #求残差平均值
for i in range(0,n):
e += (X0[i] - XX0[i])
e /= n
#求历史数据平均值
aver = 0;
for i in range(0,n):
aver += X0[i]
aver /= n
#求历史数据方差
s12 = 0;
for i in range(0,n):
s12 += (X0[i]-aver)**2;
s12 /= n
#求残差方差
s22 = 0;
for i in range(0,n):
s22 += ((X0[i] - XX0[i]) - e)**2;
s22 /= n
#求后验差比值
C = s22 / s12
#求小误差概率
cout = 0
for i in range(0,n):
if abs((X0[i] - XX0[i]) - e) < 0.6754*math.sqrt(s12):
cout = cout+1
else:
cout = cout
P = cout / n
if (C < 0.35 and P > 0.95):
#预测精度为一级
m = 10 #请输入需要预测的年数
#print(‘往后m各年负荷为:’)
f = np.zeros(m)
for i in range(0,m):
f[i] = (X0[0] - u/a)*(1-math.exp(a))math.exp(-a(i+n))
else:
print(‘灰色预测法不适用’)
【数学建模|数学建模——灰色预测模型Python代码】
文章图片
推荐阅读
- Python|Python "爬虫"出发前的装备之二数据先行( Requests 模块)
- Python|Python实现八大经典排序算法
- 数据仓库|数据仓库也SaaS,Snowflake市值缘何超700亿美元()
- python|snowflake 数据库_Snowflake数据分析教程
- 前端|JavaScript基础大总结
- Pytorch|Pytorch丨expand函数
- PyTorch自定义损失函数
- pytorch|Pytorch函数expand()详解
- pytorch中expand()和expand_as()和repeat()函数解读