数据分析第六章

财政收入印象因素及预测

1、对相关数据进行描述性统计

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
 
plt.rcParams ['font.sans-serif'] ='SimHei'               #显示中文
plt.rcParams ['axes.unicode_minus']=False                #显示负号

inputfile = r"D:\py_project\a_三下\data.csv"
data = pd.read_csv(inputfile)
# 描述性统计
description = [data.min(),data.max(),data.mean(),data.std()]
description = pd.DataFrame(description, index = ['Min','Max','Mean','STD']).T
print(np.round(description,2))

 

 

2、相关系数热力图

# 相关系数
corr = data.corr(method = 'pearson')
print('\n',np.round(corr,2))

import matplotlib.pyplot as plt
import seaborn as sns
plt.subplots(figsize=(10, 10)) # 设置画面大小 
sns.heatmap(corr, annot=True, vmax=1, square=True, cmap="Blues") 
plt.title('相关性热力图 3141')
plt.show()
plt.close

 

 

3、lasso回归选取关键属性

# lasso回归选取关键属性
from sklearn.linear_model import Lasso
lasso = Lasso(1000)
lasso.fit(data.iloc[:,0:13],data['y'])
print(np.round(lasso.coef_,5))
print(np.sum(lasso.coef_ != 0))
mask = lasso.coef_!=0
print('相关系数是否为零:',mask)
mask = np.append(mask,True)
outputfile=r'D:\py_project\a_三下\new_reg_data.csv'
new_reg_data=data.iloc[:,mask]
new_reg_data.to_csv(outputfile)
print('输出数据的维度为:',new_reg_data.shape)

 

 

 

4、灰色预测

def GM11(x0): #自定义灰色预测函数
  import numpy as np
  x1 = x0.cumsum() #1-AGO序列
  z1 = (x1[:len(x1)-1] + x1[1:])/2.0 #紧邻均值(MEAN)生成序列
  z1 = z1.reshape((len(z1),1))
  B = np.append(-z1, np.ones_like(z1), axis = 1)
  Yn = x0[1:].reshape((len(x0)-1, 1))
  [[a],[b]] = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Yn) #计算参数
  f = lambda k: (x0[0]-b/a)*np.exp(-a*(k-1))-(x0[0]-b/a)*np.exp(-a*(k-2)) #还原值
  delta = np.abs(x0 - np.array([f(i) for i in range(1,len(x0)+1)]))
  C = delta.std()/x0.std()
  P = 1.0*(np.abs(delta - delta.mean()) < 0.6745*x0.std()).sum()/len(x0)
  return f, a, b, x0[0], C, P #返回灰色预测函数、a、b、首项、方差比、小残差概率

inputfile1 =  r'D:/py_project/a_三下/new_reg_data.csv' # 输入的数据文件
inputfile2 =  r'D:/py_project/a_三下/data.csv'  # 输入的数据文件
new_reg_data = pd.read_csv(inputfile1)  # 读取经过特征选择后的数据
data = pd.read_csv(inputfile2)  # 读取总的数据
new_reg_data.index = range(1994, 2014)
new_reg_data.loc[2014] = None
new_reg_data.loc[2015] = None
new_reg_data.loc[2016] = None
l = ['x1', 'x4', 'x5', 'x6', 'x7', 'x8']
for i in l:
    f = GM11(new_reg_data.loc[range(1994, 2014),i].to_numpy())[0]
    new_reg_data.loc[2014,i] = f(len(new_reg_data)-1)  # 2014年预测结果
    print(new_reg_data.loc[2014,i])
    new_reg_data.loc[2015,i] = f(len(new_reg_data))  # 2015年预测结果
    print(new_reg_data.loc[2015,i])
    new_reg_data.loc[2016,i] = f(len(new_reg_data)+1)  # 2016年预测结果
    print(new_reg_data.loc[2016,i])
    new_reg_data[i] = new_reg_data[i].round(3)  # 保留两位小数
    print("*"*50)
outputfile = './new_reg_data_GM11.xls'  # 灰色预测后保存的路径
y = list(data['y'].values)  # 提取财政收入列,合并至新数据框中
y.extend([np.nan,np.nan,np.nan])
new_reg_data['y'] = y
new_reg_data.to_excel(outputfile)  # 结果输出
print('预测结果为:\n',new_reg_data.loc[2015:2016,:])  # 预测结果展示

 

posted @ 2023-03-05 22:30  Yunnnaaaaa  阅读(10)  评论(0编辑  收藏  举报