質問編集履歴
2
訂正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
PythonでのlightGBMの実装の際にevals_resultのエラー,XGBoost
|
body
CHANGED
File without changes
|
1
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,25 +4,72 @@
|
|
4
4
|
ですが,LightGBMのハイパーパラメータの設定の際にevals_resultで文法エラーとなります。
|
5
5
|
|
6
6
|
```python
|
7
|
+
#インポート
|
8
|
+
import lightgbm as lgb
|
9
|
+
from sklearn import datasets
|
10
|
+
from sklearn.model_selection import train_test_split
|
11
|
+
import pandas as pd
|
12
|
+
import numpy as np
|
13
|
+
from matplotlib import pyplot as plt
|
14
|
+
from sklearn.metrics import mean_squared_error
|
15
|
+
from sklearn.model_selection import GridSearchCV
|
16
|
+
|
17
|
+
#############################################################################################
|
18
|
+
|
19
|
+
# ファイルの読み込みおよびN/Aの削除
|
20
|
+
df9 = pd.read_csv(path1, header = 0, index_col = 0, encoding='shift-JIS')
|
21
|
+
df8 = df9.dropna()
|
22
|
+
|
23
|
+
# 分類するクラスの種類と数を確認
|
24
|
+
df8['evaluation'].value_counts()
|
25
|
+
|
26
|
+
# 訓練用とテスト用に7:3で分割
|
27
|
+
train_x = df8.drop(['evaluation'], axis=1)
|
28
|
+
train_y = df8['evaluation']
|
29
|
+
(train_x, test_x ,train_y, test_y) = train_test_split(train_x, train_y, test_size = 0.3)
|
30
|
+
|
31
|
+
|
32
|
+
# LightGBMにデータセットを登録
|
33
|
+
lgb_train = lgb.Dataset(train_x, train_y)
|
34
|
+
lgb_test = lgb.Dataset(test_x, test_y, reference=lgb_train)
|
35
|
+
|
36
|
+
|
7
37
|
# LightGBMのハイパーパラメータを設定
|
8
38
|
params = {'task': 'train', # タスクを訓練に設定
|
9
39
|
'boosting_type': 'gbdt', # GBDTを指定
|
10
40
|
'objective': 'multiclass', # 多クラス分類を指定
|
11
41
|
'metric': {'multi_logloss'}, # 多クラス分類の損失(誤差)
|
12
|
-
'num_class':
|
42
|
+
'num_class': 4, # クラスの数(irisデータセットが3個のクラスなので)
|
13
43
|
'learning_rate': 0.1, # 学習率
|
14
44
|
'num_leaves': 21, # ノードの数
|
15
45
|
'min_data_in_leaf': 3, # 決定木ノードの最小データ数
|
16
46
|
'num_iteration': 100} # 予測器(決定木)の数:イタレーション
|
17
47
|
|
48
|
+
|
49
|
+
# LightGBMで訓練する
|
18
50
|
lgb_results = {} # 学習の履歴を入れる入物
|
19
51
|
model = lgb.train(params=params, # ハイパーパラメータをセット
|
20
52
|
train_set=lgb_train, # 訓練データを訓練用にセット
|
21
53
|
valid_sets=[lgb_train, lgb_test], # 訓練データとテストデータをセット
|
22
54
|
valid_names=['Train', 'Test'], # データセットの名前をそれぞれ設定
|
23
55
|
num_boost_round=100, # 計算回数
|
24
|
-
early_stopping_rounds=10
|
56
|
+
early_stopping_rounds=10) # アーリーストッピング設定
|
25
|
-
evals_result=lgb_results) # 履歴を保存する
|
57
|
+
evals_result=lgb_results) # 履歴を保存する
|
58
|
+
|
59
|
+
# 結果を抽出する
|
60
|
+
loss_train = lgb_results['Train']['multi_logloss'] # 訓練誤差
|
61
|
+
loss_test = lgb_results['Test']['multi_logloss'] # 汎化誤差
|
62
|
+
best_iteration = model.best_iteration # 最良の予測器が得られたイタレーション数
|
63
|
+
print(best_iteration)
|
64
|
+
|
65
|
+
############################################################################################
|
66
|
+
|
67
|
+
# グラフ描画
|
68
|
+
import lightgbm as lgb
|
69
|
+
from sklearn import datasets
|
70
|
+
from sklearn.model_selection import train_test_split
|
71
|
+
import pandas as pd
|
72
|
+
from matplotlib import pyplot as plt
|
26
73
|
```
|
27
74
|
|
28
75
|
解決策をご存じの方いましたらご教示いただけますと幸いです。
|