train2-checkpoint.ipynb
14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n",
"/Users/yangyoonji/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
"/Users/yangyoonji/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
"/Users/yangyoonji/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
"/Users/yangyoonji/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
"/Users/yangyoonji/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
"/Users/yangyoonji/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n"
]
}
],
"source": [
"from keras.models import Model, Sequential\n",
"from keras.layers import Input, Dense, LSTM, Bidirectional\n",
"from keras.callbacks import EarlyStopping, ModelCheckpoint\n",
"from keras import backend as K\n",
"import matplotlib.pyplot as plt\n",
"from keras.layers.core import Dense, Activation, Dropout\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"train = pd.read_csv('/Users/yangyoonji/Documents/2020_2학기/캡스톤디자인/data/scaled/train_scaled.csv')\n",
"test = pd.read_csv('/Users/yangyoonji/Documents/2020_2학기/캡스톤디자인/data/recent_test/recent_data_scaled.csv')\n",
"validation = pd.read_csv('/Users/yangyoonji/Documents/2020_2학기/캡스톤디자인/data/scaled/validation_scaled.csv')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# 불필요한 열 삭제\n",
"del train['Unnamed: 0']\n",
"del test['Unnamed: 0']\n",
"del validation['Unnamed: 0']"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"test.columns = ['close','open','high','low','vol']"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"checking if any null values are present in train\n",
" close 0\n",
"open 0\n",
"high 0\n",
"low 0\n",
"vol 0\n",
"dtype: int64\n",
"checking if any null values are present in test\n",
" close 0\n",
"open 0\n",
"high 0\n",
"low 0\n",
"vol 0\n",
"dtype: int64\n",
"checking if any null values are present in validation\n",
" close 0\n",
"open 0\n",
"high 0\n",
"low 0\n",
"vol 0\n",
"dtype: int64\n"
]
}
],
"source": [
"# NULL value 없나 확인해보기\n",
"print(\"checking if any null values are present in train\\n\", train.isna().sum())\n",
"print(\"checking if any null values are present in test\\n\", test.isna().sum())\n",
"print(\"checking if any null values are present in validation\\n\", validation.isna().sum())"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def make_dataset(data, label,window_size = 20):\n",
" feature_list = []\n",
" label_list = []\n",
" for i in range(len(data)-window_size):\n",
" feature_list.append(np.array(data.iloc[i:i+window_size]))\n",
" label_list.append(np.array(label.iloc[i+window_size]))\n",
" return np.array(feature_list), np.array(label_list) "
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# feature , label 분리\n",
"feature_cols = ['open','high','low','vol']\n",
"label_cols = ['close']\n",
"\n",
"##----train_data----##\n",
"train_feature = train[feature_cols]\n",
"train_label = train[label_cols]\n",
"\n",
"train_feature, train_label = make_dataset(train_feature,train_label,3)\n",
"\n",
"##----test_data----##\n",
"test_feature = test[feature_cols]\n",
"test_label = test[label_cols]\n",
"test_feature, test_label = make_dataset(test_feature,test_label,3)\n",
"\n",
"##----validation_data----##\n",
"validation_feature = validation[feature_cols]\n",
"validation_label = validation[label_cols]\n",
"validation_feature, validation_label = make_dataset(validation_feature,validation_label,3)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>close</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>3458</th>\n",
" <td>0.672652</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3459</th>\n",
" <td>0.673757</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3460</th>\n",
" <td>0.671060</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3461</th>\n",
" <td>0.655714</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3462</th>\n",
" <td>0.653901</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3463</th>\n",
" <td>0.660136</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" close\n",
"3458 0.672652\n",
"3459 0.673757\n",
"3460 0.671060\n",
"3461 0.655714\n",
"3462 0.653901\n",
"3463 0.660136"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train[label_cols].tail(6)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"train_label_pd = pd.DataFrame(train_label)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>3455</th>\n",
" <td>0.672652</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3456</th>\n",
" <td>0.673757</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3457</th>\n",
" <td>0.671060</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3458</th>\n",
" <td>0.655714</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3459</th>\n",
" <td>0.653901</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3460</th>\n",
" <td>0.660136</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0\n",
"3455 0.672652\n",
"3456 0.673757\n",
"3457 0.671060\n",
"3458 0.655714\n",
"3459 0.653901\n",
"3460 0.660136"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_label_pd.tail(6)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3461"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(train_label)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"X_train, y_train, X_val, y_val = train_feature, train_label, validation_feature, validation_label"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"X_test, y_test = test_feature, test_label"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(y_val)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"lstm_7 (LSTM) (None, 20, 64) 17664 \n",
"_________________________________________________________________\n",
"lstm_8 (LSTM) (None, 50) 23000 \n",
"_________________________________________________________________\n",
"dense_4 (Dense) (None, 1) 51 \n",
"=================================================================\n",
"Total params: 40,715\n",
"Trainable params: 40,715\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"#keras 활용한 LSTM 모델 생성\n",
"\n",
"model = Sequential() \n",
"model.add(LSTM(64, return_sequences=True, input_shape=(20, 4))) #첫번째 LSTM 은 유닛수 50개\n",
"model.add(LSTM(50, return_sequences=False)) #두번째 LSTM 유닛수는 64개\n",
"model.add(Dense(1, activation='linear')) # 아웃풋으로 나오는 값은 1개 (다음날 하루 예측)\n",
"model.compile(loss='mse', optimizer='adam') #손실 함수 ,optimizer= rmsprop\n",
"model.summary() #모델의 개요"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From /Users/yangyoonji/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.cast instead.\n",
"Train on 3444 samples, validate on 167 samples\n",
"Epoch 1/100\n",
"3444/3444 [==============================] - 53s 15ms/step - loss: 0.0041 - val_loss: 0.0097\n",
"Epoch 2/100\n",
"3444/3444 [==============================] - 48s 14ms/step - loss: 7.3472e-04 - val_loss: 0.0073\n",
"Epoch 3/100\n",
"2510/3444 [====================>.........] - ETA: 13s - loss: 5.2751e-04"
]
}
],
"source": [
"early_stopping = EarlyStopping() \n",
"hist = model.fit(X_train, y_train, validation_data=(X_val, y_val),batch_size=5, epochs=100,callbacks=[early_stopping])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"score = model.evaluate(X_test, y_test, batch_size = 5)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}