Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,11 @@
|
|
1 |
import yfinance as yf
|
2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Fetch stock data from Yahoo Finance
|
5 |
def get_stock_data(ticker, start_date, end_date):
|
@@ -8,25 +14,12 @@ def get_stock_data(ticker, start_date, end_date):
|
|
8 |
raise ValueError("No data found for the given ticker and date range.")
|
9 |
return stock_data
|
10 |
|
11 |
-
# Example usage
|
12 |
-
ticker = 'AAPL'
|
13 |
-
start_date = '2023-01-01'
|
14 |
-
end_date = '2024-01-01'
|
15 |
-
data = get_stock_data(ticker, start_date, end_date)
|
16 |
-
print(data.head())
|
17 |
-
from sklearn.preprocessing import MinMaxScaler
|
18 |
-
|
19 |
# Preprocess stock data
|
20 |
def preprocess_data(stock_data):
|
21 |
scaler = MinMaxScaler(feature_range=(0, 1)) # Normalizing the close prices
|
22 |
scaled_data = scaler.fit_transform(stock_data[['Close']].values)
|
23 |
return scaled_data, scaler
|
24 |
|
25 |
-
# Preprocess the stock data
|
26 |
-
scaled_data, scaler = preprocess_data(data)
|
27 |
-
from tensorflow.keras.models import Sequential
|
28 |
-
from tensorflow.keras.layers import LSTM, Dense
|
29 |
-
|
30 |
# Define and train an LSTM model
|
31 |
def create_lstm_model(input_shape):
|
32 |
model = Sequential()
|
@@ -42,17 +35,14 @@ def train_model(stock_data, window_size=60):
|
|
42 |
for i in range(window_size, len(stock_data)):
|
43 |
X_train.append(stock_data[i-window_size:i, 0])
|
44 |
y_train.append(stock_data[i, 0])
|
45 |
-
X_train, y_train = np.array(X_train), np.array(y_train)
|
46 |
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
|
47 |
|
48 |
model = create_lstm_model((X_train.shape[1], 1))
|
49 |
model.fit(X_train, y_train, epochs=10, batch_size=32)
|
50 |
return model
|
51 |
|
52 |
-
#
|
53 |
-
lstm_model = train_model(scaled_data)
|
54 |
-
import numpy as np
|
55 |
-
|
56 |
def predict_future_prices(model, scaler, recent_data, days_to_predict=90):
|
57 |
predictions = []
|
58 |
input_data = recent_data[-60:].reshape(1, 60, 1) # Using the last 60 days to predict
|
@@ -65,12 +55,6 @@ def predict_future_prices(model, scaler, recent_data, days_to_predict=90):
|
|
65 |
predicted_prices = scaler.inverse_transform(np.array(predictions).reshape(-1, 1))
|
66 |
return predicted_prices
|
67 |
|
68 |
-
# Predict future stock prices
|
69 |
-
recent_data = scaled_data[-60:]
|
70 |
-
future_prices = predict_future_prices(lstm_model, scaler, recent_data)
|
71 |
-
import gradio as gr
|
72 |
-
import matplotlib.pyplot as plt
|
73 |
-
|
74 |
# Gradio function to predict stock prices and display results
|
75 |
def stock_prediction_app(ticker, start_date, end_date):
|
76 |
stock_data = get_stock_data(ticker, start_date, end_date)
|
|
|
1 |
import yfinance as yf
|
2 |
import pandas as pd
|
3 |
+
import numpy as np # Make sure to import numpy
|
4 |
+
from sklearn.preprocessing import MinMaxScaler
|
5 |
+
from tensorflow.keras.models import Sequential
|
6 |
+
from tensorflow.keras.layers import LSTM, Dense
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import gradio as gr
|
9 |
|
10 |
# Fetch stock data from Yahoo Finance
|
11 |
def get_stock_data(ticker, start_date, end_date):
|
|
|
14 |
raise ValueError("No data found for the given ticker and date range.")
|
15 |
return stock_data
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Preprocess stock data
|
18 |
def preprocess_data(stock_data):
|
19 |
scaler = MinMaxScaler(feature_range=(0, 1)) # Normalizing the close prices
|
20 |
scaled_data = scaler.fit_transform(stock_data[['Close']].values)
|
21 |
return scaled_data, scaler
|
22 |
|
|
|
|
|
|
|
|
|
|
|
23 |
# Define and train an LSTM model
|
24 |
def create_lstm_model(input_shape):
|
25 |
model = Sequential()
|
|
|
35 |
for i in range(window_size, len(stock_data)):
|
36 |
X_train.append(stock_data[i-window_size:i, 0])
|
37 |
y_train.append(stock_data[i, 0])
|
38 |
+
X_train, y_train = np.array(X_train), np.array(y_train) # Ensure numpy is used here
|
39 |
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
|
40 |
|
41 |
model = create_lstm_model((X_train.shape[1], 1))
|
42 |
model.fit(X_train, y_train, epochs=10, batch_size=32)
|
43 |
return model
|
44 |
|
45 |
+
# Predict future stock prices
|
|
|
|
|
|
|
46 |
def predict_future_prices(model, scaler, recent_data, days_to_predict=90):
|
47 |
predictions = []
|
48 |
input_data = recent_data[-60:].reshape(1, 60, 1) # Using the last 60 days to predict
|
|
|
55 |
predicted_prices = scaler.inverse_transform(np.array(predictions).reshape(-1, 1))
|
56 |
return predicted_prices
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Gradio function to predict stock prices and display results
|
59 |
def stock_prediction_app(ticker, start_date, end_date):
|
60 |
stock_data = get_stock_data(ticker, start_date, end_date)
|