Posted on ::
# -*- coding: utf-8 -*-

# 代码6-1

import numpy as np
import pandas as pd

inputfile = '../data/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('描述性统计结果:\n', np.round(description, 2))  # 保留两位小数
描述性统计结果:
             Min         Max        Mean         STD
x1   3831732.00  7599295.00  5579519.95  1262194.72
x2       181.54     2110.78      765.04      595.70
x3       448.19     6882.85     2370.83     1919.17
x4      7571.00    42049.14    19644.69    10203.02
x5      6212.70    33156.83    15870.95     8199.77
x6   6370241.00  8323096.00  7350513.60   621341.85
x7       525.71     4454.55     1712.24     1184.71
x8       985.31    15420.14     5705.80     4478.40
x9        60.62      228.46      129.49       50.51
x10       65.66      852.56      340.22      251.58
x11       97.50      120.00      103.31        5.51
x12        1.03        1.91        1.42        0.25
x13     5321.00    41972.00    17273.80    11109.19
y         64.87     2088.14      618.08      609.25
# 代码6-2

# 相关性分析
corr = data.corr(method='pearson')  # 计算相关系数矩阵
print('相关系数矩阵为:\n', np.round(corr, 2))  # 保留两位小数
相关系数矩阵为:
        x1    x2    x3    x4    x5    x6    x7    x8    x9   x10   x11   x12  \
x1   1.00  0.95  0.95  0.97  0.97  0.99  0.95  0.97  0.98  0.98 -0.29  0.94   
x2   0.95  1.00  1.00  0.99  0.99  0.92  0.99  0.99  0.98  0.98 -0.13  0.89   
x3   0.95  1.00  1.00  0.99  0.99  0.92  1.00  0.99  0.98  0.99 -0.15  0.89   
x4   0.97  0.99  0.99  1.00  1.00  0.95  0.99  1.00  0.99  1.00 -0.19  0.91   
x5   0.97  0.99  0.99  1.00  1.00  0.95  0.99  1.00  0.99  1.00 -0.18  0.90   
x6   0.99  0.92  0.92  0.95  0.95  1.00  0.93  0.95  0.97  0.96 -0.34  0.95   
x7   0.95  0.99  1.00  0.99  0.99  0.93  1.00  0.99  0.98  0.99 -0.15  0.89   
x8   0.97  0.99  0.99  1.00  1.00  0.95  0.99  1.00  0.99  1.00 -0.15  0.90   
x9   0.98  0.98  0.98  0.99  0.99  0.97  0.98  0.99  1.00  0.99 -0.23  0.91   
x10  0.98  0.98  0.99  1.00  1.00  0.96  0.99  1.00  0.99  1.00 -0.17  0.90   
x11 -0.29 -0.13 -0.15 -0.19 -0.18 -0.34 -0.15 -0.15 -0.23 -0.17  1.00 -0.43   
x12  0.94  0.89  0.89  0.91  0.90  0.95  0.89  0.90  0.91  0.90 -0.43  1.00   
x13  0.96  1.00  1.00  1.00  0.99  0.94  1.00  1.00  0.99  0.99 -0.16  0.90   
y    0.94  0.98  0.99  0.99  0.99  0.91  0.99  0.99  0.98  0.99 -0.12  0.87   

      x13     y  
x1   0.96  0.94  
x2   1.00  0.98  
x3   1.00  0.99  
x4   1.00  0.99  
x5   0.99  0.99  
x6   0.94  0.91  
x7   1.00  0.99  
x8   1.00  0.99  
x9   0.99  0.98  
x10  0.99  0.99  
x11 -0.16 -0.12  
x12  0.90  0.87  
x13  1.00  0.99  
y    0.99  1.00  
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['SimHei']  # 中文标签
plt.rcParams['axes.unicode_minus'] = False  # 正常显示负号

plt.subplots(figsize=(12, 12))
sns.heatmap(corr, annot=True, vmax=1, square=True, cmap="Reds")
plt.title('相关性热力图(20信计1班李之琛3322)')
plt.savefig('../imag/hot.jpg', dpi=600)
plt.show()

# 代码6-3

# 绘制热力图
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['SimHei']  # 中文标签
plt.rcParams['axes.unicode_minus'] = False  # 正常显示负号
plt.subplots(figsize=(10, 10))  # 设置画面大小
sns.heatmap(corr, annot=True, vmax=1, square=True, cmap="Blues")
plt.title('相关性热力图')
plt.show()
# plt.close

import numpy as np
import pandas as pd
from sklearn.linear_model import Lasso
inputfile = '../data/data.csv'  # 输入的数据文件
data = pd.read_csv(inputfile)  # 读取数据
lasso = Lasso(1000)  # 调用Lasso()函数,设置λ的值为1000
lasso.fit(data.iloc[:,0:13],data['y'])
print('相关系数为:',np.round(lasso.coef_,5))  # 输出结果,保留五位小数
相关系数为: [-1.8000e-04 -0.0000e+00  1.2414e-01 -1.0310e-02  6.5400e-02  1.2000e-04
  3.1741e-01  3.4900e-02 -0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
 -4.0300e-02]


F:\Anaconda\envs\my\lib\site-packages\sklearn\linear_model\_coordinate_descent.py:648: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations, check the scale of the features or consider increasing regularisation. Duality gap: 3.417e+04, tolerance: 7.053e+02
  model = cd_fast.enet_coordinate_descent(
lasso.coef_
array([-1.76147790e-04, -0.00000000e+00,  1.24143041e-01, -1.03120575e-02,
        6.53999569e-02,  1.15234764e-04,  3.17411469e-01,  3.49002210e-02,
       -0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
       -4.02991430e-02])
print('相关系数非零个数为:',np.sum(lasso.coef_ != 0))  # 计算相关系数非零的个数
相关系数非零个数为: 8
mask = lasso.coef_ != 0  # 返回一个相关系数是否为零的布尔数组
print('相关系数是否为零:',mask)
相关系数是否为零: [ True False  True  True  True  True  True  True False False False False
  True]
data.head(5)
x1x2x3x4x5x6x7x8x9x10x11x12x13y
03831732181.54448.197571.006212.706370241525.71985.3160.6265.66120.01.029532164.87
13913824214.63549.979038.167601.736467115618.251259.2073.4695.46113.51.051652999.75
23928907239.56686.449905.318092.826560508638.941468.0681.1681.16108.21.064700888.11
34282130261.58802.5910444.608767.986664862656.581678.1285.7291.70102.21.0927694106.07
44453911283.14904.5711255.709422.336741400758.831893.5288.88114.6197.71.2008027137.32
mask = np.append(mask,True)
mask
array([ True, False,  True,  True,  True,  True,  True,  True, False,
       False, False, False,  True,  True])
outputfile ='../tmp/new_reg_data_2.csv'  # 输出的数据文件
new_reg_data = data.iloc[:, mask]  # 返回相关系数非零的数据
new_reg_data
x1x3x4x5x6x7x8x13y
03831732448.197571.006212.706370241525.71985.31532164.87
13913824549.979038.167601.736467115618.251259.20652999.75
23928907686.449905.318092.826560508638.941468.06700888.11
34282130802.5910444.608767.986664862656.581678.127694106.07
44453911904.5711255.709422.336741400758.831893.528027137.32
545488521000.6912018.529751.446850024878.262139.188549188.14
649625791121.1313966.5311349.477006896923.672492.749566219.91
750293381248.2914694.0011467.357125979978.212841.6510473271.91
850702161370.6813380.4710671.7872062291009.243203.9611469269.10
952107061494.2715002.5911570.5872518881175.173758.6212360300.55
1054070871677.7716884.1613120.8373767201348.934450.5514174338.45
1157445501905.8418287.2414468.2475053221519.165154.2316394408.86
1259949732199.1419850.6615444.9376072201696.386081.8617881476.72
1362363122624.2422469.2218951.3277347871863.347140.3220058838.99
1465290453187.3925316.7220835.9578416952105.548287.3822114843.14
1567914953615.7727609.5922820.8979461542659.859138.21241901107.67
1671106954476.3830658.4925011.6180613703263.5710748.28295491399.16
1774317555243.0334438.0828209.7481457973412.2112423.44342141535.14
1875129975977.2738053.5230490.4482229693758.3913551.21379341579.68
1975992956882.8542049.1433156.8383230964454.5515420.14419722088.14
new_reg_data = new_reg_data.iloc[:,:-1]
new_reg_data
x1x3x4x5x6x7x8x13
03831732448.197571.006212.706370241525.71985.315321
13913824549.979038.167601.736467115618.251259.206529
23928907686.449905.318092.826560508638.941468.067008
34282130802.5910444.608767.986664862656.581678.127694
44453911904.5711255.709422.336741400758.831893.528027
545488521000.6912018.529751.446850024878.262139.188549
649625791121.1313966.5311349.477006896923.672492.749566
750293381248.2914694.0011467.357125979978.212841.6510473
850702161370.6813380.4710671.7872062291009.243203.9611469
952107061494.2715002.5911570.5872518881175.173758.6212360
1054070871677.7716884.1613120.8373767201348.934450.5514174
1157445501905.8418287.2414468.2475053221519.165154.2316394
1259949732199.1419850.6615444.9376072201696.386081.8617881
1362363122624.2422469.2218951.3277347871863.347140.3220058
1465290453187.3925316.7220835.9578416952105.548287.3822114
1567914953615.7727609.5922820.8979461542659.859138.2124190
1671106954476.3830658.4925011.6180613703263.5710748.2829549
1774317555243.0334438.0828209.7481457973412.2112423.4434214
1875129975977.2738053.5230490.4482229693758.3913551.2137934
1975992956882.8542049.1433156.8383230964454.5515420.1441972
new_reg_data.to_csv(outputfile)  # 存储数据
print('输出数据的维度为:',new_reg_data.shape)  # 查看输出数据的维度
输出数据的维度为: (20, 8)
#-*- coding: utf-8 -*-

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、首项、方差比、小残差概率
# 代码6-5

# import sys
# sys.path.append('../code')  # 设置路径
import numpy as np
import pandas as pd
# from GM11 import GM11  # 引入自编的灰色预测函数
inputfile1 = '../tmp/new_reg_data_2.csv'  # 输入的数据文件
inputfile2 = '../data/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
l = ['x1', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x13']
C:\Windows\Temp\ipykernel_63584\756966604.py:6: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  new_reg_data.loc[2014] = None
C:\Windows\Temp\ipykernel_63584\756966604.py:7: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  new_reg_data.loc[2015] = None
new_reg_data
Unnamed: 0x1x3x4x5x6x7x8x13
19940.03831732.0448.197571.006212.706370241.0525.71985.315321.0
19951.03913824.0549.979038.167601.736467115.0618.251259.206529.0
19962.03928907.0686.449905.318092.826560508.0638.941468.067008.0
19973.04282130.0802.5910444.608767.986664862.0656.581678.127694.0
19984.04453911.0904.5711255.709422.336741400.0758.831893.528027.0
19995.04548852.01000.6912018.529751.446850024.0878.262139.188549.0
20006.04962579.01121.1313966.5311349.477006896.0923.672492.749566.0
20017.05029338.01248.2914694.0011467.357125979.0978.212841.6510473.0
20028.05070216.01370.6813380.4710671.787206229.01009.243203.9611469.0
20039.05210706.01494.2715002.5911570.587251888.01175.173758.6212360.0
200410.05407087.01677.7716884.1613120.837376720.01348.934450.5514174.0
200511.05744550.01905.8418287.2414468.247505322.01519.165154.2316394.0
200612.05994973.02199.1419850.6615444.937607220.01696.386081.8617881.0
200713.06236312.02624.2422469.2218951.327734787.01863.347140.3220058.0
200814.06529045.03187.3925316.7220835.957841695.02105.548287.3822114.0
200915.06791495.03615.7727609.5922820.897946154.02659.859138.2124190.0
201016.07110695.04476.3830658.4925011.618061370.03263.5710748.2829549.0
201117.07431755.05243.0334438.0828209.748145797.03412.2112423.4434214.0
201218.07512997.05977.2738053.5230490.448222969.03758.3913551.2137934.0
201319.07599295.06882.8542049.1433156.838323096.04454.5515420.1441972.0
2014NaNNaNNaNNaNNaNNaNNaNNaNNaN
2015NaNNaNNaNNaNNaNNaNNaNNaNNaN
import xlwt
for i in l:
  f = GM11(new_reg_data.loc[range(1994, 2014),i].values)[0]
  new_reg_data.loc[2014,i] = f(len(new_reg_data)-1)  # 2014年预测结果
  new_reg_data.loc[2015,i] = f(len(new_reg_data))  # 2015年预测结果
  new_reg_data[i] = new_reg_data[i].round(2)  # 保留两位小数
outputfile = '../tmp/new_reg_data_GM11_2.xls'  # 灰色预测后保存的路径
y = list(data['y'].values)  # 提取财政收入列,合并至新数据框中
y.extend([np.nan,np.nan])
new_reg_data['y'] = y
new_reg_data.to_excel(outputfile)  # 结果输出
print('预测结果为:\n',new_reg_data.loc[2014:2015,:])  # 预测结果展示
预测结果为:
       Unnamed: 0          x1       x3        x4        x5          x6  \
2014         NaN  8142148.24  7042.31  43611.84  35046.63  8505522.58   
2015         NaN  8460489.28  8166.92  47792.22  38384.22  8627139.31   

           x7        x8       x13   y  
2014  4600.40  18686.28  44506.47 NaN  
2015  5214.78  21474.47  49945.88 NaN  


C:\Windows\Temp\ipykernel_63584\1948140384.py:10: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  new_reg_data.to_excel(outputfile)  # 结果输出
# 代码6-6

import matplotlib.pyplot as plt
from sklearn.svm import LinearSVR

inputfile = '../tmp/new_reg_data_GM11_2.xls'  # 灰色预测后保存的路径
data = pd.read_excel(inputfile)  # 读取数据
feature = ['x1', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x13']  # 属性所在列
data
Unnamed: 0.1Unnamed: 0x1x3x4x5x6x7x8x13y
019940.03831732.00448.197571.006212.706370241.00525.71985.315321.0064.87
119951.03913824.00549.979038.167601.736467115.00618.251259.206529.0099.75
219962.03928907.00686.449905.318092.826560508.00638.941468.067008.0088.11
319973.04282130.00802.5910444.608767.986664862.00656.581678.127694.00106.07
419984.04453911.00904.5711255.709422.336741400.00758.831893.528027.00137.32
519995.04548852.001000.6912018.529751.446850024.00878.262139.188549.00188.14
620006.04962579.001121.1313966.5311349.477006896.00923.672492.749566.00219.91
720017.05029338.001248.2914694.0011467.357125979.00978.212841.6510473.00271.91
820028.05070216.001370.6813380.4710671.787206229.001009.243203.9611469.00269.10
920039.05210706.001494.2715002.5911570.587251888.001175.173758.6212360.00300.55
10200410.05407087.001677.7716884.1613120.837376720.001348.934450.5514174.00338.45
11200511.05744550.001905.8418287.2414468.247505322.001519.165154.2316394.00408.86
12200612.05994973.002199.1419850.6615444.937607220.001696.386081.8617881.00476.72
13200713.06236312.002624.2422469.2218951.327734787.001863.347140.3220058.00838.99
14200814.06529045.003187.3925316.7220835.957841695.002105.548287.3822114.00843.14
15200915.06791495.003615.7727609.5922820.897946154.002659.859138.2124190.001107.67
16201016.07110695.004476.3830658.4925011.618061370.003263.5710748.2829549.001399.16
17201117.07431755.005243.0334438.0828209.748145797.003412.2112423.4434214.001535.14
18201218.07512997.005977.2738053.5230490.448222969.003758.3913551.2137934.001579.68
19201319.07599295.006882.8542049.1433156.838323096.004454.5515420.1441972.002088.14
202014NaN8142148.247042.3143611.8435046.638505522.584600.4018686.2844506.47NaN
212015NaN8460489.288166.9247792.2238384.228627139.315214.7821474.4749945.88NaN
data_train=data.loc[0:19,:]
data_train.head()
Unnamed: 0.1Unnamed: 0x1x3x4x5x6x7x8x13y
019940.03831732.0448.197571.006212.706370241.0525.71985.315321.064.87
119951.03913824.0549.979038.167601.736467115.0618.251259.206529.099.75
219962.03928907.0686.449905.318092.826560508.0638.941468.067008.088.11
319973.04282130.0802.5910444.608767.986664862.0656.581678.127694.0106.07
419984.04453911.0904.5711255.709422.336741400.0758.831893.528027.0137.32
data_mean = data_train.mean()
data_std = data_train.std()
data_train = (data_train - data_mean)/data_std  # 数据标准化
x_train = data_train[feature].values  # 属性数据
y_train = data_train['y'].values  # 标签数据
data_train[feature]
x1x3x4x5x6x7x8x13
0-1.384721-1.001807-1.183344-1.177868-1.577670-1.001532-1.054057-1.075938
1-1.319682-0.948774-1.039547-1.008469-1.421759-0.923420-0.992899-0.967199
2-1.307732-0.877665-0.954558-0.948579-1.271451-0.905956-0.946262-0.924082
3-1.027884-0.817144-0.901702-0.866240-1.103501-0.891067-0.899357-0.862331
4-0.891787-0.764006-0.822206-0.786439-0.980320-0.804759-0.851259-0.832356
5-0.816568-0.713922-0.747442-0.746302-0.805498-0.703950-0.796405-0.785368
6-0.488784-0.651165-0.556517-0.551415-0.553025-0.665620-0.717457-0.693822
7-0.435893-0.584907-0.485218-0.537039-0.361370-0.619583-0.639547-0.612178
8-0.403507-0.521135-0.613957-0.634063-0.232215-0.593391-0.558646-0.522522
9-0.292201-0.456737-0.454973-0.524450-0.158730-0.453332-0.434793-0.442319
10-0.136614-0.361123-0.270560-0.3353900.042177-0.306664-0.280290-0.279030
110.130748-0.242285-0.133043-0.1710670.249152-0.162975-0.123162-0.079196
120.329151-0.0894580.020188-0.0519550.413148-0.0133860.0839720.054657
130.5203570.1320440.2768330.3756660.6184570.1275420.3203200.250621
140.7522810.4254790.5559170.6055050.7905170.3319800.5764520.435693
150.9602120.6486900.7806420.8475780.9586360.7998650.7664370.622566
161.2131051.0971191.0794651.1147461.1440671.3094561.1259561.104959
171.4674721.4965901.4499031.5047731.2799451.4349211.5000091.524882
181.5318371.8791721.8042531.7829151.4041471.7271271.7518331.859740
191.6002092.3510332.1958652.1080931.5652942.3147452.1691542.223223
linearsvr = LinearSVR()  # 调用LinearSVR()函数
linearsvr.fit(x_train,y_train)
x = ((data[feature] - data_mean[feature])/data_std[feature]).values  # 预测,并还原结果。
data['y_pred'] = linearsvr.predict(x) * data_std['y'] + data_mean['y']
outputfile = '../tmp/new_reg_data_GM11_revenue_2.xls'  # SVR预测后保存的结果
data.to_excel(outputfile)

print('真实值与预测值分别为:\n',data[['y','y_pred']])

fig = data[['y','y_pred']].plot(subplots = True, style=['b-o','r-*'])  # 画出预测结果图
plt.show()
F:\Anaconda\envs\my\lib\site-packages\sklearn\svm\_base.py:1225: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  warnings.warn(
C:\Windows\Temp\ipykernel_63584\2363168573.py:6: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  data.to_excel(outputfile)


真实值与预测值分别为:
           y       y_pred
0     64.87    37.302597
1     99.75    83.928503
2     88.11    94.774134
3    106.07   106.434331
4    137.32   151.059897
5    188.14   188.205356
6    219.91   219.566685
7    271.91   230.340346
8    269.10   219.659959
9    300.55   300.550000
10   338.45   383.515443
11   408.86   463.222420
12   476.72   554.905404
13   838.99   691.434167
14   843.14   843.095183
15  1107.67  1088.163763
16  1399.16  1379.593271
17  1535.14  1536.823098
18  1579.68  1739.224541
19  2088.14  2086.022177
20      NaN  2188.120305
21      NaN  2539.228245