|
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') |
|
|
|
def table_generator(df, listname): |
|
name={} |
|
n= len(listname) |
|
for i in range(n): |
|
name[i]=df[df['product_item']==listname[i]] |
|
return name |
|
|
|
value_counts= data['product_item'].value_counts() |
|
|
|
|
|
tabel_baru = data.loc[data['product_item'].isin(value_counts[value_counts == 67].index)] |
|
|
|
|
|
grouped_data = tabel_baru.groupby('product_item')['quantity'].sum() |
|
|
|
|
|
sorted_data = grouped_data.sort_values(ascending=False) |
|
|
|
|
|
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() |
|
|
|
df = week.to_frame().reset_index() |
|
|
|
|
|
week = df.rename(columns={'index': 'Week Number'}) |
|
|
|
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)) |
|
|
|
|
|
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') |
|
|
|
|
|
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) |
|
|
|
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)) |
|
|
|
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') |
|
|
|
st.pyplot(fig) |
|
|
|
holtz_generator(name[num1]) |
|
|