๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[FuncAnimation] 2. ๋จ์ผ๋ณ๋ ๊ทธ๋ํ๋ฅผ ๋ง๋ค์ด์ GUI๋ก ์๊ฐํํ๊ธฐ ๋ณธ๋ฌธ
[FuncAnimation] 2. ๋จ์ผ๋ณ๋ ๊ทธ๋ํ๋ฅผ ๋ง๋ค์ด์ GUI๋ก ์๊ฐํํ๊ธฐ
์ง์ง์ํ์นด 2022. 10. 24. 15:06221024 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ ahnig ๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
[keras, TF2.0] ์จ๋ ๋ฐ์ดํฐ, ์๊ณ์ด ์์ธกํ๊ธฐ (Time Series Forecasting)
์๊ณ์ด ์์ธก(Time Series Forecasting) Licensed under the Apache License, Version 2.0 (the "License") MIT License https://www.tensorflow.org/tutorials/structured_data/time_series RNN(Recurrent Neural..
ahnjg.tistory.com
๐ csv ํ์ผ์ ๋ฐ์์ ์ค์๊ฐ ๊ทธ๋ํ๋ฅผ ๋ง๋ค์ด๋ณด์
- csv ๋ฐ์ดํฐ ์ค์ผ์ผ ์กฐ์
- ์ต๊ทผ ๋ฐ์ดํฐ๋ก ๋ฏธ๋์จ๋ ์์ธก
- LSTM์ผ๋ก ๋ฏธ๋์จ๋ ์์ธก
# 1. ๋จ์ผ๋ณ๋ ๊ทธ๋ํ
## https://operstu1.tistory.com/97
import random
from itertools import count
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from pandas.core.indexes import interval
import tensorflow as tf
import matplotlib as mpl
import os
TRAIN_SPLIT = 4000 # 4558 rows
tf.random.set_seed(13)
def load_file() :
# ๋ฐ์ดํฐ ๋ก๋
data = pd.read_csv("/home/ubuntu/FPDS/20220929/TESTTEST.csv")
# insert_date_time ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ง๊ณ ํฉ
temp = data.groupby(["insert_date_time"], as_index=False).sum()
# ๋จ์ผ ๋ณ์ ์ ํ
uni_temp = temp['cnt_item']
uni_temp.index = temp['insert_date_time']
return uni_temp
def scaling(uni_temp) :
# ๋ฐ์ดํฐ ๊ฐ์ ํ๊ท ๊ฐ์ ๋นผ๊ณ ํ์คํธ์ฐจ๋ฅผ ๋๋๋ ๋ฐ์ดํฐ ํ์คํ (Standardization)
uni_temp = uni_temp.values
uni_train_mean = uni_temp[:TRAIN_SPLIT].mean()
uni_train_std = uni_temp[:TRAIN_SPLIT].std()
uni_data = (uni_temp - uni_train_mean)/(uni_train_std)
return uni_data
def univariate_data(dataset, start_index, end_index, history_size, target_size):
data=[]
labels=[]
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = range(i-history_size, i)
# (history_size,) ์์ (history_size,1) ๋ก reshape
data.append(np.reshape(dataset[indices], (history_size,1)))
labels.append(dataset[i+target_size])
return np.array(data), np.array(labels)
def prediction(uni_data) :
# ๊ฐ์ฅ ์ต๊ทผ์ ์์ง๋, ๋ง์ง๋ง 20๊ฐ์ ๋ฐ์ดํฐํฌ์ธํธ๋ฅผ ์ฌ์ฉํด์ ๋ฏธ๋ ์จ๋๋ฅผ ์์ธก
univariate_past_history = 20
univariate_future_target = 0
x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT, univariate_past_history, univariate_future_target)
x_val_uni, y_val_uni = univariate_data(uni_data, TRAIN_SPLIT, None, univariate_past_history, univariate_future_target)
print('Single window of past history')
print(x_train_uni[0])
print('\n Target temperature to predict')
print(y_train_uni[0])
return x_train_uni, y_train_uni
def create_time_steps(length):
return list(range(-length, 0))
def show_plot(plot_data, delta, title):
# ๋ฐ์ดํฐ, ์ค์ ๋ฐ์ดํฐ, ๋ชจ๋ธ ์์ธก ๋ฐ์ดํฐ ๋น๊ต
labels = ['History', 'True Future', 'Model Prediction']
marker = ['.-', 'rx', 'go']
time_steps = create_time_steps(plot_data[0].shape[0])
if delta:
future = delta
else:
future = 0
plt.title(title)
for i, x in enumerate(plot_data):
if i:
plt.plot(future, plot_data[i], marker[i], markersize=10, label=labels[i])
else:
plt.plot(time_steps, plot_data[i].flatten(), marker[i], label=labels[i])
plt.legend()
plt.xlim([time_steps[0], (future+5)*2])
plt.xlabel('Time-Step')
return plt
# ์ค์๊ฐ ๊ทธ๋ํ
def animation(i):
data = load_file()
x = []
y1 = []
x = data[0:i].index
y1 = data[0:i]
ax.cla()
ax.plot(x,y1, label='cnt_item')
plt.legend(loc = 'upper left')
plt.tight_layout()
# 1. ๋ฐ์ดํฐ ๋ก๋ํ๊ณ , ๋จ์ผ๋ณ์๋ง ๊ฐ๊ณ ์จ๋ค
# 2. ๋ฐ์ดํฐ ๊ฐ์ ํ๊ท ๊ฐ์ ๋นผ๊ณ ํ์คํธ์ฐจ๋ฅผ ๋๋๋ ๋ฐ์ดํฐ ํ์คํ (Standardization)
uni_data = scaling(load_file())
# 3. ๋ชจ๋ธ ์์ฑ์ ์์ด์ ๊ฐ์ฅ ์ต๊ทผ์ ์์ง๋, ๋ง์ง๋ง 20๊ฐ์ ๋ฐ์ดํฐํฌ์ธํธ๋ฅผ ์ฌ์ฉํด์ ๋ฏธ๋ ์จ๋๋ฅผ ์์ธก
x_train_uni, y_train_uni = prediction(uni_data)
# 4. ์์ธก๊ฐ ์๊ฐํ
show_plot([x_train_uni[0], y_train_uni[0]], 0, 'Sample Example')
# 5. ๋ฐ์ดํฐ ์ค์๊ฐ ์๊ฐํ
plt.style.use('seaborn')
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
animation = FuncAnimation(plt.gcf(), func=animation, interval=100)
plt.show()
# 6. gif ์ ์ฅํ๊ธฐ
#graph_ani.save('graph_ani.gif', writer='imagemagick', fps=3, dpi=100)
โ ์ต๊ทผ ๋ฐ์ดํฐ๋ก ๋ฏธ๋์จ๋ ์์ธก
โ LSTM์ผ๋ก ๋ฏธ๋์จ๋ ์์ธก
์ํ์ ๊ฒฝ๋ง(Recurrent Neural Network)
: ์ํ์ ๊ฒฝ๋ง์ ํตํด ์๊ณ์ด ๋ฐ์ดํฐ๋ฅผ ํ ์์ ํ์์ ์ฉ ์ฒ๋ฆฌํ ์ ์์ผ๋ฉฐ, ์ด๋ ํด๋น ์์ ๊น์ง ์ธํ์ผ๋ก ๋ค์ด๊ฐ๋ ๋ฐ์ดํฐ๋ฅผ ์์ฝ
'๐ฉโ๐ป ์ธ๊ณต์ง๋ฅ (ML & DL) > Serial Data' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[FuncAnimation] 4. Mongo DB์ ์๊ณ์ด ๋ฐ์ดํฐ ์ ์ฅํ๊ธฐ (2) (0) | 2022.10.25 |
---|---|
[FuncAnimation] 3. Mongo DB์ ์๊ณ์ด ๋ฐ์ดํฐ ์ ์ฅํ๊ธฐ (1) (0) | 2022.10.24 |
[FuncAnimation] 1. Random ๋ฐ์ดํฐ๋ก ์ค์๊ฐ ๊ทธ๋ํ ๋ง๋ค๊ธฐ (0) | 2022.10.21 |
๋นํธ์ฝ์ธ ์ฐจํธ๋ฐ์ดํฐ ๋ถ์ํ๊ธฐ (0) | 2022.10.07 |
์๊ณ์ด ๋ฐ์ดํฐ ๋ถํด (์ ์ , ๋น์ ์ ๋ฐ์ดํฐ) (0) | 2022.10.06 |