model.py 1.51 KB
Newer Older
박예은's avatar
박예은 committed
1
import numpy as np
2
import os
박예은's avatar
박예은 committed
3
4
import tensorflow as tf

5

6
def modeling(standard_df, host):
7
8

    n = len(standard_df)
박예은's avatar
박예은 committed
9
10
11
    test_size = int(0.3 * n)
    train = standard_df[:-test_size]
    test = standard_df[-test_size:]
12
13

    def make_dataset(data, label, window_size=24):
박예은's avatar
박예은 committed
14
15
        feature_list = []
        label_list = []
16
        for i in range(len(data) - (window_size+3)):
박예은's avatar
박예은 committed
17
            feature_list.append(np.array(data.iloc[i:i+window_size]))
18
19
            label_list.append(
                np.array(label.iloc[i + window_size:i + window_size + 3]))
박예은's avatar
박예은 committed
20
        return np.array(feature_list), np.array(label_list)
21
22
23

    feature_cols = ['temp_out', 'humi_out', 'press',
                    'wind_speed', 'Day sin', 'Day cos', 'Year sin', 'Year cos']
박예은's avatar
박예은 committed
24
    label_cols = ['temp_out']
25

박예은's avatar
박예은 committed
26
27
    train_feature = train[feature_cols]
    train_label = train[label_cols]
28

박예은's avatar
박예은 committed
29
30
    test_feature = test[feature_cols]
    test_label = test[label_cols]
31
32
33
34
35
36

    train_feature, train_label = make_dataset(
        train_feature, train_label, window_size=6)
    test_feature, test_label = make_dataset(
        test_feature, test_label, window_size=6)

박예은's avatar
박예은 committed
37
    model = tf.keras.Sequential([
38
39
        tf.keras.layers.LSTM(16,
                             return_sequences=False,
박예은's avatar
박예은 committed
40
                             input_shape=(6, 8)),
41
        tf.keras.layers.Dense(3)
박예은's avatar
박예은 committed
42
    ])
43
44

    model.compile(loss='mse', optimizer='adam')
45
    model.fit(train_feature, train_label, epochs=50, batch_size=10)
46

47
48
    model.save(os.getcwd() +
               '/src/data_processing/models/{0}/model.h5'.format(host))