-
Notifications
You must be signed in to change notification settings - Fork 0
/
Univariate_LSTM_Forecast.py
130 lines (65 loc) · 1.96 KB
/
Univariate_LSTM_Forecast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# coding: utf-8
# In[31]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import keras
from keras.models import Sequential
from keras.layers import Dense,LSTM
# In[34]:
df = pd.read_csv('/home/sim06/Downloads/cer3.csv')
time = pd.date_range(start='1961',periods=56,freq='Y')
df.set_index(time,inplace=True)
df = df.drop(df[['No','month','day','year']],axis=1)
# In[35]:
feature = df.iloc[:,0:1].values
feature = feature.astype(float)
# In[36]:
scale = MinMaxScaler(feature_range=(0,1))
feature_scaled = scale.fit_transform(feature)
# In[37]:
x_train = []
y_train = []
for i in range(10,len(feature_scaled)):
x_train.append(feature_scaled[i-10:i,0])
y_train.append(feature_scaled[i,0])
x_train,y_train = np.array(x_train),np.array(y_train)
# In[38]:
x_train = np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))
# In[39]:
model = Sequential()
# In[40]:
from keras.layers import Dropout
model.add(LSTM(units=50,return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(Dropout(0.2))
model.add(LSTM(units=50,return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50,return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=1))
# In[41]:
model.compile(loss='mean_squared_error',optimizer='adam')
# In[42]:
history = model.fit(x_train,y_train,epochs=100,batch_size=20)
# In[43]:
plt.xlabel('Iterations')
plt.ylabel('Error')
plt.plot(history.history['loss'],label='Error')
plt.show()
# In[44]:
predicted_stock_price = model.predict(x_train)
#predicted_stock_price = scale.inverse_transform(predicted_stock_price)
# In[45]:
print(predicted_stock_price)
# In[46]:
plt.xlabel('Dataset')
plt.ylabel('Scaled Output')
plt.plot(y_train,color='red',label='True')
plt.plot(predicted_stock_price,color='green',label='Predicted')
plt.legend()
plt.show()
# In[ ]: