tjxj commited on
Commit
7fb7db7
1 Parent(s): f83439d
Files changed (7) hide show
  1. .streamlit/config.toml +2 -0
  2. LICENSE +201 -0
  3. LightGBM 调参.md +402 -0
  4. app.py +135 -0
  5. definitions.py +24 -0
  6. git.sh +3 -0
  7. requirements.txt +6 -0
.streamlit/config.toml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [deprecation]
2
+ showPyplotGlobalUse = False
LICENSE ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
LightGBM 调参.md ADDED
@@ -0,0 +1,402 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ **Step1. 学习率和估计器及其数目**
2
+
3
+ 不管怎么样,我们先把学习率先定一个较高的值,这里取 `learning_rate = 0.1`,其次确定估计器`boosting/boost/boosting_type`的类型,不过默认都会选`gbdt`。
4
+
5
+ 为了确定估计器的数目,也就是boosting迭代的次数,也可以说是残差树的数目,参数名为`n_estimators/num_iterations/num_round/num_boost_round`。我们可以先将该参数设成一个较大的数,然后在cv结果中查看最优的迭代次数,具体如代码。
6
+
7
+ 在这之前,我们必须给其他重要的参数一个初始值。初始值的意义不大,只是为了方便确定其他参数。下面先给定一下初始值:
8
+
9
+ 以下参数根据具体项目要求定:
10
+
11
+ ```
12
+ 'boosting_type'/'boosting': 'gbdt'
13
+ 'objective': 'regression'
14
+ 'metric': 'rmse'
15
+ ```
16
+
17
+ 以下参数我选择的初始值,你可以根据自己的情况来选择:
18
+
19
+ ```
20
+ 'max_depth': 6 ### 根据问题来定咯,由于我的数据集不是很大,所以选择了一个适中的值,其实4-10都无所谓。
21
+ 'num_leaves': 50 ### 由于lightGBM是leaves_wise生长,官方说法是要小于2^max_depth
22
+ 'subsample'/'bagging_fraction':0.8 ### 数据采样
23
+ 'colsample_bytree'/'feature_fraction': 0.8 ### 特征采样
24
+ ```
25
+
26
+ 下面我是用LightGBM的cv函数进行演示:
27
+
28
+ ```
29
+ params = {
30
+ 'boosting_type': 'gbdt',
31
+ 'objective': 'regression',
32
+
33
+ 'learning_rate': 0.1,
34
+ 'num_leaves': 50,
35
+ 'max_depth': 6,
36
+
37
+ 'subsample': 0.8,
38
+ 'colsample_bytree': 0.8,
39
+ }
40
+ data_train = lgb.Dataset(df_train, y_train, silent=True)
41
+ cv_results = lgb.cv(
42
+ params, data_train, num_boost_round=1000, nfold=5, stratified=False, shuffle=True, metrics='rmse',
43
+ early_stopping_rounds=50, verbose_eval=50, show_stdv=True, seed=0)
44
+
45
+ print('best n_estimators:', len(cv_results['rmse-mean']))
46
+ print('best cv score:', cv_results['rmse-mean'][-1])
47
+ [50] cv_agg's rmse: 1.38497 + 0.0202823
48
+ best n_estimators: 43
49
+ best cv score: 1.3838664241
50
+ ```
51
+
52
+ 由于我的数据集不是很大,所以在学习率为0.1时,最优的迭代次数只有43。那么现在,我们就代入(0.1, 43)进入其他参数的tuning。但是还是建议,在硬件条件允许的条件下,学习率还是越小越好。
53
+
54
+ **Step2. max_depth 和 num_leaves**
55
+
56
+ 这是提高精确度的最重要的参数。
57
+
58
+ `max_depth` :设置树深度,深度越大可能过拟合
59
+
60
+ `num_leaves`:因为 LightGBM 使用的是 leaf-wise 的算法,因此在调节树的复杂程度时,使用的是 num_leaves 而不是 max_depth。大致换算关系:num_leaves = 2^(max_depth),但是它的值的设置应该小于 2^(max_depth),否则可能会导致过拟合。
61
+
62
+ 我们也可以同时调节这两个参数,对于这两个参数调优,我们先粗调,再细调:
63
+
64
+ 这里我们引入`sklearn`里的`GridSearchCV()`函数进行搜索。不知道怎的,这个函数特别耗内存,特别耗时间,特别耗精力。
65
+
66
+ ```
67
+ from sklearn.model_selection import GridSearchCV
68
+ ### 我们可以创建lgb的sklearn模型,使用上面选择的(学习率,评估器数目)
69
+ model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=50,
70
+ learning_rate=0.1, n_estimators=43, max_depth=6,
71
+ metric='rmse', bagging_fraction = 0.8,feature_fraction = 0.8)
72
+
73
+ params_test1={
74
+ 'max_depth': range(3,8,2),
75
+ 'num_leaves':range(50, 170, 30)
76
+ }
77
+ gsearch1 = GridSearchCV(estimator=model_lgb, param_grid=params_test1, scoring='neg_mean_squared_error', cv=5, verbose=1, n_jobs=4)
78
+ gsearch1.fit(df_train, y_train)
79
+ gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_
80
+ Fitting 5 folds for each of 12 candidates, totalling 60 fits
81
+
82
+
83
+ [Parallel(n_jobs=4)]: Done 42 tasks | elapsed: 2.0min
84
+ [Parallel(n_jobs=4)]: Done 60 out of 60 | elapsed: 3.1min finished
85
+
86
+
87
+ ([mean: -1.88629, std: 0.13750, params: {'max_depth': 3, 'num_leaves': 50},
88
+ mean: -1.88629, std: 0.13750, params: {'max_depth': 3, 'num_leaves': 80},
89
+ mean: -1.88629, std: 0.13750, params: {'max_depth': 3, 'num_leaves': 110},
90
+ mean: -1.88629, std: 0.13750, params: {'max_depth': 3, 'num_leaves': 140},
91
+ mean: -1.86917, std: 0.12590, params: {'max_depth': 5, 'num_leaves': 50},
92
+ mean: -1.86917, std: 0.12590, params: {'max_depth': 5, 'num_leaves': 80},
93
+ mean: -1.86917, std: 0.12590, params: {'max_depth': 5, 'num_leaves': 110},
94
+ mean: -1.86917, std: 0.12590, params: {'max_depth': 5, 'num_leaves': 140},
95
+ mean: -1.89254, std: 0.10904, params: {'max_depth': 7, 'num_leaves': 50},
96
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 80},
97
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 110},
98
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 140}],
99
+ {'max_depth': 7, 'num_leaves': 80},
100
+ -1.8602436718814157)
101
+ ```
102
+
103
+ 这里,我们运行了12个参数组合,得到的最优解是在max_depth为7,num_leaves为80的情况下,分数为-1.860。
104
+
105
+ 这里必须说一下,sklearn模型评估里的scoring参数都是采用的**higher return values are better than lower return values(较高的返回值优于较低的返回值)**。
106
+
107
+ 但是,我采用的metric策略采用的是均方误差(rmse),越低越好,所以sklearn就提供了`neg_mean_squared_erro`参数,也就是返回metric的负数,所以就均方差来说,也就变成负数越大越好了。
108
+
109
+ 所以,可以看到,最优解的分数为-1.860,转化为均方差为np.sqrt(-(-1.860)) = 1.3639,明显比step1的分数要好很多。
110
+
111
+ 至此,我们将我们这步得到的最优解代入第三步。其实,我这里只进行了粗调,如果要得到更好的效果,可以将max_depth在7附近多取几个值,num_leaves在80附近多取几个值。千万不要怕麻烦,虽然这确实很麻烦。
112
+
113
+ ```
114
+ params_test2={
115
+ 'max_depth': [6,7,8],
116
+ 'num_leaves':[68,74,80,86,92]
117
+ }
118
+
119
+ gsearch2 = GridSearchCV(estimator=model_lgb, param_grid=params_test2, scoring='neg_mean_squared_error', cv=5, verbose=1, n_jobs=4)
120
+ gsearch2.fit(df_train, y_train)
121
+ gsearch2.grid_scores_, gsearch2.best_params_, gsearch2.best_score_
122
+ Fitting 5 folds for each of 15 candidates, totalling 75 fits
123
+
124
+
125
+ [Parallel(n_jobs=4)]: Done 42 tasks | elapsed: 2.8min
126
+ [Parallel(n_jobs=4)]: Done 75 out of 75 | elapsed: 5.1min finished
127
+
128
+
129
+ ([mean: -1.87506, std: 0.11369, params: {'max_depth': 6, 'num_leaves': 68},
130
+ mean: -1.87506, std: 0.11369, params: {'max_depth': 6, 'num_leaves': 74},
131
+ mean: -1.87506, std: 0.11369, params: {'max_depth': 6, 'num_leaves': 80},
132
+ mean: -1.87506, std: 0.11369, params: {'max_depth': 6, 'num_leaves': 86},
133
+ mean: -1.87506, std: 0.11369, params: {'max_depth': 6, 'num_leaves': 92},
134
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 68},
135
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 74},
136
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 80},
137
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 86},
138
+ mean: -1.86024, std: 0.11364, params: {'max_depth': 7, 'num_leaves': 92},
139
+ mean: -1.88197, std: 0.11295, params: {'max_depth': 8, 'num_leaves': 68},
140
+ mean: -1.89117, std: 0.12686, params: {'max_depth': 8, 'num_leaves': 74},
141
+ mean: -1.86390, std: 0.12259, params: {'max_depth': 8, 'num_leaves': 80},
142
+ mean: -1.86733, std: 0.12159, params: {'max_depth': 8, 'num_leaves': 86},
143
+ mean: -1.86665, std: 0.12174, params: {'max_depth': 8, 'num_leaves': 92}],
144
+ {'max_depth': 7, 'num_leaves': 68},
145
+ -1.8602436718814157)
146
+ ```
147
+
148
+ 可见最大深度7是没问题的,但是看细节的话,发现在最大深度为7的情况下,叶结点的数量对分数并没有影响。
149
+
150
+ **Step3: min_data_in_leaf 和 min_sum_hessian_in_leaf**
151
+
152
+ 说到这里,就该降低过拟合了。
153
+
154
+ `min_data_in_leaf` 是一个很重要的参数, 也叫min_child_samples,它的值取决于训练数据的样本个树和num_leaves. 将其设置的较大可以避免生成一个过深的树, 但有可能导致欠拟合。
155
+
156
+ `min_sum_hessian_in_leaf`:也叫min_child_weight,使一个结点分裂的最小海森值之和,真拗口(Minimum sum of hessians in one leaf to allow a split. Higher values potentially decrease overfitting)
157
+
158
+ 我们采用跟上面相同的方法进行:
159
+
160
+ ```
161
+ params_test3={
162
+ 'min_child_samples': [18, 19, 20, 21, 22],
163
+ 'min_child_weight':[0.001, 0.002]
164
+ }
165
+ model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=80,
166
+ learning_rate=0.1, n_estimators=43, max_depth=7,
167
+ metric='rmse', bagging_fraction = 0.8, feature_fraction = 0.8)
168
+ gsearch3 = GridSearchCV(estimator=model_lgb, param_grid=params_test3, scoring='neg_mean_squared_error', cv=5, verbose=1, n_jobs=4)
169
+ gsearch3.fit(df_train, y_train)
170
+ gsearch3.grid_scores_, gsearch3.best_params_, gsearch3.best_score_
171
+ Fitting 5 folds for each of 10 candidates, totalling 50 fits
172
+
173
+
174
+ [Parallel(n_jobs=4)]: Done 42 tasks | elapsed: 2.9min
175
+ [Parallel(n_jobs=4)]: Done 50 out of 50 | elapsed: 3.3min finished
176
+
177
+
178
+ ([mean: -1.88057, std: 0.13948, params: {'min_child_samples': 18, 'min_child_weight': 0.001},
179
+ mean: -1.88057, std: 0.13948, params: {'min_child_samples': 18, 'min_child_weight': 0.002},
180
+ mean: -1.88365, std: 0.13650, params: {'min_child_samples': 19, 'min_child_weight': 0.001},
181
+ mean: -1.88365, std: 0.13650, params: {'min_child_samples': 19, 'min_child_weight': 0.002},
182
+ mean: -1.86024, std: 0.11364, params: {'min_child_samples': 20, 'min_child_weight': 0.001},
183
+ mean: -1.86024, std: 0.11364, params: {'min_child_samples': 20, 'min_child_weight': 0.002},
184
+ mean: -1.86980, std: 0.14251, params: {'min_child_samples': 21, 'min_child_weight': 0.001},
185
+ mean: -1.86980, std: 0.14251, params: {'min_child_samples': 21, 'min_child_weight': 0.002},
186
+ mean: -1.86750, std: 0.13898, params: {'min_child_samples': 22, 'min_child_weight': 0.001},
187
+ mean: -1.86750, std: 0.13898, params: {'min_child_samples': 22, 'min_child_weight': 0.002}],
188
+ {'min_child_samples': 20, 'min_child_weight': 0.001},
189
+ -1.8602436718814157)
190
+ ```
191
+
192
+ 这是我经过粗调后细调的结果,可以看到,min_data_in_leaf的最优值为20,而min_sum_hessian_in_leaf对最后的值几乎没有影响。且这里调参之后,最后的值没有进行优化,说明之前的默认值即为20,0.001。
193
+
194
+ **Step4: feature_fraction 和 bagging_fraction**
195
+
196
+ 这两个参数都是为了降低过拟合的。
197
+
198
+ feature_fraction参数来进行特征的子抽样。这个参数可以用来防止过拟合及提高训练速度。
199
+
200
+ bagging_fraction+bagging_freq参数必须同时设置,bagging_fraction相当于subsample样本采样,可以使bagging更快的运行,同时也可以降拟合。bagging_freq默认0,表示bagging的频率,0意味着没有使用bagging,k意味着每k轮迭代进行一次bagging。
201
+
202
+ 不同的参数,同样的方法。
203
+
204
+ ```
205
+ params_test4={
206
+ 'feature_fraction': [0.5, 0.6, 0.7, 0.8, 0.9],
207
+ 'bagging_fraction': [0.6, 0.7, 0.8, 0.9, 1.0]
208
+ }
209
+ model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=80,
210
+ learning_rate=0.1, n_estimators=43, max_depth=7,
211
+ metric='rmse', bagging_freq = 5, min_child_samples=20)
212
+ gsearch4 = GridSearchCV(estimator=model_lgb, param_grid=params_test4, scoring='neg_mean_squared_error', cv=5, verbose=1, n_jobs=4)
213
+ gsearch4.fit(df_train, y_train)
214
+ gsearch4.grid_scores_, gsearch4.best_params_, gsearch4.best_score_
215
+ Fitting 5 folds for each of 25 candidates, totalling 125 fits
216
+
217
+
218
+ [Parallel(n_jobs=4)]: Done 42 tasks | elapsed: 2.6min
219
+ [Parallel(n_jobs=4)]: Done 125 out of 125 | elapsed: 7.1min finished
220
+
221
+
222
+ ([mean: -1.90447, std: 0.15841, params: {'bagging_fraction': 0.6, 'feature_fraction': 0.5},
223
+ mean: -1.90846, std: 0.13925, params: {'bagging_fraction': 0.6, 'feature_fraction': 0.6},
224
+ mean: -1.91695, std: 0.14121, params: {'bagging_fraction': 0.6, 'feature_fraction': 0.7},
225
+ mean: -1.90115, std: 0.12625, params: {'bagging_fraction': 0.6, 'feature_fraction': 0.8},
226
+ mean: -1.92586, std: 0.15220, params: {'bagging_fraction': 0.6, 'feature_fraction': 0.9},
227
+ mean: -1.88031, std: 0.17157, params: {'bagging_fraction': 0.7, 'feature_fraction': 0.5},
228
+ mean: -1.89513, std: 0.13718, params: {'bagging_fraction': 0.7, 'feature_fraction': 0.6},
229
+ mean: -1.88845, std: 0.13864, params: {'bagging_fraction': 0.7, 'feature_fraction': 0.7},
230
+ mean: -1.89297, std: 0.12374, params: {'bagging_fraction': 0.7, 'feature_fraction': 0.8},
231
+ mean: -1.89432, std: 0.14353, params: {'bagging_fraction': 0.7, 'feature_fraction': 0.9},
232
+ mean: -1.88088, std: 0.14247, params: {'bagging_fraction': 0.8, 'feature_fraction': 0.5},
233
+ mean: -1.90080, std: 0.13174, params: {'bagging_fraction': 0.8, 'feature_fraction': 0.6},
234
+ mean: -1.88364, std: 0.14732, params: {'bagging_fraction': 0.8, 'feature_fraction': 0.7},
235
+ mean: -1.88987, std: 0.13344, params: {'bagging_fraction': 0.8, 'feature_fraction': 0.8},
236
+ mean: -1.87752, std: 0.14802, params: {'bagging_fraction': 0.8, 'feature_fraction': 0.9},
237
+ mean: -1.88348, std: 0.13925, params: {'bagging_fraction': 0.9, 'feature_fraction': 0.5},
238
+ mean: -1.87472, std: 0.13301, params: {'bagging_fraction': 0.9, 'feature_fraction': 0.6},
239
+ mean: -1.88656, std: 0.12241, params: {'bagging_fraction': 0.9, 'feature_fraction': 0.7},
240
+ mean: -1.89029, std: 0.10776, params: {'bagging_fraction': 0.9, 'feature_fraction': 0.8},
241
+ mean: -1.88719, std: 0.11915, params: {'bagging_fraction': 0.9, 'feature_fraction': 0.9},
242
+ mean: -1.86170, std: 0.12544, params: {'bagging_fraction': 1.0, 'feature_fraction': 0.5},
243
+ mean: -1.87334, std: 0.13099, params: {'bagging_fraction': 1.0, 'feature_fraction': 0.6},
244
+ mean: -1.85412, std: 0.12698, params: {'bagging_fraction': 1.0, 'feature_fraction': 0.7},
245
+ mean: -1.86024, std: 0.11364, params: {'bagging_fraction': 1.0, 'feature_fraction': 0.8},
246
+ mean: -1.87266, std: 0.12271, params: {'bagging_fraction': 1.0, 'feature_fraction': 0.9}],
247
+ {'bagging_fraction': 1.0, 'feature_fraction': 0.7},
248
+ -1.8541224387666373)
249
+ ```
250
+
251
+ 从这里可以看出来,bagging_feaction和feature_fraction的理想值分别是1.0和0.7,一个很重要原因就是,我的样本数量比较小(4000+),但是特征数量很多(1000+)。所以,这里我们取更小的步长,对feature_fraction进行更细致的取值。
252
+
253
+ ```
254
+ params_test5={
255
+ 'feature_fraction': [0.62, 0.65, 0.68, 0.7, 0.72, 0.75, 0.78 ]
256
+ }
257
+ model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=80,
258
+ learning_rate=0.1, n_estimators=43, max_depth=7,
259
+ metric='rmse', min_child_samples=20)
260
+ gsearch5 = GridSearchCV(estimator=model_lgb, param_grid=params_test5, scoring='neg_mean_squared_error', cv=5, verbose=1, n_jobs=4)
261
+ gsearch5.fit(df_train, y_train)
262
+ gsearch5.grid_scores_, gsearch5.best_params_, gsearch5.best_score_
263
+ Fitting 5 folds for each of 7 candidates, totalling 35 fits
264
+
265
+
266
+ [Parallel(n_jobs=4)]: Done 35 out of 35 | elapsed: 2.3min finished
267
+
268
+
269
+ ([mean: -1.86696, std: 0.12658, params: {'feature_fraction': 0.62},
270
+ mean: -1.88337, std: 0.13215, params: {'feature_fraction': 0.65},
271
+ mean: -1.87282, std: 0.13193, params: {'feature_fraction': 0.68},
272
+ mean: -1.85412, std: 0.12698, params: {'feature_fraction': 0.7},
273
+ mean: -1.88235, std: 0.12682, params: {'feature_fraction': 0.72},
274
+ mean: -1.86329, std: 0.12757, params: {'feature_fraction': 0.75},
275
+ mean: -1.87943, std: 0.12107, params: {'feature_fraction': 0.78}],
276
+ {'feature_fraction': 0.7},
277
+ -1.8541224387666373)
278
+ ```
279
+
280
+ 好吧,feature_fraction就是0.7了
281
+
282
+ **Step5: 正则化参数**
283
+
284
+ 正则化参数lambda_l1(reg_alpha), lambda_l2(reg_lambda),毫无疑问,是降低过拟合的,两者分别对应l1正则化和l2正则化。我们也来尝试一下使用这两个参数。
285
+
286
+ ```
287
+ params_test6={
288
+ 'reg_alpha': [0, 0.001, 0.01, 0.03, 0.08, 0.3, 0.5],
289
+ 'reg_lambda': [0, 0.001, 0.01, 0.03, 0.08, 0.3, 0.5]
290
+ }
291
+ model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=80,
292
+ learning_rate=0.b1, n_estimators=43, max_depth=7,
293
+ metric='rmse', min_child_samples=20, feature_fraction=0.7)
294
+ gsearch6 = GridSearchCV(estimator=model_lgb, param_grid=params_test6, scoring='neg_mean_squared_error', cv=5, verbose=1, n_jobs=4)
295
+ gsearch6.fit(df_train, y_train)
296
+ gsearch6.grid_scores_, gsearch6.best_params_, gsearch6.best_score_
297
+ Fitting 5 folds for each of 49 candidates, totalling 245 fits
298
+
299
+
300
+ [Parallel(n_jobs=4)]: Done 42 tasks | elapsed: 2.8min
301
+ [Parallel(n_jobs=4)]: Done 192 tasks | elapsed: 10.6min
302
+ [Parallel(n_jobs=4)]: Done 245 out of 245 | elapsed: 13.3min finished
303
+
304
+
305
+ ([mean: -1.85412, std: 0.12698, params: {'reg_alpha': 0, 'reg_lambda': 0},
306
+ mean: -1.85990, std: 0.13296, params: {'reg_alpha': 0, 'reg_lambda': 0.001},
307
+ mean: -1.86367, std: 0.13634, params: {'reg_alpha': 0, 'reg_lambda': 0.01},
308
+ mean: -1.86787, std: 0.13881, params: {'reg_alpha': 0, 'reg_lambda': 0.03},
309
+ mean: -1.87099, std: 0.12476, params: {'reg_alpha': 0, 'reg_lambda': 0.08},
310
+ mean: -1.87670, std: 0.11849, params: {'reg_alpha': 0, 'reg_lambda': 0.3},
311
+ mean: -1.88278, std: 0.13064, params: {'reg_alpha': 0, 'reg_lambda': 0.5},
312
+ mean: -1.86190, std: 0.13613, params: {'reg_alpha': 0.001, 'reg_lambda': 0},
313
+ mean: -1.86190, std: 0.13613, params: {'reg_alpha': 0.001, 'reg_lambda': 0.001},
314
+ mean: -1.86515, std: 0.14116, params: {'reg_alpha': 0.001, 'reg_lambda': 0.01},
315
+ mean: -1.86908, std: 0.13668, params: {'reg_alpha': 0.001, 'reg_lambda': 0.03},
316
+ mean: -1.86852, std: 0.12289, params: {'reg_alpha': 0.001, 'reg_lambda': 0.08},
317
+ mean: -1.88076, std: 0.11710, params: {'reg_alpha': 0.001, 'reg_lambda': 0.3},
318
+ mean: -1.88278, std: 0.13064, params: {'reg_alpha': 0.001, 'reg_lambda': 0.5},
319
+ mean: -1.87480, std: 0.13889, params: {'reg_alpha': 0.01, 'reg_lambda': 0},
320
+ mean: -1.87284, std: 0.14138, params: {'reg_alpha': 0.01, 'reg_lambda': 0.001},
321
+ mean: -1.86030, std: 0.13332, params: {'reg_alpha': 0.01, 'reg_lambda': 0.01},
322
+ mean: -1.86695, std: 0.12587, params: {'reg_alpha': 0.01, 'reg_lambda': 0.03},
323
+ mean: -1.87415, std: 0.13100, params: {'reg_alpha': 0.01, 'reg_lambda': 0.08},
324
+ mean: -1.88543, std: 0.13195, params: {'reg_alpha': 0.01, 'reg_lambda': 0.3},
325
+ mean: -1.88076, std: 0.13502, params: {'reg_alpha': 0.01, 'reg_lambda': 0.5},
326
+ mean: -1.87729, std: 0.12533, params: {'reg_alpha': 0.03, 'reg_lambda': 0},
327
+ mean: -1.87435, std: 0.12034, params: {'reg_alpha': 0.03, 'reg_lambda': 0.001},
328
+ mean: -1.87513, std: 0.12579, params: {'reg_alpha': 0.03, 'reg_lambda': 0.01},
329
+ mean: -1.88116, std: 0.12218, params: {'reg_alpha': 0.03, 'reg_lambda': 0.03},
330
+ mean: -1.88052, std: 0.13585, params: {'reg_alpha': 0.03, 'reg_lambda': 0.08},
331
+ mean: -1.87565, std: 0.12200, params: {'reg_alpha': 0.03, 'reg_lambda': 0.3},
332
+ mean: -1.87935, std: 0.13817, params: {'reg_alpha': 0.03, 'reg_lambda': 0.5},
333
+ mean: -1.87774, std: 0.12477, params: {'reg_alpha': 0.08, 'reg_lambda': 0},
334
+ mean: -1.87774, std: 0.12477, params: {'reg_alpha': 0.08, 'reg_lambda': 0.001},
335
+ mean: -1.87911, std: 0.12027, params: {'reg_alpha': 0.08, 'reg_lambda': 0.01},
336
+ mean: -1.86978, std: 0.12478, params: {'reg_alpha': 0.08, 'reg_lambda': 0.03},
337
+ mean: -1.87217, std: 0.12159, params: {'reg_alpha': 0.08, 'reg_lambda': 0.08},
338
+ mean: -1.87573, std: 0.14137, params: {'reg_alpha': 0.08, 'reg_lambda': 0.3},
339
+ mean: -1.85969, std: 0.13109, params: {'reg_alpha': 0.08, 'reg_lambda': 0.5},
340
+ mean: -1.87632, std: 0.12398, params: {'reg_alpha': 0.3, 'reg_lambda': 0},
341
+ mean: -1.86995, std: 0.12651, params: {'reg_alpha': 0.3, 'reg_lambda': 0.001},
342
+ mean: -1.86380, std: 0.12793, params: {'reg_alpha': 0.3, 'reg_lambda': 0.01},
343
+ mean: -1.87577, std: 0.13002, params: {'reg_alpha': 0.3, 'reg_lambda': 0.03},
344
+ mean: -1.87402, std: 0.13496, params: {'reg_alpha': 0.3, 'reg_lambda': 0.08},
345
+ mean: -1.87032, std: 0.12504, params: {'reg_alpha': 0.3, 'reg_lambda': 0.3},
346
+ mean: -1.88329, std: 0.13237, params: {'reg_alpha': 0.3, 'reg_lambda': 0.5},
347
+ mean: -1.87196, std: 0.13099, params: {'reg_alpha': 0.5, 'reg_lambda': 0},
348
+ mean: -1.87196, std: 0.13099, params: {'reg_alpha': 0.5, 'reg_lambda': 0.001},
349
+ mean: -1.88222, std: 0.14735, params: {'reg_alpha': 0.5, 'reg_lambda': 0.01},
350
+ mean: -1.86618, std: 0.14006, params: {'reg_alpha': 0.5, 'reg_lambda': 0.03},
351
+ mean: -1.88579, std: 0.12398, params: {'reg_alpha': 0.5, 'reg_lambda': 0.08},
352
+ mean: -1.88297, std: 0.12307, params: {'reg_alpha': 0.5, 'reg_lambda': 0.3},
353
+ mean: -1.88148, std: 0.12622, params: {'reg_alpha': 0.5, 'reg_lambda': 0.5}],
354
+ {'reg_alpha': 0, 'reg_lambda': 0},
355
+ -1.8541224387666373)
356
+ ```
357
+
358
+ 哈哈,看来我多此一举了。
359
+
360
+ **step6: 降低learning_rate**
361
+
362
+ 之前使用较高的学习速率是因为可以让收敛更快,但是准确度肯定没有细水长流来的好。最后,我们使用较低的学习速率,以及使用更多的决策树n_estimators来训练数据,看能不能可以进一步的优化分数。
363
+
364
+ 我们可以用回lightGBM的cv函数了 ,我们代入之前优化好的参数。
365
+
366
+ ```
367
+ params = {
368
+ 'boosting_type': 'gbdt',
369
+ 'objective': 'regression',
370
+
371
+ 'learning_rate': 0.005,
372
+ 'num_leaves': 80,
373
+ 'max_depth': 7,
374
+ 'min_data_in_leaf': 20,
375
+
376
+ 'subsample': 1,
377
+ 'colsample_bytree': 0.7,
378
+ }
379
+
380
+ data_train = lgb.Dataset(df_train, y_train, silent=True)
381
+ cv_results = lgb.cv(
382
+ params, data_train, num_boost_round=10000, nfold=5, stratified=False, shuffle=True, metrics='rmse',
383
+ early_stopping_rounds=50, verbose_eval=100, show_stdv=True)
384
+
385
+ print('best n_estimators:', len(cv_results['rmse-mean']))
386
+ print('best cv score:', cv_results['rmse-mean'][-1])
387
+ [100] cv_agg's rmse: 1.52939 + 0.0261756
388
+ [200] cv_agg's rmse: 1.43535 + 0.0187243
389
+ [300] cv_agg's rmse: 1.39584 + 0.0157521
390
+ [400] cv_agg's rmse: 1.37935 + 0.0157429
391
+ [500] cv_agg's rmse: 1.37313 + 0.0164503
392
+ [600] cv_agg's rmse: 1.37081 + 0.0172752
393
+ [700] cv_agg's rmse: 1.36942 + 0.0177888
394
+ [800] cv_agg's rmse: 1.36854 + 0.0180575
395
+ [900] cv_agg's rmse: 1.36817 + 0.0188776
396
+ [1000] cv_agg's rmse: 1.36796 + 0.0190279
397
+ [1100] cv_agg's rmse: 1.36783 + 0.0195969
398
+ best n_estimators: 1079
399
+ best cv score: 1.36772351783
400
+ ```
401
+
402
+ 这就是一个大概过程吧,其实也有更高级的方法,但是这种基本的对于GBM模型的调参方法也是需要了解的吧。如有问题,请多指教。
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from definitions import *
2
+
3
+ st.set_option('deprecation.showPyplotGlobalUse', False)
4
+ st.sidebar.subheader("请选择模型参数:sunglasses:")
5
+
6
+ num_leaves = st.sidebar.slider(label = 'num_leaves', min_value = 4.0,
7
+ max_value = 16.0 ,
8
+ value = 10.0,
9
+ step = 0.1)
10
+
11
+ max_depth = st.sidebar.slider(label = 'max_depth', min_value = 8,
12
+ max_value = 15,
13
+ value = 10,
14
+ step = 1)
15
+
16
+ min_data_in_leaf = st.sidebar.slider(label = 'min_data_in_leaf', min_value = 8,
17
+ max_value = 15,
18
+ value = 10,
19
+ step = 1)
20
+
21
+ feature_fraction = st.sidebar.slider(label = 'feature_fraction', min_value = 0.0,
22
+ max_value = 1.0 ,
23
+ value = 0.3,
24
+ step = 0.1)
25
+
26
+ lambda_l1 = st.sidebar.slider(label = 'lambda_l1', min_value = 0.000,
27
+ max_value = 1.000 ,
28
+ value = 0.500,
29
+ step = 0.001)
30
+
31
+ lambda_l2 = st.sidebar.slider(label = 'lambda_l2', min_value = 1,
32
+ max_value = 72,
33
+ value = 36,
34
+ step = 1)
35
+
36
+ min_split_gain = st.sidebar.slider(label = 'min_split_gain', min_value = 6,
37
+ max_value = 289 ,
38
+ value = 144,
39
+ step = 1)
40
+
41
+ top_rate = st.sidebar.slider(label = 'top_rate', min_value = 0.0,
42
+ max_value = 1.0 ,
43
+ value = 0.3,
44
+ step = 0.1)
45
+
46
+ other_rate = st.sidebar.slider(label = 'other_rate', min_value = 0.0,
47
+ max_value = 1.0 ,
48
+ value = 0.3,
49
+ step = 0.1)
50
+
51
+ min_data_per_group = st.sidebar.slider(label = 'min_data_per_group', min_value = 6,
52
+ max_value = 289 ,
53
+ value = 32,
54
+ step = 1)
55
+
56
+ max_cat_threshold = st.sidebar.slider(label = 'max_cat_threshold', min_value = 6,
57
+ max_value = 289 ,
58
+ value = 32,
59
+ step = 1)
60
+
61
+ learning_rate = st.sidebar.slider(label = 'learning_rate', min_value = 8.0,
62
+ max_value = 15.0,
63
+ value = 10.5,
64
+ step = 0.1)
65
+
66
+ num_leaves = st.sidebar.slider(label = 'num_leaves', min_value = 6,
67
+ max_value = 289 ,
68
+ value = 31,
69
+ step = 1)
70
+
71
+ min_gain_to_split = st.sidebar.slider(label = 'min_gain_to_split', min_value = 0.0,
72
+ max_value = 15.0,
73
+ value = 0.0,
74
+ step = 0.1)
75
+
76
+
77
+ max_bin = st.sidebar.slider(label = 'max_bin', min_value = 6,
78
+ max_value = 289 ,
79
+ value = 255,
80
+ step = 1)
81
+
82
+ num_iterations = st.sidebar.slider(label = 'num_iterations', min_value = 8,
83
+ max_value = 15,
84
+ value = 10,
85
+ step = 1)
86
+
87
+ st.title('LightGBM-parameter-tuning-with-streamlit')
88
+
89
+
90
+ # 加载数据
91
+ breast_cancer = load_breast_cancer()
92
+ data = breast_cancer.data
93
+ target = breast_cancer.target
94
+
95
+ # 划分训练数据和测试数据
96
+ X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2)
97
+
98
+ # 转换为Dataset数据格式
99
+ lgb_train = lgb.Dataset(X_train, y_train)
100
+ lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
101
+
102
+ # 模型训练
103
+ params = {'num_leaves': num_leaves, 'max_depth': max_depth,
104
+ 'min_data_in_leaf': min_data_in_leaf,
105
+ 'feature_fraction': feature_fraction,
106
+ 'lambda_l1': lambda_l1, 'lambda_l2': lambda_l2,
107
+ 'min_split_gain': min_split_gain, 'top_rate': top_rate,
108
+ 'other_rate': other_rate, 'min_data_per_group': min_data_per_group,
109
+ 'max_cat_threshold': max_cat_threshold,
110
+ 'learning_rate':learning_rate,'num_leaves':num_leaves,'min_gain_to_split':min_gain_to_split,
111
+ 'max_bin':max_bin,'num_iterations':num_iterations
112
+ }
113
+
114
+ gbm = lgb.train(params, lgb_train, num_boost_round=2000, valid_sets=lgb_eval, early_stopping_rounds=500)
115
+ lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
116
+ probs = gbm.predict(X_test, num_iteration=gbm.best_iteration) # 输出的是概率结果
117
+
118
+ fpr, tpr, thresholds = roc_curve(y_test, probs)
119
+ st.write('------------------------------------')
120
+ st.write('Confusion Matrix:')
121
+
122
+ st.write(confusion_matrix(y_test, np.where(probs > 0.5, 1, 0)))
123
+
124
+
125
+
126
+ st.write('------------------------------------')
127
+ st.write('Classification Report:')
128
+ report = classification_report(y_test, np.where(probs > 0.5, 1, 0), output_dict=True)
129
+ report_matrix = pd.DataFrame(report).transpose()
130
+ st.dataframe(report_matrix)
131
+
132
+ st.write('------------------------------------')
133
+ st.write('ROC:')
134
+
135
+ plot_roc(fpr, tpr)
definitions.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import streamlit as st
3
+ import numpy as np
4
+ from sklearn.model_selection import train_test_split
5
+ from sklearn.datasets import load_breast_cancer
6
+ from sklearn.metrics import roc_auc_score,roc_curve,auc,accuracy_score,classification_report,confusion_matrix,precision_recall_curve
7
+ import lightgbm as lgb
8
+ import matplotlib.pyplot as plt
9
+ import warnings
10
+ warnings.filterwarnings('ignore')
11
+
12
+ def plot_roc(fpr, tpr, label=None):
13
+ roc_auc = auc(fpr, tpr)
14
+ plt.title('Receiver Operating Characteristic')
15
+ plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
16
+ plt.legend(loc = 'lower right')
17
+ plt.plot([0, 1], [0, 1],'r--')
18
+ plt.xlim([0, 1])
19
+ plt.ylim([0, 1])
20
+ plt.ylabel('True Positive Rate')
21
+ plt.xlabel('False Positive Rate')
22
+ plt.show()
23
+ st.pyplot()
24
+
git.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ git add .
2
+ git commit -m "1.0"
3
+ git push
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ pandas 1.3.1
2
+ streamlit 1.8.1
3
+ numpy 1.20.3
4
+ sklearn 0.0
5
+ lightgbm 3.3.2
6
+ matplotlib 3.4.2