File size: 3,371 Bytes
79d2ded |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
from sklearn.metrics import mean_squared_error, mean_absolute_error
from math import sqrt
data= pd.read_csv('sample_dataset_timeseries_noarea.csv')
#menggunakan dictionary untuk menyimpan tabel dari setiap product
def table_generator(df, listname):
name={}
n= len(listname)
for i in range(n):
name[i]=df[df['product_item']==listname[i]]
return name
# menghitung berapa banyak kejadian pembelian setiap produk
value_counts= data['product_item'].value_counts()
#membuat tabel baru dengan hanya produk yang dibeli di seluruh minggu
tabel_baru = data.loc[data['product_item'].isin(value_counts[value_counts == 67].index)]
# Kelompokkan data berdasarkan product_item dan hitung total quantity
grouped_data = tabel_baru.groupby('product_item')['quantity'].sum()
# Urutkan data secara menurun berdasarkan total quantity
sorted_data = grouped_data.sort_values(ascending=False)
# Ambil product_item dengan total quantity terbanyak
top_product_item = sorted_data.index[0]
sorted_data2= sorted_data.head(10)
product_item_names = sorted_data2.index.tolist()
new_data = data[data['product_item'].isin(product_item_names)]
name= table_generator(data, product_item_names)
week= new_data.groupby('week_number')['quantity'].sum()
# Ubah series menjadi dataframe
df = week.to_frame().reset_index()
# Ganti nama kolom dari indeks menjadi 'Week Number'
week = df.rename(columns={'index': 'Week Number'})
#splitting train and test
train=week[0:53]
test=week[53:]
y_hat_avg = test.copy()
fit1 = ExponentialSmoothing(np.asarray(train['quantity']), seasonal_periods=13, trend='multiplicative', seasonal='multiplicative').fit()
y_hat_avg['Holt_Winter'] = fit1.forecast(len(test))
# Create the plot
fig, ax = plt.subplots(figsize=(20, 8))
ax.plot(train['quantity'], label='Train')
ax.plot(test['quantity'], label='Test')
ax.plot(y_hat_avg['Holt_Winter'], label='Holt_Winter')
ax.legend(loc='best')
# Show the plot in Streamlit
st.pyplot(fig)
def forecasting(x):
predict= fit1.forecast(len(test)+x)
predict= predict[-x:]
rounded_arr = []
for num in predict:
rounded_num = round(num)
rounded_arr.append(rounded_num)
return rounded_arr
number = st.number_input("Enter a number between 1 and 10 to predict upcoming weeks", min_value=1, max_value=10)
num=forecasting(number)
st.write(num)
num1 = st.number_input("forecast a product", min_value=0, max_value=9)
def holtz_generator(df):
df=df.drop(columns=['week_start_date','week_end_date','product_item'])
n=0.8*len(df)
n=round(n)
#splitting train and test
train=df.iloc[0:n]
test=df.iloc[n:]
y_hat_avg = test
fit1 = ExponentialSmoothing(np.asarray(train['quantity']) ,seasonal_periods=8 ,trend='multiplicative', seasonal='multiplicative').fit()
y_hat_avg['Holt_Winter'] = fit1.forecast(len(test))
# Create the plot
fig, ax = plt.subplots(figsize=(20, 8))
ax.plot(train['quantity'], label='Train')
ax.plot(test['quantity'], label='Test')
ax.plot(y_hat_avg['Holt_Winter'], label='Holt_Winter')
ax.legend(loc='best')
# Show the plot in Streamlit
st.pyplot(fig)
holtz_generator(name[num1])
|