excel学习库

excel表格_excel函数公式大全_execl从入门到精通

Python中随机森林(RF)回归与变量重要性影响程度分析的代码实现


本文分为两部分,第一部分为代码的分段讲解,第二部分为完整代码。

1 代码分段讲解

1.1 模块与数据准备

pydotgraphvizAnaconda

1import
2importas
3importas
4importas
5importas
6fromimport
7fromimport
8fromimport
9fromimport

接下来,我们将代码接下来需要用的主要变量加以定义。这一部分大家先不用过于在意,浏览一下继续向下看即可;待到对应的变量需要运用时我们自然会理解其具体含义。

1'G:/CropYield/03_DL/00_Data/AllDataAll_Train.csv'
2'G:/CropYield/03_DL/00_Data/AllDataAll_Test.csv'
3'G:/CropYield/03_DL/05_NewML/ParameterResult_ML.xlsx'
4'G:/CropYield/03_DL/05_NewML/tree.dot'
5'G:/CropYield/03_DL/05_NewML/tree.png'
6
744
81230

接下来,我们需要导入输入数据。

需要注意

  • 初始数据划分训练集与测试集

  • 类别变量的独热编码(One-hot Encoding)

.csv

 1# Data import
2
3
4
5
6
7
8
9
10
11
12
13
14

150
160

1.2 特征与标签分离

特征与标签,换句话说其实就是自变量与因变量。我们要将训练集与测试集中对应的特征与标签分别分离开来。

 1# Separate independent and dependent variables
2
3'Yield'
4'ID''Yield'1
5
6
7
8'Yield'
9'ID''Yield'1
10

drop'Yield''ID'train_X_column_name

1.3 RF模型构建、训练与预测

n_estimators

1# Build RF regression model
2
3200
4
5
6# Predict test set data
7
8
9

RandomForestRegressorn_estimatorsrandom_stateBaggingBootstrapfitpredict

1.4 预测图像绘制、精度衡量指标计算与保存

 1# Draw test plot
2
31
4
5'equal'
6
7'True Values'
8'Predictions'
9010000
10
11
12
13False
14
152
16
1730
18'Prediction Error'
19'Count'
20False

以上两幅图的绘图结果如下所示。

皮尔逊相关系数、决定系数与RMSE

 1# Verify the accuracy
2
3
4
50.5
6'Pearson correlation coefficient is {0}, and RMSE is {1}.'0
7
8
9# Save key parameters
10
11
12
130
14
15
160
17forin
18"excel_write_sheet.cell(max_row+1,i+1).value=excel_write_content[i]"
19

1.5 决策树可视化

这一部分我们借助DOT这一图像描述语言,进行随机森林算法中决策树的绘制。

1# Draw decision tree visualizing plot
2
35
4
5True1
6
7

estimators_[5]0n_estimators

根节点

samples=151

BaggingBootstrapAggregationBaggingBootstrapAggregation

1.6 变量重要性分析

在这里,我们进行变量重要性的分析,并以图的形式进行可视化。

 1# Calculate the importance of variables
2
3
48
5forin
6lambda1True
73
8
9
10'vertical'
11'vertical'
12'Variable'
13'Importance'
14'Variable Importances'

得到图像如下所示。这里是由于我的特征数量(自变量数量)过多,大概有150多个,导致横坐标的标签(也就是自变量的名称)都重叠了;大家一般的自变量个数都不会太多,就不会有问题~

以上就是全部的代码分段介绍~

2 完整代码

  1# -*- coding: utf-8 -*-
2
3
4
5
6

7
8import
9importas
10importas
11importas
12importas
13fromimport
14fromimport
15fromimport
16fromimport
17
18
19# Attention! Data Partition
20# Attention! One-Hot Encoding
21
22'G:/CropYield/03_DL/00_Data/AllDataAll_Train.csv'
23'G:/CropYield/03_DL/00_Data/AllDataAll_Test.csv'
24'G:/CropYield/03_DL/05_NewML/ParameterResult_ML.xlsx'
25'G:/CropYield/03_DL/05_NewML/tree.dot'
26'G:/CropYield/03_DL/05_NewML/tree.png'
27
2844
291230
30
31# Data import
32
33
34
35
36
37
38
39
40
41
42
43
44

450
460
47
48# Separate independent and dependent variables
49
50'Yield'
51'ID''Yield'1
52
53
54
55'Yield'
56'ID''Yield'1
57
58
59# Build RF regression model
60
61200
62
63
64# Predict test set data
65
66
67
68
69# Draw test plot
70
711
72
73'equal'
74
75'True Values'
76'Predictions'
77010000
78
79
80
81False
82
832
84
8530
86'Prediction Error'
87'Count'
88False
89
90# Verify the accuracy
91
92
93
940.5
95'Pearson correlation coefficient is {0}, and RMSE is {1}.'0
96
97
98# Save key parameters
99
100
101
1020
103
104
1050
106forin
107"excel_write_sheet.cell(max_row+1,i+1).value=excel_write_content[i]"
108
109
110# Draw decision tree visualizing plot
111
1125
113
114True1
115
116
117
118# Calculate the importance of variables
119
120
1218
122forin
123lambda1True
1243
125
126
127'vertical'
128'vertical'
129'Variable'
130'Importance'
131'Variable Importances'

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2024年12月    »
1
2345678
9101112131415
16171819202122
23242526272829
3031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
      友情链接