概述
我的GM(1,1)灰色预测模型老是出现错误,小白一枚,求大神指教
#coding=
import numpy as np
import pandas as pd
from GM11 import GM11
inputfile=‘C:UsersdellDesktop毕业设计社会消费品零售总额数据new_reg_data.csv’ #输入的数据文件
inputfile1=‘C:UsersdellDesktop毕业设计社会消费品零售总额数据data.csv’ #输入的数据文件
new_reg_data=pd.read_csv(inputfile) #读取经过特征选择后的数据
data=pd.read_csv(inputfile1) #读取总的数据
new_reg_data.index=range(1996, 2016)
new_reg_data.loc[2016]=None
new_reg_data.loc[2017]=None
new_reg_data.loc[2018]=None
l=[‘x1’,‘x2’,‘x3’,‘x4’,‘x5’,‘x6’,‘x7’,‘x8’] #根据变量选择选取这六个变量
for i in l:
f = GM11(new_reg_data[i][range(1996,2016)].as_matrix())[0] #灰色预测函数
new_reg_data.loc[2016,i]=f(len(new_reg_data)-1) #2016年预测结果
new_reg_data.loc[2017,i]=f(len(new_reg_data)) #2017年预测结果
new_reg_data.loc[2018,i] = f(len(new_reg_data)) # 2018年预测结果
new_reg_data[i]=new_reg_data[i].round(2) #保留两位小数
outputfile =‘C:UsersdellDesktop毕业设计社会消费品零售总额数据new_reg_data_GM11.xls’ #灰色预测后保存的路径
y=list(data[‘y’].values) #提取财政收入列,合并至新数据框中
y.extend([np.nan,np.nan])
new_reg_data[‘y’]=y
new_reg_data.to_excel(outputfile) #结果输出
print(‘预测结果为:’,new_reg_data.loc[2016:2017:2018,:]) #预测结果展示
运行结果
Traceback (most recent call last):
File “C:UsersdellDesktopperson相关性检验venvlibsite-packagespandascoreindexesbase.py”, line 2657, in get_loc
return self._engine.get_loc(key)
File “pandas_libsindex.pyx”, line 108, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libsindex.pyx”, line 132, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libshashtable_class_helper.pxi”, line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas_libshashtable_class_helper.pxi”, line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘x1’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:/Users/dell/Desktop/person相关性检验/GM11灰色预测模型.py”, line 17, in
f = GM11(new_reg_data[i][range(1996,2016)].as_matrix())[0] #灰色预测函数
File “C:UsersdellDesktopperson相关性检验venvlibsite-packagespandascoreframe.py”, line 2927, in getitem
indexer = self.columns.get_loc(key)
File “C:UsersdellDesktopperson相关性检验venvlibsite-packagespandascoreindexesbase.py”, line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File “pandas_libsindex.pyx”, line 108, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libsindex.pyx”, line 132, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libshashtable_class_helper.pxi”, line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas_libshashtable_class_helper.pxi”, line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘x1’
最后
以上就是老实哈密瓜为你收集整理的Python Keyerror,x1 求大神指教,GM11灰色预测的全部内容,希望文章能够帮你解决Python Keyerror,x1 求大神指教,GM11灰色预测所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复