Spaces:
Sleeping
Sleeping
eaglelandsonce
commited on
Create 19_RNN.py
Browse files- pages/19_RNN.py +75 -0
pages/19_RNN.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras import layers, models
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from sklearn.preprocessing import MinMaxScaler
|
7 |
+
|
8 |
+
# Load the dataset
|
9 |
+
data_url = 'https://raw.githubusercontent.com/selva86/datasets/master/aapl.csv'
|
10 |
+
df = pd.read_csv(data_url)
|
11 |
+
df = df[['Date', 'Close']]
|
12 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
13 |
+
df.set_index('Date', inplace=True)
|
14 |
+
|
15 |
+
# Normalize the data
|
16 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
17 |
+
scaled_data = scaler.fit_transform(df)
|
18 |
+
|
19 |
+
# Create sequences
|
20 |
+
def create_sequences(data, seq_length):
|
21 |
+
xs = []
|
22 |
+
ys = []
|
23 |
+
for i in range(len(data) - seq_length):
|
24 |
+
x = data[i:i + seq_length]
|
25 |
+
y = data[i + seq_length]
|
26 |
+
xs.append(x)
|
27 |
+
ys.append(y)
|
28 |
+
return np.array(xs), np.array(ys)
|
29 |
+
|
30 |
+
seq_length = 60
|
31 |
+
X, y = create_sequences(scaled_data, seq_length)
|
32 |
+
|
33 |
+
# Split the data into training and testing sets
|
34 |
+
split = int(0.8 * len(X))
|
35 |
+
X_train, X_test = X[:split], X[split:]
|
36 |
+
y_train, y_test = y[:split], y[split:]
|
37 |
+
|
38 |
+
# Reshape data for the model
|
39 |
+
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
|
40 |
+
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
|
41 |
+
|
42 |
+
# Build the RNN model
|
43 |
+
model = models.Sequential()
|
44 |
+
model.add(layers.LSTM(50, return_sequences=True, input_shape=(seq_length, 1)))
|
45 |
+
model.add(layers.LSTM(50, return_sequences=False))
|
46 |
+
model.add(layers.Dense(25))
|
47 |
+
model.add(layers.Dense(1))
|
48 |
+
|
49 |
+
model.summary()
|
50 |
+
|
51 |
+
# Compile the model
|
52 |
+
model.compile(optimizer='adam', loss='mean_squared_error')
|
53 |
+
|
54 |
+
# Train the model
|
55 |
+
history = model.fit(X_train, y_train, batch_size=32, epochs=20, validation_split=0.1)
|
56 |
+
|
57 |
+
# Make predictions
|
58 |
+
train_predict = model.predict(X_train)
|
59 |
+
test_predict = model.predict(X_test)
|
60 |
+
|
61 |
+
# Inverse transform the predictions
|
62 |
+
train_predict = scaler.inverse_transform(train_predict)
|
63 |
+
y_train = scaler.inverse_transform(y_train.reshape(-1, 1))
|
64 |
+
test_predict = scaler.inverse_transform(test_predict)
|
65 |
+
y_test = scaler.inverse_transform(y_test.reshape(-1, 1))
|
66 |
+
|
67 |
+
# Plot the results
|
68 |
+
plt.figure(figsize=(14, 5))
|
69 |
+
plt.plot(df.index, df['Close'], label='True Price')
|
70 |
+
plt.plot(df.index[seq_length:seq_length + len(train_predict)], train_predict, label='Train Predict')
|
71 |
+
plt.plot(df.index[seq_length + len(train_predict):], test_predict, label='Test Predict')
|
72 |
+
plt.xlabel('Date')
|
73 |
+
plt.ylabel('Close Price')
|
74 |
+
plt.legend()
|
75 |
+
plt.show()
|