当前位置:   article > 正文

2021国赛C题

2021国赛c题

指导老师编写的代码,时间序列模型

第一部分代码 

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. from scipy import stats
  4. import numpy as np
  5. data1 = pd.read_excel('C_附件1.xlsx',sheet_name='企业的订货量(m³)')
  6. data2 = pd.read_excel('C_附件1.xlsx',sheet_name='供应商的供货量(m³)')
  1. for n in range(data1.shape[0]):
  2. plt.figure(figsize=[15,6])
  3. plt.plot(data1.iloc[n,2:],'r')
  4. plt.plot(data2.iloc[n,2:],'g')
  5. plt.title(data1.iloc[n,0])
  6. plt.show()

  1. n=4
  2. x = data1.iloc[n,2:]
  3. y = data2.iloc[n,2:]
  4. plt.figure(figsize=[15,6])
  5. plt.plot(data1.iloc[n,2:],'r')
  6. plt.plot(data2.iloc[n,2:],'g')
  7. plt.title(data1.iloc[n,0])
  8. plt.show()
  1. data1_T = data1.T
  2. data2_T = data2.T
  3. def get_character(x,y):
  4. '获取重要性特征指标'
  5. y1 = y[x>0] # 去掉订单为零的数据
  6. x1 = x[x>0]
  7. # dy = y1-x1
  8. # 均值
  9. a = stats.trim_mean(y1,0.1)
  10. # 标准差
  11. b = y1.std()
  12. # 有效订货天数
  13. c = y1.shape[0]
  14. # 最大值
  15. d = y1.max()
  16. # 最小值
  17. f = y1.min()
  18. # 足额供货率
  19. e = y1[y1>=x1].shape[0] / y1.shape[0]
  20. out = [d,f,a,-b,c,e]
  21. return out
  22. out = []
  23. for n in range(data1_T.shape[1]):
  24. x = data1_T.iloc[2:,n] # 第n+1个供货商的订单量
  25. y = data2_T.iloc[2:,n] # 第n+1个供货商的供货量
  26. # print(get_character(x,y))
  27. out.append(get_character(x,y))
  28. columns = ['最大值','最小值','截断均值','标准差','有效订货天数','足额供货率']
  29. # data_c = pd.DataFrame(out,index=data1_T.index,columns=columns)
  30. data_c = pd.DataFrame(out)
  31. data_c.columns = columns
  32. data_c.to_excel('out.xlsx')
data_c
  1. n=267
  2. print(out[n])
  3. data1_T.iloc[2:,n].plot()
  4. data2_T.iloc[2:,n].plot()
  1. from GRA_BuildModel import GraModel
  2. #建立灰色关联模型
  3. data = pd.read_excel('out.xlsx')
  4. model = GraModel(data, standard=True,p=0.1)
  5. aa = model.result['cors']['value']
  6. aa = pd.DataFrame(aa)
  7. aaa = aa.mean(axis=1)
  8. aac= aaa.sort_values(ascending=False)
  9. aac
  1. # 因子分析
  2. from factor_analyzer import FactorAnalyzer
  3. from factor_analyzer.factor_analyzer import calculate_bartlett_sphericity
  4. chi_square_value,p_value=calculate_bartlett_sphericity(data_c) # KMO检验
  5. chi_square_value, p_value
  6. # 因子分析
  7. fa = FactorAnalyzer(25, rotation=None)
  8. fa.fit(data_c)
  9. # Check Eigenvalues
  10. ev, v = fa.get_eigenvalues()
  11. plt.scatter(range(1,data_c.shape[1]+1),ev)
  12. plt.plot(range(1,data_c.shape[1]+1),ev)
  13. plt.title('Scree Plot')
  14. plt.xlabel('Factors')
  15. plt.ylabel('Eigenvalue')
  16. plt.grid()
  17. plt.show()
  1. fa = FactorAnalyzer(5, rotation="varimax")
  2. fa.fit(data_c)
  3. at = fa.get_factor_variance()
  4. ya = fa.transform(data_c) * (at[1].T)
  5. ya
p
fa.transform(data_c)
  1. ccc=pd.DataFrame(fa.transform(data_c))
  2. new= ccc.sum(axis=1).sort_values(ascending=False)
new
  1. for i in range(50):
  2. n=new.index[i]
  3. print(n+1,out[n])
  4. plt.figure(figsize=[15,6])
  5. plt.plot(data1_T.iloc[2:,n],'r')
  6. plt.plot(data2_T.iloc[2:,n],'g')
  7. plt.title('i=%d,n=%d'%(i+1,n+1))
  8. plt.show()

第二部分代码

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. from scipy import stats
  4. import numpy as np
  5. from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
  6. import itertools
  7. import statsmodels.api as sm
  8. import warnings
  9. warnings.filterwarnings("ignore")
  10. from statsmodels.tsa.stattools import adfuller # 单位根检验
  11. from statsmodels.tsa.arima_model import ARIMA
  12. from statsmodels.stats.diagnostic import acorr_ljungbox # 白噪声检验
  13. # 绘图支持中文
  14. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
  15. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
  16. data1 = pd.read_excel('C_附件1.xlsx',sheet_name='企业的订货量(m³)')
  17. data2 = pd.read_excel('C_附件1.xlsx',sheet_name='供应商的供货量(m³)')
  18. t = pd.period_range('2017-2-1', periods=240, freq='w')
  19. index = pd.PeriodIndex(t,freq='d')
  20. data1_T = data1.T.iloc[2:,:]
  21. data2_T = data2.T.iloc[2:,:]
  22. data1_T.index=index
  23. data2_T.index=index
  1. def get_pdq(dy_24):
  2. '模型定阶'
  3. p = q =range(0,2)
  4. d = range(0,2)
  5. pdq = list(itertools.product(p,d,q))
  6. seasonal_pdq = [(x[0],x[1],x[2],24) for x in list(itertools.product(p,d,q))]
  7. out = []
  8. for param in pdq:
  9. for ps in seasonal_pdq:
  10. try:
  11. mod = sm.tsa.statespace.SARIMAX(dy_24,
  12. order=param,
  13. seasonal_order=ps,
  14. enforce_stationarity=False,
  15. enforce_invertibility=False)
  16. results = mod.fit()
  17. # print('ARIMA{}*{} - AIC:{}'.format(param,ps,results.aic))
  18. out.append([param,ps,results.aic])
  19. except:
  20. continue
  21. return out
  1. def forecast_y(par,dy):
  2. '预测'
  3. aic = pd.DataFrame([x[2] for x in par])
  4. i = aic.idxmin().loc[0]
  5. order = par[i][0]
  6. seasonal_order = par[i][1]
  7. mod = sm.tsa.statespace.SARIMAX(dy,
  8. order=order,
  9. seasonal_order=seasonal_order,
  10. enforce_stationarity=False,
  11. enforce_invertibility=False)
  12. results = mod.fit()
  13. pre = results.predict()
  14. forecast = results.forecast(24)
  15. return forecast,pre
  1. # 预测24周的供货量
  2. out = pd.DataFrame()
  3. fore_out=pd.DataFrame()
  4. for n in range(data2_T.shape[1]):#
  5. print('正在预测第 {} 个供货商,请稍候...'.format(n+1),end=' ')
  6. y = data2_T.iloc[:,n]
  7. dy_24 = y.diff(24).dropna().astype('float')
  8. # 模型定阶
  9. par = get_pdq(dy_24)
  10. aic = pd.DataFrame([x[2] for x in par])
  11. i = aic.idxmin().loc[0]
  12. order = par[i][0]
  13. seasonal_order = par[i][1]
  14. print(' --> 最优预测模型为:ARIMA{}*{}'.format(order,seasonal_order))
  15. # 向后预测24周
  16. fore,pre=forecast_y(par,dy_24)
  17. #还原数据
  18. t = pd.period_range('2021-9-12', periods=24, freq='w')
  19. index = pd.PeriodIndex(t,freq='d')
  20. fore.index = index
  21. tem = y[-24:].copy()
  22. tem.index = index
  23. # y = data2_T.iloc[:,n]
  24. # forecast =pd.concat([y,fore+tem])
  25. # plt.figure(figsize=[16,6])
  26. # plt.plot(forecast.dropna(),'g')
  27. # plt.plot(y,'r')
  28. # plt.show()
  29. te = fore.astype('int')+tem
  30. te[te<0]=0
  31. new_y = pd.concat([pre.astype('int'),te],axis=0)
  32. new_y[new_y<0]=0
  33. out['S'+str(n+1).zfill(3)] = te
  34. fore_out['S'+str(n+1).zfill(3)] = new_y
  35. out.index = ['第'+ str(i+1).zfill(2) + '周' for i in range(24)]
  36. out.T.to_excel('供货量预测结果_24周.xlsx')
  37. fore_out.index = ['第'+ str(i+25).zfill(2) + '周' for i in range(240)]
  38. fore_out.T.to_excel('供货量预测结果_全部.xlsx')
  1. new_y = pd.concat([pre,te],axis=0)
  2. plt.figure(figsize=[16,6])
  3. plt.plot(y.astype('int'),'g')
  4. plt.plot(new_y,'r')
  5. plt.show()
  1. forecast =pd.concat([y,fore+tem])
  2. plt.figure(figsize=[16,6])
  3. plt.plot(forecast.dropna(),'g')
  4. plt.plot(y,'r')
  5. plt.show()
  1. y = data2_T.iloc[:,79]
  2. dy_24 = y.diff(24).dropna().astype('float')
  3. plt.figure(figsize=(15,6))
  4. plot_acf(dy_24,lags=30)
  5. plt.show()
  6. plt.figure(figsize=(15,6))
  7. # tsplot(cpi_d,lags=30)
  8. plot_pacf(dy_24,lags=30)
  9. plt.show()
  1. # 白噪声检验
  2. acorr_ljungbox(y.diff().diff(24).dropna().astype('float'),lags=1)
  1. # 平稳性检验
  2. adfuller(dy_24)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/361724
推荐阅读
相关标签
  

闽ICP备14008679号