File size: 2,202 Bytes
6fc1ba0
 
 
 
b25aa06
6fc1ba0
 
b25aa06
6fc1ba0
b25aa06
b6c3bcc
b25aa06
b6c3bcc
b25aa06
 
 
 
 
6fc1ba0
b25aa06
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc1ba0
57ef87d
6fc1ba0
b25aa06
57ef87d
b25aa06
 
 
 
 
 
 
 
 
 
 
 
57ef87d
b25aa06
 
 
 
 
 
 
 
 
 
6fc1ba0
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
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import MinMaxScaler
import pickle
import streamlit as st
import matplotlib.pyplot as plt

# Load all files
with open('linreg_model.pkl', 'rb') as file_1:
    model_lr = pickle.load(file_1)
with open('num_columns.pkl', 'rb') as file_2:
    scaler = pickle.load(file_2)

quantity_flow1 = pd.read_csv('quantity_flow1.csv')
quantity_flow1['week_start_date'] = pd.to_datetime(quantity_flow1['week_start_date'])
quantity_flow1.set_index('week_start_date', inplace=True)

# Define the forecasting function
def forecasting(month):
    # :param month: how many months to predict

    quantity_forecast = quantity_flow1.copy()
    window = 2
    for i in range(month):
        X = np.array(quantity_forecast[-window:].values).reshape(1, -1)
        X_scaled = scaler.transform(X)
        new_idx = quantity_forecast.index[-1] + timedelta(weeks=1)
        quantity_forecast.loc[new_idx] = round(model_lr.predict(X_scaled)[0])
    return quantity_forecast.tail(month + window)

# Define the Streamlit app
def run():
    st.title("Quantity Sales Forecasting")
    
    # Input the number of months to forecast
    months_to_forecast = st.slider("How many weeks to forecast?", 1, 13, 3)

    # Predict button
    if st.button("Predict"):
        # Get the forecast
        quantity_forecast = forecasting(months_to_forecast)

        # Display the forecast plot
        fig, ax = plt.subplots(figsize=(20,5))
        ax.plot(quantity_flow1, color='red', label='Real Quantity')
        ax.plot(quantity_forecast, color='blue', label='Quantity Forecast')
        ax.set_xlabel('Date')
        ax.set_ylabel('Quantity')
        ax.set_title(f"Quantity Forecast for the Next {months_to_forecast} Weeks")
        ax.legend()
        st.pyplot(fig)

        # Reset the index of quantity_forecast and rename the index column to "Date"
        quantity_forecast_table = quantity_forecast.reset_index().rename(columns={"index": "Date"})

        # Display the forecasted sales in a table
        st.write(quantity_forecast_table)

if __name__ == '__main__':
    run()