잠토의 잠망경

[ML] Multiple Parallel Series 본문

공부/Python

[ML] Multiple Parallel Series

잠수함토끼 2020. 2. 15. 17:11

GITHUB

https://github.com/yiwonjae/Project_Lotto/blob/master/Book_001/p104.py

 

0. 목표

 

이전 값들을 갖고 한단계 앞 값을 예측한다.

 

 

 

1. DATA

in_seq1 = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])
in_seq2 = np.array([15, 25, 35, 45, 55, 65, 75, 85, 95])
out_seq = np.array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))])

in_seq1 = in_seq1.reshape((len(in_seq1), 1))
in_seq2 = in_seq2.reshape((len(in_seq2), 1))
out_seq = out_seq.reshape((len(out_seq), 1))

dataset = np.hstack((in_seq1, in_seq2, out_seq))

 

2. DATA 정제

X

import numpy as np
from numpy import ndarray


def split_sequence(sequence:ndarray, n_stpes:int)->(ndarray, ndarray):

    x, y =[], []

    for i in range(len(sequence)):

        if(i+n_steps>= len(sequence)):
            break

        x.append(sequence[i:i+n_steps,:])
        y.append(sequence[i+n_steps,:])

    return (np.asarray(x), np.asarray(y))


n_steps = 3

x, y = split_sequence(dataset, n_steps)

n_feature = x.shape[2]

 

Y

 

3. 학습

from keras import Sequential
from keras.layers import Dense, Conv1D, MaxPool1D, Flatten

model = Sequential()
model.add(Conv1D(64, 2, activation='relu', input_shape=(n_steps, n_feature)))
model.add(MaxPool1D())
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(n_feature))
model.compile(loss='mse', optimizer='adam')

model.fit(x, y, epochs=200, verbose=1)

 

4. 표시

x_input = np.array([[70,75,145], [80,85,165], [90,95,185]]) # 기대 값: 100, 105, 205
x_input = x_input.reshape((1, n_steps, n_feature))
yhat = model.predict(x_input, verbose=0)
print(yhat) # [[104.76112 112.68031 217.38632]]
Comments