<repo_name>temitope101/Time-Series-Prediction-with-LSTM-Recurrent-Neural-Networks-in-Python-with-Keras<file_sep>/TensorFlow-Tutorials-for-Time-Series.py
sudo -H pip install sklearn
# sudo -H pip install lstm_predictor
sudo -H pip install keras
sudo -H pip install readline
# doesnt work on windows



#Could not find a version that satisfies the requirement lstm_predictor (from versions: )
#No matching distribution found for lstm_predictor

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

from tensorflow.contrib import learn
from sklearn.metrics import mean_squared_error, mean_absolute_error
from lstm_predictor import generate_data, load_csvdata, lstm_model


LOG_DIR = './ops_logs'
TIMESTEPS = 10
RNN_LAYERS = [{'steps': TIMESTEPS}]
DENSE_LAYERS = [10, 10]
TRAINING_STEPS = 100000
BATCH_SIZE = 100
PRINT_STEPS = TRAINING_STEPS / 100

dateparse = lambda dates: pd.datetime.strptime(dates, '%d/%m/%Y %H:%M')

rawdata =  pd.read_csv("./TotalLoad.csv",parse_dates={'timeline':['date', '(UTC)']},index_col='timeline', date_parser=dateparse)

rawdata =  pd.read_csv("./TotalLoad.csv",index_col=['date', '(UTC)'])

# parsing errors in pandas


df = pd.read_csv("./TotalLoad.csv")
df

X, y = load_csvdata(rawdata, TIMESTEPS, seperate=False)

regressor = learn.TensorFlowEstimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS),n_classes=0,verbose=1,steps=TRAINING_STEPS,optimizer='Adagrad',learning_rate=0.03, batch_size=BATCH_SIZE)

regressor = learn.Estimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS),n_classes=0,verbose=1,steps=TRAINING_STEPS,optimizer='Adagrad',learning_rate=0.03, batch_size=BATCH_SIZE)

https://www.tensorflow.org/api_guides/python/contrib.learn

regressor = learn.Estimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS))

WARNING:tensorflow:Using temporary folder as model directory: C:\Users\aroug\AppData\Local\Temp\tmp3nwb8sg9
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\aroug\AppData\Local\Temp\tmp3nwb8sg9
INFO:tensorflow:Using default config.
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_keep_checkpoint_max': 5, '_task_type': None, '_master': '', '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_environment': 'local', '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x0000014792A352E8>, '_save_checkpoints_secs': 600, '_task_id': 0, '_keep_checkpoint_ev
ery_n_hours': 10000, '_evaluation_master': '', '_tf_random_seed': None, '_num_ps_replicas': 0, '_save_checkpoints_steps': None, '_save_summary_steps': 100, '_is_chief': True}
INFO:tensorflow:Using config: {'_keep_checkpoint_max': 5, '_task_type': None, '_master': '', '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_environment': 'local', '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x0000014792A352E8>, '_save_checkpoints_secs': 600, '_task_id': 0, '_keep_checkpoint_ev
ery_n_hours': 10000, '_evaluation_master': '', '_tf_random_seed': None, '_num_ps_replicas': 0, '_save_checkpoints_steps': None, '_save_summary_steps': 100, '_is_chief': True}
>>>


++++++++++++++++
import numpy
import pandas
import tkinter
import matplotlib.pyplot as plt

dataset = pandas.read_csv('./TotalLoad.csv', usecols=[2], engine='python', skipfooter=3)
dataset 
plt.plot(dataset)
plt.show()

import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error


dataframe = pandas.read_csv('./TotalLoad.csv', usecols=[2], engine='python', skipfooter=3)
dataset = dataframe.values
dataset = dataset.astype('float32')
	
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)

# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
print(len(train), len(test))

# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1): \
	dataX, dataY = [], [] \
	for i in range(len(dataset)-look_back-1): \
		a = dataset[i:(i+look_back), 0] \
		dataX.append(a)
		dataY.append(dataset[i + look_back, 0]) \
	return numpy.array(dataX), numpy.array(dataY) 


	#readline fails on Windows
	<file_sep>/lstm-for-sine-wave.py

# coding: utf-8

# In[13]:

# get_ipython().magic('matplotlib inline')
import numpy as np
from matplotlib import pyplot as plt

from tensorflow.contrib import learn
from sklearn.metrics import mean_squared_error, mean_absolute_error
from lstm_predictor import generate_data, lstm_model


# ## Libraries
# 
# - numpy: package for scientific computing 
# - pandas: data structures and data analysis tools
# - tensorflow: open source software library for machine intelligence
# - matplotlib: 2D plotting library
# 
# 
# - **learn**: Simplified interface for TensorFlow (mimicking Scikit Learn) for Deep Learning
# - mse: "mean squared error" as evaluation metric
# - **lstm_predictor**: our lstm class 
# 

# In[14]:

LOG_DIR = './ops_logs'
TIMESTEPS = 5
RNN_LAYERS = [{'steps': TIMESTEPS}]
DENSE_LAYERS = [10, 10]
TRAINING_STEPS = 100000
BATCH_SIZE = 100
PRINT_STEPS = TRAINING_STEPS / 100


# ## Parameter definitions
# 
# - LOG_DIR: log file
# - TIMESTEPS: RNN time steps
# - RNN_LAYERS: RNN layer 정보
# - DENSE_LAYERS: DNN 크기 [10, 10]: Two dense layer with 10 hidden units
# - TRAINING_STEPS: 학습 스텝
# - BATCH_SIZE: 배치 학습 크기
# - PRINT_STEPS: 학습 과정 중간 출력 단계 (전체의 1% 해당하는 구간마다 출력)

# In[15]:

regressor = learn.TensorFlowEstimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS), 
                                      n_classes=0,
                                      verbose=1,  
                                      steps=TRAINING_STEPS, 
                                      optimizer='Adagrad',
                                      learning_rate=0.03, 
                                      batch_size=BATCH_SIZE)


# ## Create a regressor with TF Learn
# 
# : 예측을 위한 모델 생성. TF learn 라이브러리에 제공되는 TensorFlowEstimator를 사용.
# 
# **Parameters**: 
# 
# - model_fn: 학습 및 예측에 사용할 모델
# - n_classes: label에 해당하는 클래스 수 (0: prediction, 1이상: classification) 확인필요
# - verbose: 과정 출력
# - steps: 학습 스텝
# - optimizer: 최적화 기법 ("SGD", "Adam", "Adagrad")
# - learning_rate: learning rate
# - batch_size: batch size
# 
# 

# In[ ]:

X, y = generate_data(np.sin, np.linspace(0, 100, 10000), TIMESTEPS, seperate=False)
# create a lstm instance and validation monitor

validation_monitor = learn.monitors.ValidationMonitor(X['val'], y['val'],
                                                      every_n_steps=PRINT_STEPS,
                                                      early_stopping_rounds=1000)


# ## Generate a dataset
# 
# 1. generate_data: 학습에 사용될 데이터를 특정 함수를 이용하여 만듭니다.
#  - fct: 데이터를 생성할 함수
#  - x: 함수값을 관측할 위치
#  - time_steps: 관측(observation)
#  - seperate: check multimodal
# 1. ValidationMonitor: training 이후, validation 과정을 모니터링
#  - x
#  - y
#  - every_n_steps: 중간 출력
#  - early_stopping_rounds

# In[16]:

regressor.fit(X['train'], y['train'], monitors=[validation_monitor], logdir=LOG_DIR)


# ## Train and validation
# 
# - fit: training data를 이용해 학습, 모니터링과 로그를 통해 기록
# 
# 

# In[17]:

predicted = regressor.predict(X['test'])
mse = mean_squared_error(y['test'], predicted)
print ("Error: %f" % mse)


# ## Evaluate using test set
# 
# Evaluate our hypothesis using test set. The mean squared error (MSE) is used for the evaluation metric.
# 
# 

# In[18]:

plot_predicted, = plt.plot(predicted, label='predicted')
plot_test, = plt.plot(y['test'], label='test')
plt.legend(handles=[plot_predicted, plot_test])



# ## Plotting
# 
# Then, plot both predicted values and original values from test set.
<file_sep>/README.md
Derived from:
- http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
- https://github.com/tgjeon

# TensorFlow Tutorial for Time Series Prediction

This tutorial is designed to easily learn TensorFlow for time series prediction. 
Each tutorial subject includes both code and notebook with descriptions.

## Tutorial Index

#### MNIST classification using Recurrent Neural Networks (RNN)
- Classification for MNIST using RNN ([notebook](https://github.com/tgjeon/TensorFlow-Tutorials-for-Time-Series/blob/master/mnist-rnn.ipynb))

#### Time series prediction using Recurrent Neural Networks (RNN)

- Prediction for sine wave function using Gaussian process ([code](https://github.com/tgjeon/TensorFlow-Tutorials-for-Time-Series/blob/master/gp-for-sine-wave.py) / [notebook](https://github.com/tgjeon/TensorFlow-Tutorials-for-Time-Series/blob/master/gp-for-sine-wave.ipynb))
- Prediction for sine wave function using RNN ([code](https://github.com/tgjeon/TensorFlow-Tutorials-for-Time-Series/blob/master/lstm-for-sine-wave.py) / [notebook](https://github.com/tgjeon/TensorFlow-Tutorials-for-Time-Series/blob/master/lstm-for-sine-wave.ipynb))
- Prediction for electricity price ([code](https://github.com/tgjeon/TensorFlow-Tutorials-for-Time-Series/blob/master/lstm-for-epf.py) / notebook)

## Slide materials
- [Slides on slideshare (TensorFlow-KR Meetup)](http://www.slideshare.net/TaegyunJeon1/electricity-price-forecasting-with-recurrent-neural-networks)
- [Slides on github (KSC 2016 Tutorial)](https://github.com/tgjeon/TensorFlow-Tutorials-for-Time-Series/raw/master/KSC2016%20-%20Recurrent%20Neural%20Networks.pptx)

#### Dependencies

```
Python (3.4.4)
TensorFlow (r0.9)
numpy (1.11.1)
pandas (0.16.2)
cuda (to run examples on GPU)
sudo apt install python3-tk (for tcl/tk)
sudo apt install tk
```

#### Dataset

- Energy Price Forecast 2016: http://complatt.smartwatt.net
- Or use the uploaded csv file for price history for 2015.

<file_sep>/lstm-for-epf.py
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

from tensorflow.contrib import learn
from sklearn.metrics import mean_squared_error, mean_absolute_error
from lstm_predictor import generate_data, load_csvdata, lstm_model


LOG_DIR = './ops_logs'
TIMESTEPS = 10
RNN_LAYERS = [{'steps': TIMESTEPS}]
DENSE_LAYERS = [10, 10]
TRAINING_STEPS = 100000
BATCH_SIZE = 100
PRINT_STEPS = TRAINING_STEPS / 100

dateparse = lambda dates: pd.datetime.strptime(dates, '%d/%m/%Y %H:%M')
rawdata = pd.read_csv("./input/ElectricityPrice/RealMarketPriceDataPT.csv", 
                   parse_dates={'timeline': ['date', '(UTC)']}, 
                   index_col='timeline', date_parser=dateparse)


X, y = load_csvdata(rawdata, TIMESTEPS, seperate=False)


regressor = learn.TensorFlowEstimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS), 
                                      n_classes=0,
                                      verbose=1,  
                                      steps=TRAINING_STEPS, 
                                      optimizer='Adagrad',
                                      learning_rate=0.03, 
                                      batch_size=BATCH_SIZE)




validation_monitor = learn.monitors.ValidationMonitor(X['val'], y['val'],
                                                      every_n_steps=PRINT_STEPS,
                                                      early_stopping_rounds=1000)

regressor.fit(X['train'], y['train'], monitors=[validation_monitor], logdir=LOG_DIR)


predicted = regressor.predict(X['test'])
mse = mean_absolute_error(y['test'], predicted)
print ("Error: %f" % mse)

plot_predicted, = plt.plot(predicted, label='predicted')
plot_test, = plt.plot(y['test'], label='test')
plt.legend(handles=[plot_predicted, plot_test])
