-
01-summary.py
1 #-*- coding: utf-8 -*- 2 3 # 代码6-1 4 5 import numpy as np 6 import pandas as pd 7 8 inputfile = '../data/data.csv' # 输入的数据文件 9 data = pd.read_csv(inputfile) # 读取数据 10 11 # 描述性统计分析 12 description = [data.min(), data.max(), data.mean(), data.std()] # 依次计算最小值、最大值、均值、标准差 13 description = pd.DataFrame(description, index = ['Min', 'Max', 'Mean', 'STD']).T # 将结果存入数据框 14 print('描述性统计结果:\n',np.round(description, 2)) # 保留两位小数 15 16 17 18 # 代码6-2 19 20 # 相关性分析 21 corr = data.corr(method = 'pearson') # 计算相关系数矩阵 22 print('相关系数矩阵为:\n',np.round(corr, 2)) # 保留两位小数 23 24 25 26 # 代码6-3 27 28 # 绘制热力图 29 import matplotlib.pyplot as plt 30 import seaborn as sns 31 plt.subplots(figsize=(10, 10)) # 设置画面大小 32 sns.heatmap(corr, annot=True, vmax=1, square=True, cmap="Blues") 33 plt.title('相关性热力图') 34 plt.show() 35 plt.close
- 02-lasso.py
1 #-*- coding: utf-8 -*- 2 3 # 代码6-4 4 5 import numpy as np 6 import pandas as pd 7 from sklearn.linear_model import Lasso 8 9 inputfile = '../data/data.csv' # 输入的数据文件 10 data = pd.read_csv(inputfile) # 读取数据 11 lasso = Lasso(1000) # 调用Lasso()函数,设置λ的值为1000 12 lasso.fit(data.iloc[:,0:13],data['y']) 13 print('相关系数为:',np.round(lasso.coef_,5)) # 输出结果,保留五位小数 14 15 print('相关系数非零个数为:',np.sum(lasso.coef_ != 0)) # 计算相关系数非零的个数 16 17 mask = lasso.coef_ != 0 # 返回一个相关系数是否为零的布尔数组 18 print('相关系数是否为零:',mask) 19 20 outputfile ='../tmp/new_reg_data.csv' # 输出的数据文件 21 new_reg_data = data.iloc[:, mask] # 返回相关系数非零的数据 22 new_reg_data.to_csv(outputfile) # 存储数据 23 print('输出数据的维度为:',new_reg_data.shape) # 查看输出数据的维度
- 03-predict.py
1 #-*- coding: utf-8 -*- 2 3 # 代码6-5 4 5 import sys 6 sys.path.append('../code') # 设置路径 7 import numpy as np 8 import pandas as pd 9 from GM11 import GM11 # 引入自编的灰色预测函数 10 11 inputfile1 = '../tmp/new_reg_data.csv' # 输入的数据文件 12 inputfile2 = '../data/data.csv' # 输入的数据文件 13 new_reg_data = pd.read_csv(inputfile1) # 读取经过特征选择后的数据 14 data = pd.read_csv(inputfile2) # 读取总的数据 15 new_reg_data.index = range(1994, 2014) 16 new_reg_data.loc[2014] = None 17 new_reg_data.loc[2015] = None 18 l = ['x1', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x13'] 19 for i in l: 20 f = GM11(new_reg_data.loc[range(1994, 2014),i].as_matrix())[0] 21 new_reg_data.loc[2014,i] = f(len(new_reg_data)-1) # 2014年预测结果 22 new_reg_data.loc[2015,i] = f(len(new_reg_data)) # 2015年预测结果 23 new_reg_data[i] = new_reg_data[i].round(2) # 保留两位小数 24 outputfile = '../tmp/new_reg_data_GM11.xls' # 灰色预测后保存的路径 25 y = list(data['y'].values) # 提取财政收入列,合并至新数据框中 26 y.extend([np.nan,np.nan]) 27 new_reg_data['y'] = y 28 new_reg_data.to_excel(outputfile) # 结果输出 29 print('预测结果为:\n',new_reg_data.loc[2014:2015,:]) # 预测结果展示 30 31 32 33 # 代码6-6 34 35 import matplotlib.pyplot as plt 36 from sklearn.svm import LinearSVR 37 38 inputfile = '../tmp/new_reg_data_GM11.xls' # 灰色预测后保存的路径 39 data = pd.read_excel(inputfile) # 读取数据 40 feature = ['x1', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x13'] # 属性所在列 41 data_train = data.loc[range(1994,2014)].copy() # 取2014年前的数据建模 42 data_mean = data_train.mean() 43 data_std = data_train.std() 44 data_train = (data_train - data_mean)/data_std # 数据标准化 45 x_train = data_train[feature].as_matrix() # 属性数据 46 y_train = data_train['y'].as_matrix() # 标签数据 47 48 linearsvr = LinearSVR() # 调用LinearSVR()函数 49 linearsvr.fit(x_train,y_train) 50 x = ((data[feature] - data_mean[feature])/data_std[feature]).as_matrix() # 预测,并还原结果。 51 data['y_pred'] = linearsvr.predict(x) * data_std['y'] + data_mean['y'] 52 outputfile = '../tmp/new_reg_data_GM11_revenue.xls' # SVR预测后保存的结果 53 data.to_excel(outputfile) 54 55 print('真实值与预测值分别为:\n',data[['y','y_pred']]) 56 57 fig = data[['y','y_pred']].plot(subplots = True, style=['b-o','r-*']) # 画出预测结果图 58 plt.show()
- GM11.py
1 #-*- coding: utf-8 -*- 2 3 def GM11(x0): #自定义灰色预测函数 4 import numpy as np 5 x1 = x0.cumsum() #1-AGO序列 6 z1 = (x1[:len(x1)-1] + x1[1:])/2.0 #紧邻均值(MEAN)生成序列 7 z1 = z1.reshape((len(z1),1)) 8 B = np.append(-z1, np.ones_like(z1), axis = 1) 9 Yn = x0[1:].reshape((len(x0)-1, 1)) 10 [[a],[b]] = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Yn) #计算参数 11 f = lambda k: (x0[0]-b/a)*np.exp(-a*(k-1))-(x0[0]-b/a)*np.exp(-a*(k-2)) #还原值 12 delta = np.abs(x0 - np.array([f(i) for i in range(1,len(x0)+1)])) 13 C = delta.std()/x0.std() 14 P = 1.0*(np.abs(delta - delta.mean()) < 0.6745*x0.std()).sum()/len(x0) 15 return f, a, b, x0[0], C, P #返回灰色预测函数、a、b、首项、方差比、小残差概率
标签:数据分析,Python,财政收入,import,np,new,x0,data,reg From: https://www.cnblogs.com/zhangfurong/p/17168780.html