Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
4 |
+
import numpy as np
|
5 |
+
from itertools import product
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Function to run the Exponential Smoothing Model
|
9 |
+
def run_exp_smoothing(city_data, trend, damped_trend, seasonal, seasonal_period):
|
10 |
+
try:
|
11 |
+
model = ExponentialSmoothing(city_data, trend=trend, damped_trend=damped_trend, seasonal=seasonal, seasonal_periods=seasonal_period)
|
12 |
+
model_fit = model.fit(optimized=True)
|
13 |
+
return model_fit.forecast(steps=6), model_fit.aic
|
14 |
+
except Exception as e:
|
15 |
+
return None, None
|
16 |
+
|
17 |
+
# Function to convert DataFrame to Excel
|
18 |
+
def to_excel(df):
|
19 |
+
output = BytesIO()
|
20 |
+
writer = pd.ExcelWriter(output, engine='xlsxwriter')
|
21 |
+
df.to_excel(writer, sheet_name='Sheet1')
|
22 |
+
writer.save()
|
23 |
+
processed_data = output.getvalue()
|
24 |
+
return processed_data
|
25 |
+
|
26 |
+
# Streamlit app starts here
|
27 |
+
def main():
|
28 |
+
st.title("Exponential Smoothing Forecasting")
|
29 |
+
|
30 |
+
# Upload Data Section
|
31 |
+
uploaded_file = st.file_uploader("Choose a file")
|
32 |
+
if uploaded_file is not None:
|
33 |
+
data = pd.read_csv(uploaded_file)
|
34 |
+
unique_cities = data['City'].unique()
|
35 |
+
|
36 |
+
# Select a city
|
37 |
+
selected_city = st.selectbox('Select a City', unique_cities)
|
38 |
+
|
39 |
+
# Sliders for parameter adjustment
|
40 |
+
trend = st.select_slider('Select Trend', options=['add', 'mul', None])
|
41 |
+
damped_trend = st.checkbox('Damped Trend')
|
42 |
+
seasonal = st.select_slider('Select Seasonal', options=['add', 'mul', None])
|
43 |
+
seasonal_period = st.slider('Seasonal Period', 1, 24, 12)
|
44 |
+
|
45 |
+
# Display forecast with current parameters
|
46 |
+
city_data = data[data['City'] == selected_city]['Accident Count']
|
47 |
+
forecast, aic = run_exp_smoothing(city_data, trend, damped_trend, seasonal, seasonal_period)
|
48 |
+
if forecast is not None:
|
49 |
+
st.write(f"Forecast with AIC: {aic}")
|
50 |
+
st.line_chart(forecast)
|
51 |
+
|
52 |
+
# Grid search button
|
53 |
+
if st.button('Run Grid Search'):
|
54 |
+
best_aic = float('inf')
|
55 |
+
best_params = None
|
56 |
+
for param_set in product(['add', 'mul', None], [True, False], ['add', 'mul', None], [12]):
|
57 |
+
_, temp_aic = run_exp_smoothing(city_data, *param_set)
|
58 |
+
if temp_aic and temp_aic < best_aic:
|
59 |
+
best_aic = temp_aic
|
60 |
+
best_params = param_set
|
61 |
+
st.write(f"Best Parameters: {best_params} with AIC: {best_aic}")
|
62 |
+
|
63 |
+
# Export to Excel button
|
64 |
+
if st.button('Export to Excel'):
|
65 |
+
df_to_export = pd.DataFrame(forecast)
|
66 |
+
excel_data = to_excel(df_to_export)
|
67 |
+
st.download_button(label='📥 Download Excel', data=excel_data, file_name='forecast.xlsx', mime='application/vnd.ms-excel')
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
main()
|