Spaces:
Sleeping
Sleeping
File size: 8,311 Bytes
229dc69 9298950 229dc69 9298950 229dc69 | 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | import os
from pathlib import Path
import joblib
import numpy as np
import pandas as pd
import streamlit as st
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error, mean_squared_error
from tensorflow.keras.models import load_model
st.set_page_config(page_title="Stock LSTM Forecast", page_icon="π", layout="wide")
BASE_DIR = Path(__file__).resolve().parent
TICKERS = ["AAPL", "GOOG", "MSFT", "NFLX"]
FEATURE_COLS = ["Open", "High", "Low", "Close", "Volume"]
def find_file(name_candidates):
for name in name_candidates:
p = BASE_DIR / name
if p.exists():
return p
return None
@st.cache_resource
def load_artifacts(ticker: str):
model_path = find_file([f"{ticker}_lstm.keras", f"{ticker}_lstm.keras".lower(), f"{ticker}_lstm.keras".upper(),
f"{ticker}_lstm.keras".replace("_", ""), f"{ticker}_lstm.keras"])
# You used: "AAPL_lstm.keras", "GOOG_lstm.keras" ...
model_path = find_file([f"{ticker}_lstm.keras"])
x_scaler_path = find_file([f"{ticker}_x_scaler.pkl"])
y_scaler_path = find_file([f"{ticker}_y_scaler.pkl"])
if model_path is None or x_scaler_path is None or y_scaler_path is None:
missing = []
if model_path is None: missing.append(f"{ticker}_lstm.keras")
if x_scaler_path is None: missing.append(f"{ticker}_x_scaler.pkl")
if y_scaler_path is None: missing.append(f"{ticker}_y_scaler.pkl")
raise FileNotFoundError(f"Missing artifacts: {missing}. Make sure they are in the repo root.")
model = load_model(model_path)
x_scaler = joblib.load(x_scaler_path)
y_scaler = joblib.load(y_scaler_path)
return model, x_scaler, y_scaler
def load_data(uploaded_file):
# Default local file
if uploaded_file is None:
default_csv = find_file(["stocks.csv", "stocks"])
if default_csv is None:
raise FileNotFoundError("No stocks.csv found in repo. Upload a CSV in the sidebar or add stocks.csv to repo root.")
df = pd.read_csv(default_csv)
else:
df = pd.read_csv(uploaded_file)
# Basic cleanup
df.columns = [c.strip() for c in df.columns]
if "Date" not in df.columns or "Ticker" not in df.columns:
raise ValueError("CSV must contain at least columns: Ticker, Date, Open, High, Low, Close, Volume")
df["Date"] = pd.to_datetime(df["Date"])
df = df.sort_values(["Ticker", "Date"]).reset_index(drop=True)
# Ensure needed columns exist
for c in FEATURE_COLS + ["Adj Close"]:
if c not in df.columns:
# Adj Close is optional; we won't require it
pass
missing_feats = [c for c in FEATURE_COLS if c not in df.columns]
if missing_feats:
raise ValueError(f"Missing required feature columns: {missing_feats}")
return df
def make_sequences(X: np.ndarray, y: np.ndarray, lookback: int):
xs, ys = [], []
for i in range(lookback, len(X)):
xs.append(X[i - lookback:i])
ys.append(y[i])
return np.array(xs), np.array(ys)
def eval_on_test(df_ticker: pd.DataFrame, model, x_scaler, y_scaler, lookback: int, split_ratio: float = 0.8):
X = df_ticker[FEATURE_COLS]
y = df_ticker["Close"]
split = int(len(X) * split_ratio)
X_train = X.iloc[:split]
X_test = X.iloc[split:]
y_train = y.iloc[:split]
y_test = y.iloc[split:]
# Scale (fit on train only)
X_train_s = x_scaler.fit_transform(X_train)
X_test_s = x_scaler.transform(X_test)
y_train_s = y_scaler.fit_transform(y_train.values.reshape(-1, 1))
y_test_s = y_scaler.transform(y_test.values.reshape(-1, 1))
# Sequences
X_train_seq, y_train_seq = make_sequences(X_train_s, y_train_s, lookback)
X_test_seq, y_test_seq = make_sequences(X_test_s, y_test_s, lookback)
if len(X_test_seq) == 0:
raise ValueError(f"Not enough test rows to build sequences. "
f"Test rows={len(X_test_s)} must be > LOOKBACK={lookback}. "
f"Try smaller LOOKBACK.")
# Predict
y_pred_s = model.predict(X_test_seq, verbose=0)
# Inverse
y_pred = y_scaler.inverse_transform(y_pred_s).flatten()
y_true = y_scaler.inverse_transform(y_test_seq).flatten()
mae = mean_absolute_error(y_true, y_pred)
rmse = np.sqrt(mean_squared_error(y_true, y_pred))
return {
"mae": mae,
"rmse": rmse,
"y_true": y_true,
"y_pred": y_pred,
"test_dates": df_ticker["Date"].iloc[split:].reset_index(drop=True).iloc[lookback:].values
}
def next_day_forecast(df_ticker: pd.DataFrame, model, x_scaler, y_scaler, lookback: int):
# Fit scalers on ALL available data for "latest window" inference (common for deployment)
X_all = df_ticker[FEATURE_COLS].values
y_all = df_ticker["Close"].values.reshape(-1, 1)
X_all_s = x_scaler.fit_transform(X_all)
y_all_s = y_scaler.fit_transform(y_all)
if len(X_all_s) < lookback:
raise ValueError(f"Not enough rows for LOOKBACK={lookback}. Need at least {lookback} rows.")
last_window = X_all_s[-lookback:] # (lookback, features)
last_window = np.expand_dims(last_window, axis=0) # (1, lookback, features)
pred_s = model.predict(last_window, verbose=0)
pred = y_scaler.inverse_transform(pred_s).flatten()[0]
last_date = df_ticker["Date"].iloc[-1]
return pred, last_date
# ---------------- UI ----------------
st.title("π Multivariate Stock Forecasting (LSTM)")
st.caption("Train/test evaluation + next-step forecast for AAPL, GOOG, MSFT, NFLX.")
with st.sidebar:
st.header("Settings")
uploaded = st.file_uploader("Upload CSV (optional)", type=["csv"])
ticker = st.selectbox("Select Ticker", TICKERS, index=0)
lookback = st.slider("LOOKBACK (timesteps)", min_value=3, max_value=20, value=5, step=1)
split_ratio = st.slider("Train/Test split ratio", 0.6, 0.9, 0.8, 0.05)
run_btn = st.button("Run Forecast")
try:
df = load_data(uploaded)
except Exception as e:
st.error(str(e))
st.stop()
df_t = df[df["Ticker"] == ticker].copy().sort_values("Date").reset_index(drop=True)
if df_t.empty:
st.error(f"No rows found for ticker: {ticker}")
st.stop()
# Quick preview
c1, c2, c3 = st.columns(3)
c1.metric("Rows", len(df_t))
c2.metric("Date start", str(df_t["Date"].min().date()))
c3.metric("Date end", str(df_t["Date"].max().date()))
st.subheader("Data preview")
st.dataframe(df_t.head(10), use_container_width=True)
# Load artifacts for selected ticker
try:
model, x_scaler, y_scaler = load_artifacts(ticker)
except Exception as e:
st.error(str(e))
st.stop()
if run_btn:
st.subheader("Test evaluation (chronological split)")
try:
out = eval_on_test(df_t, model, x_scaler, y_scaler, lookback=lookback, split_ratio=split_ratio)
except Exception as e:
st.error(str(e))
st.stop()
m1, m2 = st.columns(2)
m1.metric("Test MAE (USD)", f"{out['mae']:.3f}")
m2.metric("Test RMSE (USD)", f"{out['rmse']:.3f}")
# Plot actual vs predicted
fig = plt.figure(figsize=(12, 5))
plt.plot(out["test_dates"], out["y_true"], label="Actual")
plt.plot(out["test_dates"], out["y_pred"], label="Predicted")
plt.title(f"{ticker} LSTM β Actual vs Predicted Close")
plt.xlabel("Date")
plt.ylabel("Close Price")
plt.legend()
st.pyplot(fig)
# Residuals
resid = out["y_true"] - out["y_pred"]
fig2 = plt.figure(figsize=(12, 4))
plt.plot(out["test_dates"], resid)
plt.axhline(0, linestyle="--")
plt.title("Residuals (Actual - Predicted)")
plt.xlabel("Date")
plt.ylabel("Error (USD)")
st.pyplot(fig2)
# Error histogram
fig3 = plt.figure(figsize=(7, 4))
plt.hist(resid, bins=10)
plt.title("Distribution of Prediction Errors")
plt.xlabel("Error (USD)")
plt.ylabel("Frequency")
st.pyplot(fig3)
st.subheader("Next-step forecast (latest window)")
try:
pred_next, last_date = next_day_forecast(df_t, model, x_scaler, y_scaler, lookback=lookback)
st.success(f"Predicted next close after {last_date.date()} for **{ticker}**: **{pred_next:.2f} USD**")
except Exception as e:
st.warning(str(e))
else:
st.info("Click **Run Forecast** in the sidebar to evaluate and forecast.") |