Spaces:
Runtime error
Runtime error
Final Submission
Browse files- prediction.py +49 -34
- quantity_flow1.csv +68 -68
prediction.py
CHANGED
@@ -1,47 +1,62 @@
|
|
1 |
import numpy as np
|
2 |
import pandas as pd
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
from datetime import datetime, timedelta
|
5 |
from sklearn.linear_model import LinearRegression
|
6 |
-
from sklearn.preprocessing import
|
7 |
import pickle
|
8 |
import streamlit as st
|
|
|
9 |
|
10 |
-
# Load
|
11 |
-
|
12 |
with open('linreg_model.pkl', 'rb') as file_1:
|
13 |
-
|
14 |
with open('num_columns.pkl', 'rb') as file_2:
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def run():
|
18 |
-
|
19 |
-
quantity_flow1 = pd.read_csv('quantity_flow1.csv', index_col=0, parse_dates=True)
|
20 |
-
|
21 |
-
# Function to Predict Quantity in Next n-weeks
|
22 |
-
def forecasting(month):
|
23 |
-
quantity_forecast = quantity_flow1.copy()
|
24 |
-
window = 2
|
25 |
-
for i in range(month):
|
26 |
-
X = np.array(quantity_forecast[-window:].values).reshape(1, -1)
|
27 |
-
X_scaled = scaler.transform(X)
|
28 |
-
new_idx = quantity_forecast.index[-1] + timedelta(weeks=1)
|
29 |
-
quantity_forecast[new_idx] = round(model_lr.predict(X_scaled)[0])
|
30 |
-
return quantity_forecast
|
31 |
|
32 |
-
#
|
33 |
-
st.
|
34 |
-
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
run()
|
|
|
1 |
import numpy as np
|
2 |
import pandas as pd
|
|
|
3 |
from datetime import datetime, timedelta
|
4 |
from sklearn.linear_model import LinearRegression
|
5 |
+
from sklearn.preprocessing import MinMaxScaler
|
6 |
import pickle
|
7 |
import streamlit as st
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
|
10 |
+
# Load all files
|
|
|
11 |
with open('linreg_model.pkl', 'rb') as file_1:
|
12 |
+
model_lr = pickle.load(file_1)
|
13 |
with open('num_columns.pkl', 'rb') as file_2:
|
14 |
+
scaler = pickle.load(file_2)
|
15 |
+
|
16 |
+
quantity_flow1 = pd.read_csv('quantity_flow1.csv')
|
17 |
+
quantity_flow1['week_start_date'] = pd.to_datetime(quantity_flow1['week_start_date'])
|
18 |
+
quantity_flow1.set_index('week_start_date', inplace=True)
|
19 |
|
20 |
+
# Define the forecasting function
|
21 |
+
def forecasting(month):
|
22 |
+
# :param month: how many months to predict
|
23 |
+
|
24 |
+
quantity_forecast = quantity_flow1.copy()
|
25 |
+
window = 2
|
26 |
+
for i in range(month):
|
27 |
+
X = np.array(quantity_forecast[-window:].values).reshape(1, -1)
|
28 |
+
X_scaled = scaler.transform(X)
|
29 |
+
new_idx = quantity_forecast.index[-1] + timedelta(weeks=1)
|
30 |
+
quantity_forecast.loc[new_idx] = round(model_lr.predict(X_scaled)[0])
|
31 |
+
return quantity_forecast.tail(month + window)
|
32 |
+
|
33 |
+
# Define the Streamlit app
|
34 |
def run():
|
35 |
+
st.title("Quantity Forecasting App")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Input the number of months to forecast
|
38 |
+
months_to_forecast = st.slider("How many months to forecast?", 1, 13, 3)
|
39 |
+
|
40 |
+
# Predict button
|
41 |
+
if st.button("Predict"):
|
42 |
+
# Get the forecast
|
43 |
+
quantity_forecast = forecasting(months_to_forecast)
|
44 |
+
|
45 |
+
# Display the forecast plot
|
46 |
+
fig, ax = plt.subplots(figsize=(20,5))
|
47 |
+
ax.plot(quantity_flow1, color='red', label='Real Quantity')
|
48 |
+
ax.plot(quantity_forecast, color='blue', label='Quantity Forecast')
|
49 |
+
ax.set_xlabel('Date')
|
50 |
+
ax.set_ylabel('Quantity')
|
51 |
+
ax.set_title(f"Quantity Forecast for the Next {months_to_forecast} Months")
|
52 |
+
ax.legend()
|
53 |
+
st.pyplot(fig)
|
54 |
+
|
55 |
+
# Reset the index of quantity_forecast and rename the index column to "Date"
|
56 |
+
quantity_forecast_table = quantity_forecast.reset_index().rename(columns={"index": "Date"})
|
57 |
+
|
58 |
+
# Display the forecasted sales in a table
|
59 |
+
st.write(quantity_forecast_table)
|
60 |
+
|
61 |
+
if __name__ == '__main__':
|
62 |
run()
|
quantity_flow1.csv
CHANGED
@@ -1,68 +1,68 @@
|
|
1 |
-
quantity
|
2 |
-
128808
|
3 |
-
3839557
|
4 |
-
4138934
|
5 |
-
4149077
|
6 |
-
4824273
|
7 |
-
4008027
|
8 |
-
4311010
|
9 |
-
3882636
|
10 |
-
4256541
|
11 |
-
3883810
|
12 |
-
5047898
|
13 |
-
4303200
|
14 |
-
4980761
|
15 |
-
4620208
|
16 |
-
4553719
|
17 |
-
4921338
|
18 |
-
5768747
|
19 |
-
4684319
|
20 |
-
1334134
|
21 |
-
7172144
|
22 |
-
5505616
|
23 |
-
5394786
|
24 |
-
5817233
|
25 |
-
6260083
|
26 |
-
5759699
|
27 |
-
5709933
|
28 |
-
5556481
|
29 |
-
5275889
|
30 |
-
5682207
|
31 |
-
5824993
|
32 |
-
5900392
|
33 |
-
6143352
|
34 |
-
6256332
|
35 |
-
5021634
|
36 |
-
5918497
|
37 |
-
6112693
|
38 |
-
5237531
|
39 |
-
5554898
|
40 |
-
5343014
|
41 |
-
5912301
|
42 |
-
4907074
|
43 |
-
5269967
|
44 |
-
4229764
|
45 |
-
4878485
|
46 |
-
4661839
|
47 |
-
5234579
|
48 |
-
4861820
|
49 |
-
4465411
|
50 |
-
4953197
|
51 |
-
5717901
|
52 |
-
5259200
|
53 |
-
5768419
|
54 |
-
4677997
|
55 |
-
4111427
|
56 |
-
4623965
|
57 |
-
5265632
|
58 |
-
4364421
|
59 |
-
4703388
|
60 |
-
4267946
|
61 |
-
3731588
|
62 |
-
4701262
|
63 |
-
3732954
|
64 |
-
4775638
|
65 |
-
4787111
|
66 |
-
4757590
|
67 |
-
6001166
|
68 |
-
4172070
|
|
|
1 |
+
week_start_date,quantity
|
2 |
+
2022-01-02,128808
|
3 |
+
2022-01-09,3839557
|
4 |
+
2022-01-16,4138934
|
5 |
+
2022-01-23,4149077
|
6 |
+
2022-01-30,4824273
|
7 |
+
2022-02-06,4008027
|
8 |
+
2022-02-13,4311010
|
9 |
+
2022-02-20,3882636
|
10 |
+
2022-02-27,4256541
|
11 |
+
2022-03-06,3883810
|
12 |
+
2022-03-13,5047898
|
13 |
+
2022-03-20,4303200
|
14 |
+
2022-03-27,4980761
|
15 |
+
2022-04-03,4620208
|
16 |
+
2022-04-10,4553719
|
17 |
+
2022-04-17,4921338
|
18 |
+
2022-04-24,5768747
|
19 |
+
2022-05-01,4684319
|
20 |
+
2022-05-08,1334134
|
21 |
+
2022-05-15,7172144
|
22 |
+
2022-05-22,5505616
|
23 |
+
2022-05-29,5394786
|
24 |
+
2022-06-05,5817233
|
25 |
+
2022-06-12,6260083
|
26 |
+
2022-06-19,5759699
|
27 |
+
2022-06-26,5709933
|
28 |
+
2022-07-03,5556481
|
29 |
+
2022-07-10,5275889
|
30 |
+
2022-07-17,5682207
|
31 |
+
2022-07-24,5824993
|
32 |
+
2022-07-31,5900392
|
33 |
+
2022-08-07,6143352
|
34 |
+
2022-08-14,6256332
|
35 |
+
2022-08-21,5021634
|
36 |
+
2022-08-28,5918497
|
37 |
+
2022-09-04,6112693
|
38 |
+
2022-09-11,5237531
|
39 |
+
2022-09-18,5554898
|
40 |
+
2022-09-25,5343014
|
41 |
+
2022-10-02,5912301
|
42 |
+
2022-10-09,4907074
|
43 |
+
2022-10-16,5269967
|
44 |
+
2022-10-23,4229764
|
45 |
+
2022-10-30,4878485
|
46 |
+
2022-11-06,4661839
|
47 |
+
2022-11-13,5234579
|
48 |
+
2022-11-20,4861820
|
49 |
+
2022-11-27,4465411
|
50 |
+
2022-12-04,4953197
|
51 |
+
2022-12-11,5717901
|
52 |
+
2022-12-18,5259200
|
53 |
+
2022-12-25,5768419
|
54 |
+
2023-01-01,4677997
|
55 |
+
2023-01-08,4111427
|
56 |
+
2023-01-15,4623965
|
57 |
+
2023-01-22,5265632
|
58 |
+
2023-01-29,4364421
|
59 |
+
2023-02-05,4703388
|
60 |
+
2023-02-12,4267946
|
61 |
+
2023-02-19,3731588
|
62 |
+
2023-02-26,4701262
|
63 |
+
2023-03-05,3732954
|
64 |
+
2023-03-12,4775638
|
65 |
+
2023-03-19,4787111
|
66 |
+
2023-03-26,4757590
|
67 |
+
2023-04-02,6001166
|
68 |
+
2023-04-09,4172070
|