-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecastfix.py
74 lines (62 loc) · 1.89 KB
/
forecastfix.py
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
# -*- coding: utf-8 -*-
"""ForecastFix.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/14PCmGiF7VVNLDdlvnjlXs2KI5v4apF5V
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
csv_dataset = pd.read_excel("datawaldi2.xlsx")
csv_dataset.plot()
plt.show()
optimal_alpha = None
best_mse = None
db = csv_dataset.iloc[:, :].values.astype('float32')
mean_results_for_all_possible_alpha_values = np.zeros(9)
for alpha in range(0, 9):
pt = np.mean(db[:, 0][0:5])
mean_for_alpha = np.zeros(len(db))
mean_for_alpha[0] = np.power(db[0][0] - pt, 2)
for i in range(1, len(db)):
pt = pt + ((alpha + 1) * 0.1) * (db[i - 1][0] - pt)
mean_for_alpha[i] = np.power(db[i][0] - pt, 2)
mean_results_for_all_possible_alpha_values[alpha] = np.mean(mean_for_alpha)
optimal_alpha = (np.argmin(mean_results_for_all_possible_alpha_values) + 1) * 0.1
best_mse = np.min(mean_results_for_all_possible_alpha_values)
print("Best MSE = %s" % best_mse)
print("Optimal alpha = %s" % optimal_alpha)
pt = np.mean(db[:, 0][0:5])
for i in range(1, len(db) + 1):
pt = pt + optimal_alpha * (db[i - 1][0] - pt)
print("Next observation = %s" % pt)
forecast = np.zeros(len(db) + 1)
pt = np.mean(db[:, 0][0:5])
forecast[0] = pt
for i in range(1, len(db) + 1):
pt = pt + optimal_alpha * (db[i - 1][0] - pt)
forecast[i] = pt
plt.plot(db[:, 0],label = 'Data Asli')
plt.plot(forecast, label = 'Data Peramalan')
plt.legend()
plt.show()
st.header('UAS Forecasting')
fig, ax = plt.subplots(figsize=(16, 8))
ax.plot(
csv_dataset["Penjualan Y"],
marker="o",
linewidth=2,
color="#85CAF9"
)
st.subheader('Data Asli')
st.pyplot(fig)
fig2, ay = plt.subplots(figsize=(16, 8))
ay.plot(
forecast[i],
marker="o",
linewidth=2,
color="#FF8C00"
)
st.subheader('Data Forecast')
st.pyplot(fig2)