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

5

박예은's avatar
박예은 committed
6
def modeling(standard_df):
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
16
17
        feature_list = []
        label_list = []
        for i in range(len(data) - window_size):
            feature_list.append(np.array(data.iloc[i:i+window_size]))
18
            label_list.append(np.array(label.iloc[i + window_size]))
박예은's avatar
박예은 committed
19
        return np.array(feature_list), np.array(label_list)
20
21
22

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

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

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

    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
36
    model = tf.keras.Sequential([
37
38
        tf.keras.layers.LSTM(16,
                             return_sequences=False,
박예은's avatar
박예은 committed
39
                             input_shape=(6, 8)),
박예은's avatar
박예은 committed
40
41
        tf.keras.layers.Dense(1)
    ])
42
43
44
45
46

    model.compile(loss='mse', optimizer='adam')
    # model.fit(train_feature, train_label, epochs=50, batch_size=1000)

    model.save(os.getcwd() + '/src/data_processing/model.h5')