Azrieldr
commited on
Commit
•
79d2ded
1
Parent(s):
101eb9f
initial commit
Browse files- Untitled-1.ipynb +19 -0
- app.py +93 -0
- requirements.txt +9 -0
- sample_dataset_timeseries_noarea.csv +0 -0
Untitled-1.ipynb
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": []
|
9 |
+
}
|
10 |
+
],
|
11 |
+
"metadata": {
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
},
|
15 |
+
"orig_nbformat": 4
|
16 |
+
},
|
17 |
+
"nbformat": 4,
|
18 |
+
"nbformat_minor": 2
|
19 |
+
}
|
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from statsmodels.tsa.stattools import adfuller
|
6 |
+
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
|
7 |
+
from sklearn.metrics import mean_squared_error, mean_absolute_error
|
8 |
+
from math import sqrt
|
9 |
+
|
10 |
+
data= pd.read_csv('sample_dataset_timeseries_noarea.csv')
|
11 |
+
#menggunakan dictionary untuk menyimpan tabel dari setiap product
|
12 |
+
def table_generator(df, listname):
|
13 |
+
name={}
|
14 |
+
n= len(listname)
|
15 |
+
for i in range(n):
|
16 |
+
name[i]=df[df['product_item']==listname[i]]
|
17 |
+
return name
|
18 |
+
# menghitung berapa banyak kejadian pembelian setiap produk
|
19 |
+
value_counts= data['product_item'].value_counts()
|
20 |
+
|
21 |
+
#membuat tabel baru dengan hanya produk yang dibeli di seluruh minggu
|
22 |
+
tabel_baru = data.loc[data['product_item'].isin(value_counts[value_counts == 67].index)]
|
23 |
+
|
24 |
+
# Kelompokkan data berdasarkan product_item dan hitung total quantity
|
25 |
+
grouped_data = tabel_baru.groupby('product_item')['quantity'].sum()
|
26 |
+
|
27 |
+
# Urutkan data secara menurun berdasarkan total quantity
|
28 |
+
sorted_data = grouped_data.sort_values(ascending=False)
|
29 |
+
|
30 |
+
# Ambil product_item dengan total quantity terbanyak
|
31 |
+
top_product_item = sorted_data.index[0]
|
32 |
+
|
33 |
+
sorted_data2= sorted_data.head(10)
|
34 |
+
|
35 |
+
product_item_names = sorted_data2.index.tolist()
|
36 |
+
new_data = data[data['product_item'].isin(product_item_names)]
|
37 |
+
name= table_generator(data, product_item_names)
|
38 |
+
week= new_data.groupby('week_number')['quantity'].sum()
|
39 |
+
# Ubah series menjadi dataframe
|
40 |
+
df = week.to_frame().reset_index()
|
41 |
+
|
42 |
+
# Ganti nama kolom dari indeks menjadi 'Week Number'
|
43 |
+
week = df.rename(columns={'index': 'Week Number'})
|
44 |
+
#splitting train and test
|
45 |
+
train=week[0:53]
|
46 |
+
test=week[53:]
|
47 |
+
y_hat_avg = test.copy()
|
48 |
+
fit1 = ExponentialSmoothing(np.asarray(train['quantity']), seasonal_periods=13, trend='multiplicative', seasonal='multiplicative').fit()
|
49 |
+
y_hat_avg['Holt_Winter'] = fit1.forecast(len(test))
|
50 |
+
|
51 |
+
# Create the plot
|
52 |
+
fig, ax = plt.subplots(figsize=(20, 8))
|
53 |
+
ax.plot(train['quantity'], label='Train')
|
54 |
+
ax.plot(test['quantity'], label='Test')
|
55 |
+
ax.plot(y_hat_avg['Holt_Winter'], label='Holt_Winter')
|
56 |
+
ax.legend(loc='best')
|
57 |
+
|
58 |
+
# Show the plot in Streamlit
|
59 |
+
st.pyplot(fig)
|
60 |
+
|
61 |
+
def forecasting(x):
|
62 |
+
predict= fit1.forecast(len(test)+x)
|
63 |
+
predict= predict[-x:]
|
64 |
+
rounded_arr = []
|
65 |
+
for num in predict:
|
66 |
+
rounded_num = round(num)
|
67 |
+
rounded_arr.append(rounded_num)
|
68 |
+
return rounded_arr
|
69 |
+
number = st.number_input("Enter a number between 1 and 10 to predict upcoming weeks", min_value=1, max_value=10)
|
70 |
+
num=forecasting(number)
|
71 |
+
st.write(num)
|
72 |
+
num1 = st.number_input("forecast a product", min_value=0, max_value=9)
|
73 |
+
def holtz_generator(df):
|
74 |
+
df=df.drop(columns=['week_start_date','week_end_date','product_item'])
|
75 |
+
n=0.8*len(df)
|
76 |
+
n=round(n)
|
77 |
+
#splitting train and test
|
78 |
+
train=df.iloc[0:n]
|
79 |
+
test=df.iloc[n:]
|
80 |
+
|
81 |
+
y_hat_avg = test
|
82 |
+
fit1 = ExponentialSmoothing(np.asarray(train['quantity']) ,seasonal_periods=8 ,trend='multiplicative', seasonal='multiplicative').fit()
|
83 |
+
y_hat_avg['Holt_Winter'] = fit1.forecast(len(test))
|
84 |
+
# Create the plot
|
85 |
+
fig, ax = plt.subplots(figsize=(20, 8))
|
86 |
+
ax.plot(train['quantity'], label='Train')
|
87 |
+
ax.plot(test['quantity'], label='Test')
|
88 |
+
ax.plot(y_hat_avg['Holt_Winter'], label='Holt_Winter')
|
89 |
+
ax.legend(loc='best')
|
90 |
+
# Show the plot in Streamlit
|
91 |
+
st.pyplot(fig)
|
92 |
+
|
93 |
+
holtz_generator(name[num1])
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
matplotlib==3.5.2
|
2 |
+
numpy==1.21.5
|
3 |
+
numpy==1.18.0
|
4 |
+
numpy==1.20.0
|
5 |
+
pandas==1.4.4
|
6 |
+
scikit_learn==1.0.2
|
7 |
+
statsmodels==0.13.2
|
8 |
+
streamlit==1.21.0
|
9 |
+
~treamlit==1.20.0
|
sample_dataset_timeseries_noarea.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|