-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime_series_prediction_Stock_Tensorflow.py
More file actions
68 lines (56 loc) · 1.89 KB
/
Time_series_prediction_Stock_Tensorflow.py
File metadata and controls
68 lines (56 loc) · 1.89 KB
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
# Time Series Prediction (Stock Prices)
#
# Problem: Predict stock prices using LSTM
import tensorflow as tf
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import yfinance as yf
# Download stock data
ticker = "AAPL"
data = yf.download(ticker, start="2020-01-01", end="2023-01-01")
prices = data['Close'].values.reshape(-1, 1)
# Normalize data
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_prices = scaler.fit_transform(prices)
# Create sequences for training
def create_sequences(data, seq_length):
X, y = [], []
for i in range(len(data) - seq_length):
X.append(data[i:(i + seq_length), 0])
y.append(data[i + seq_length, 0])
return np.array(X), np.array(y)
seq_length = 60 # Use 60 days to predict next day
X, y = create_sequences(scaled_prices, seq_length)
# Split data
train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
# Reshape for LSTM (samples, time steps, features)
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))
# Build LSTM model
model = keras.Sequential([
keras.layers.LSTM(50, return_sequences=True, input_shape=(seq_length, 1)),
keras.layers.Dropout(0.2),
keras.layers.LSTM(50, return_sequences=False),
keras.layers.Dropout(0.2),
keras.layers.Dense(25),
keras.layers.Dense(1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
# Train model
history = model.fit(
X_train, y_train,
batch_size=32,
epochs=50,
validation_data=(X_test, y_test),
verbose=1
)
# Make predictions
predictions = model.predict(X_test)
predictions = scaler.inverse_transform(predictions)
y_test_actual = scaler.inverse_transform(y_test.reshape(-1, 1))
# Calculate RMSE
rmse = np.sqrt(np.mean((predictions - y_test_actual) ** 2))
print(f"RMSE: {rmse:.2f}")