#!/usr/bin/env python
# coding: utf-8
#
# # ** 6 Predict the TimeSeries Data**
# 시계열 데이터 예측
#
# ## **1 은닉 마르코프 모델을 활용 (HMM)**
#
#
# ### **01 Pattern 이 분명한 데이터를 활용하여 은닉 마르코프 모델 생성 (HMM)**
# 은닉 마르코프 모델의 용도를 확인해본다
# In[1]:
import numpy as np
from hmmlearn.hmm import GaussianHMM
data = np.loadtxt('./data/hmm_data.txt', delimiter=',')
X = np.column_stack([data[:, 2]])
x = [x[0] for x in X]
get_ipython().run_line_magic('matplotlib', 'inline')
import pandas as pd
pd.Series(x).plot(figsize=(25,2))
# In[2]:
import warnings
warnings.filterwarnings("ignore")
num_components = 5
hmm = GaussianHMM(n_components = num_components,
covariance_type = 'diag',
n_iter = 1000)
hmm.fit(X)
# In[3]:
hmm
# In[4]:
hmm.means_
# In[5]:
for i in range(hmm.n_components):
print('''Hidden state {:2} - Mean : {:.3f} \\ Variance : {:.3f}'''.format(
i + 1, hmm.means_[i][0], np.diag(hmm.covars_[i])[0]))
# In[6]:
hidden_states = hmm.predict(X[-100:])
pd.Series(hidden_states).plot(figsize=(15,3), grid=True)
#
# ### **02 주가 데이터를 활용한 은닉 마르코프 모델 생성 (HMM)**
# by Stock Price Data
# In[7]:
# from pandas_datareader import get_data_yahoo
# price = get_data_yahoo('005930.KS','2017-06-01')
# price = price.fillna('ffill')
# price = price.dropna()
# price.tail(3)
# In[8]:
# 저장된 CSV 파일을 사용한다
import pandas as pd
price = pd.read_csv('./data/stock.csv')
price = price.set_index('Date')
price.head(3)
price.index = pd.DatetimeIndex(price.index)
price.info()
# In[9]:
import numpy as np
price_array = np.asarray(price.Close)
price_array.shape
# In[10]:
price_array = price_array.reshape(1, -1)
price_array.shape
# In[11]:
price_array.max()
# In[12]:
import warnings
warnings.filterwarnings("ignore")
num_components = 100
hmm = GaussianHMM(n_components = num_components,
covariance_type = 'diag',
n_iter = 1000)
hmm.fit(price_array.T)
# In[13]:
hidden_states = hmm.predict(price_array.T[:15])
pd.Series(hidden_states).plot(figsize=(10,2), grid=True)
# In[ ]: