Spaces:
Sleeping
Sleeping
File size: 2,655 Bytes
7452b23 | 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 | import streamlit as st
import pandas as pd
import numpy as np
import joblib
from datetime import datetime
# 1. Load the model and features
model = joblib.load('linear_regression_model.pkl')
features = joblib.load('model_features.pkl')
st.set_page_config(page_title="Store Sales Forecaster", layout="centered")
st.title("📈 Store Sales Forecasting")
st.markdown("""
This app predicts daily sales for a retail store in Ecuador using a **Linear Regression** model.
It accounts for promotions, oil prices, and special events like holidays and paydays.
""")
st.sidebar.header("User Input Parameters")
def user_input_features():
# Date Input
date = st.sidebar.date_input("Select Date", datetime(2017, 8, 16))
# Store and Family Input
store_nbr = st.sidebar.number_input("Store Number", min_value=1, max_value=54, value=1)
onpromotion = st.sidebar.number_input("Items on Promotion", min_value=0, value=0)
# Simplified dropdowns (we'll map these to the codes the model expects)
# Note: In a production app, you'd map these to the exact category codes used in training
family_code = st.sidebar.slider("Product Family Code", 0, 32, 10)
holiday_code = st.sidebar.slider("Holiday Type Code", 0, 6, 0)
store_type_code = st.sidebar.slider("Store Type Code", 0, 4, 0)
# Static or Derived Features
oil_price = st.sidebar.number_input("Current Oil Price (dcoilwtico)", value=45.0)
# Deriving date-based features
month = date.month
day_of_week = date.weekday()
is_payday = 1 if (date.day == 15 or date.day == pd.Period(date, freq='D').days_in_month) else 0
earthquake = 0 # Currently 0 as the event has passed
data = {
'store_nbr': store_nbr,
'family_code': family_code,
'onpromotion': onpromotion,
'dcoilwtico': oil_price,
'month': month,
'day_of_week': day_of_week,
'holiday_code': holiday_code,
'is_payday': is_payday,
'earthquake': earthquake,
'store_code': store_type_code
}
return pd.DataFrame(data, index=[0])
input_df = user_input_features()
st.subheader("Input Summary")
st.write(input_df)
# Prediction
if st.button("Predict Sales"):
# Reorder columns to match the trained model's features list
input_df = input_df[features]
prediction = model.predict(input_df)
# Apply clip to ensure no negative sales
result = np.clip(prediction[0], 0, None)
st.success(f"### Estimated Sales: {result:.2f} units")
st.info("Note: This prediction is based on a baseline Linear Regression model trained on historical data from 2013-2017.") |