---
# System prepended metadata

title: Assignment 3：Time Series Prediction

---

# Assignment 3：Time Series Prediction
## 1.Description & data
給出第一部分{x(t)}（t=1->1000）的時間串列，來預測第二部分{x(t)}(t=1001->1500)的時間串列。
## 2.實作RNN
1. 數據前處理
```python=
f = open('train_data1.txt')                                                 
num_file = f.read()                            
data = num_file.split('\n')
f.close()  
file_seq = list(map(float,data))      
```
2. 數據圖像
```python=
import matplotlib.pyplot as plt         
plt.plot(file_seq )
plt.show()
```
3. 引入keras，並且定義了兩個方法用於切分數字向量和預測
```python=
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
import numpy as np
import keras                           
def int_to_vectors(sequence, n_steps):                          #  這一方法可以將數據切分為數字向量
    X = [ ]
    y =[ ]
    for i in range(len(sequence)):
        end_idx = i + n_steps                                    # n_steps為數字向量的大小
        if end_idx > len(sequence)-1:
            break
        vectors_x = sequence[i:end_idx]
        vector_y =  sequence[end_idx]
        X.append(vectors_x)
        y.append(vector_y)
    return array(X), array(y)

def seq_prediction(epochs):                             #  這一方法用於預測後面的數據，輸入的值為要測多少個
    global x_result
    x_input_r = raw_seq[-(n_steps-1):]
    x_result = [ ]
    x_input = array(raw_seq[-n_steps:])
    x_input = x_input.reshape((1, n_steps, n_features))
    for i in range(epochs):
        yhat = model.predict(x_input, verbose=0)
        x_result.append(yhat[0][0])
        x_input_r.append(yhat[0][0])
        x_input = array(x_input_r[-100:])
        x_input = x_input.reshape((1, n_steps, n_features))
```
4. 切分數據
將讀入的原始序列切分為大小為n_step的數字向量，這裡我們用的步長為100，可以觀察得知100步內數據呈現規律，
     這裡的訓練數據為X，標籤為y
```python=
x_result = [ ]    
raw_seq = file_seq                           # 將文件內的數據存入原始序列
n_steps = 100                                # 時序步長設為100
X, y = int_to_vectors(raw_seq,n_steps)              
n_features = 1                               # 特征值只有一個
X = X.reshape((X.shape[0], X.shape[1], n_features))
```
5. 定義模型
```python=
model = Sequential()                                                  
model.add(LSTM(50, activation='relu',input_shape=(n_steps, n_features),return_sequences=True))
model.add(LSTM(30))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.summary()
   
```
![](https://i.imgur.com/e0iy7tT.png)

6. 訓練模型，并得出第一筆測資的值
```python=
model.fit(X, y, epochs=50, batch_size=20, verbose=1,validation_split = 0.1) 
x_input = array(raw_seq[-n_steps:])                             
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
```
7. 保存訓練好的模型，預測後面的500個值
```python=
model.save('a2.h5')  
seq_prediction(500)
```
8. 繪製圖像
```python=
import matplotlib.pyplot as plt                   
a = list(range(1000,1500))      
plt.plot(raw_seq[:1000])
plt.plot(a,x_result)
plt.show()
```
![](https://i.imgur.com/aeMFSiK.png)
## 3.Conclusion
RNN全稱Recurrent Neural Networks，可以用來處理序列數據，當前狀態的輸出與過去狀態的輸出也有關係。
![](https://i.imgur.com/YBZms2c.png)
