1. 数据分析
import matplotlib.pyplot as plt import numpy as np import pandas as pd inputfile = './data.csv' data = pd.read_csv(inputfile) describe = data.describe() #describe()函数能算出数据集的八个统计量 print(describe)
2.相关性分析
corr = data.corr(method = 'pearson') pd.options.display.float_format = '{:,.2f}'.format ## 指定小数位数 data.corr() # print(np.round(corr,2))
3.绘制热力图
import matplotlib.pyplot as plt import seaborn as sns plt.subplots(figsize=(10,10)) sns.heatmap(corr,annot = True,vmax = 1,square = True,cmap = "Accent") plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.title('相关性热力图') plt.show() plt.close
4.Lasso回归
from sklearn.linear_model import Lasso inputfile = './data.csv' data = pd.read_csv(inputfile) lasso = Lasso(1000) lasso.fit(data.iloc[:,0:14],data['y']) print(np.round(lasso.coef_,5)) print(np.sum(lasso.coef_ != 0)) mask = lasso.coef_ != 0 print(mask) outputfile = './new_reg_data.csv' new_reg_data = data.iloc[:,mask] new_reg_data.to_csv(outputfile) print(new_reg_data.shape)
5.灰色预测模型
import sys sys.path.append('./code') # 设置路径 import numpy as np import pandas as pd from GM11 import GM11 # 引入自编的灰色预测函数 inputfile1 = './new_reg_data.csv' # 输入的数据文件 inputfile2 = './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,:]) # 预测结果展示
标签:loc,--,print,财政收入,import,第六章,new,data,reg From: https://www.cnblogs.com/2020310148tjy/p/17179423.html