質問編集履歴
1
a
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,3 +15,111 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
![イメージ説明](150cdfe9f97c2265a850f4e691420466.png)
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
'''
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
# --------------------------------------------------
|
26
|
+
|
27
|
+
# NOW,TMP_DIR = mpre2.mk_dir()
|
28
|
+
|
29
|
+
import datetime
|
30
|
+
|
31
|
+
NOW = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
|
32
|
+
|
33
|
+
TMP_DIR = '../models/'+NOW
|
34
|
+
|
35
|
+
gc.collect()
|
36
|
+
|
37
|
+
# --------------------------------------------------
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
va_period_list = [1, 2, 3]
|
42
|
+
|
43
|
+
for va_period in va_period_list:
|
44
|
+
|
45
|
+
print('i ================================================================== ',va_period)
|
46
|
+
|
47
|
+
is_tr = train_x['period'] < va_period
|
48
|
+
|
49
|
+
is_va = train_x['period'] == va_period
|
50
|
+
|
51
|
+
tr_x, va_x = train_x[is_tr], train_x[is_va]
|
52
|
+
|
53
|
+
tr_y, va_y = train_y[is_tr], train_y[is_va]
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
lgb_train = lgb.Dataset(tr_x, tr_y)
|
58
|
+
|
59
|
+
lgb_eval = lgb.Dataset(va_x, va_y)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
params = {'objective': 'regression',
|
64
|
+
|
65
|
+
'seed': 71,
|
66
|
+
|
67
|
+
'verbose': 1,
|
68
|
+
|
69
|
+
'metrics': 'rmse',
|
70
|
+
|
71
|
+
'force_col_wise':'true' # メモリが足りないから
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
num_round = 100
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
categorical_features = categorical_features
|
80
|
+
|
81
|
+
model = lgb.train(params, lgb_train, num_boost_round=num_round,
|
82
|
+
|
83
|
+
categorical_feature=categorical_features,
|
84
|
+
|
85
|
+
valid_names=['train', 'valid'], valid_sets=[lgb_train, lgb_eval],
|
86
|
+
|
87
|
+
)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
tmpfile = f'{TMP_DIR}/trained_model{va_period}.pkl'
|
94
|
+
|
95
|
+
pickle.dump(model, open(tmpfile, 'wb'))
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
va_pred = model.predict(va_x); va_pred = va_pred.reshape(-1, 1)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
tmp =va_y['Sales']==0
|
104
|
+
|
105
|
+
va_pred[tmp]=0
|
106
|
+
|
107
|
+
va_pred=va_pred+1; va_y=va_y+1
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# score = mean_squared_error(va_y, va_pred)
|
112
|
+
|
113
|
+
RMSPE = np.sqrt(np.mean((( (va_y-va_pred)/va_y)**2) )).values
|
114
|
+
|
115
|
+
RMSPE = RMSPE.astype(float)[0]
|
116
|
+
|
117
|
+
# score = log_loss(va_y, va_pred)
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
del model
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
'''
|