Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,7 @@ def stock_prediction_app(ticker, start_date, end_date):
|
|
20 |
return "No data available for the selected date range.", None
|
21 |
|
22 |
# Prepare the data for LSTM model
|
23 |
-
df_close = stock_data[['Close'
|
24 |
scaler = MinMaxScaler(feature_range=(0, 1))
|
25 |
scaled_data = scaler.fit_transform(df_close)
|
26 |
|
@@ -28,19 +28,21 @@ def stock_prediction_app(ticker, start_date, end_date):
|
|
28 |
def create_dataset(data, time_step=60):
|
29 |
X_train, y_train = [], []
|
30 |
for i in range(len(data)-time_step-1):
|
31 |
-
X_train.append(data[i:(i+time_step),
|
32 |
-
y_train.append(data[i + time_step, 0])
|
33 |
return np.array(X_train), np.array(y_train)
|
34 |
|
35 |
X_train, y_train = create_dataset(scaled_data)
|
36 |
|
37 |
# Reshape the data for LSTM [samples, time steps, features]
|
38 |
-
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1],
|
39 |
|
40 |
-
# Define LSTM model
|
41 |
lstm_model = tf.keras.Sequential([
|
42 |
-
tf.keras.layers.LSTM(
|
43 |
-
tf.keras.layers.
|
|
|
|
|
44 |
tf.keras.layers.Dense(25),
|
45 |
tf.keras.layers.Dense(1)
|
46 |
])
|
@@ -48,16 +50,16 @@ def stock_prediction_app(ticker, start_date, end_date):
|
|
48 |
# Compile the model
|
49 |
lstm_model.compile(optimizer='adam', loss='mean_squared_error')
|
50 |
|
51 |
-
# Train the model
|
52 |
-
lstm_model.fit(X_train, y_train, batch_size=
|
53 |
|
54 |
# Predict on the same data (just for demonstration)
|
55 |
predictions = lstm_model.predict(X_train)
|
56 |
-
predictions = scaler.inverse_transform(predictions) # Convert back to original scale
|
57 |
|
58 |
# Create a plot to show predictions
|
59 |
plt.figure(figsize=(10, 5))
|
60 |
-
plt.plot(df_close.values, label='Actual Stock Price', color='blue')
|
61 |
plt.plot(predictions, label='Predicted Stock Price', color='orange')
|
62 |
plt.title(f'{ticker} Stock Price Prediction')
|
63 |
plt.xlabel('Days')
|
|
|
20 |
return "No data available for the selected date range.", None
|
21 |
|
22 |
# Prepare the data for LSTM model
|
23 |
+
df_close = stock_data[['Close', 'Open', 'High', 'Low', 'Volume']]
|
24 |
scaler = MinMaxScaler(feature_range=(0, 1))
|
25 |
scaled_data = scaler.fit_transform(df_close)
|
26 |
|
|
|
28 |
def create_dataset(data, time_step=60):
|
29 |
X_train, y_train = [], []
|
30 |
for i in range(len(data)-time_step-1):
|
31 |
+
X_train.append(data[i:(i+time_step), :])
|
32 |
+
y_train.append(data[i + time_step, 0]) # Predicting the 'Close' price
|
33 |
return np.array(X_train), np.array(y_train)
|
34 |
|
35 |
X_train, y_train = create_dataset(scaled_data)
|
36 |
|
37 |
# Reshape the data for LSTM [samples, time steps, features]
|
38 |
+
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2]) # (samples, time steps, features)
|
39 |
|
40 |
+
# Define LSTM model with dropout for regularization
|
41 |
lstm_model = tf.keras.Sequential([
|
42 |
+
tf.keras.layers.LSTM(100, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])),
|
43 |
+
tf.keras.layers.Dropout(0.2),
|
44 |
+
tf.keras.layers.LSTM(100, return_sequences=False),
|
45 |
+
tf.keras.layers.Dropout(0.2),
|
46 |
tf.keras.layers.Dense(25),
|
47 |
tf.keras.layers.Dense(1)
|
48 |
])
|
|
|
50 |
# Compile the model
|
51 |
lstm_model.compile(optimizer='adam', loss='mean_squared_error')
|
52 |
|
53 |
+
# Train the model with more epochs
|
54 |
+
lstm_model.fit(X_train, y_train, batch_size=32, epochs=50, verbose=0)
|
55 |
|
56 |
# Predict on the same data (just for demonstration)
|
57 |
predictions = lstm_model.predict(X_train)
|
58 |
+
predictions = scaler.inverse_transform(np.concatenate((predictions, np.zeros((predictions.shape[0], scaled_data.shape[1]-1))), axis=1))[:, 0] # Convert back to original scale
|
59 |
|
60 |
# Create a plot to show predictions
|
61 |
plt.figure(figsize=(10, 5))
|
62 |
+
plt.plot(df_close['Close'].values, label='Actual Stock Price', color='blue')
|
63 |
plt.plot(predictions, label='Predicted Stock Price', color='orange')
|
64 |
plt.title(f'{ticker} Stock Price Prediction')
|
65 |
plt.xlabel('Days')
|