vincentiusyoshuac commited on
Commit
631262b
·
verified ·
1 Parent(s): db99fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -15,10 +15,15 @@ class TimeSeriesForecaster:
15
  self.original_series = None
16
  self.context = None
17
 
18
- def preprocess_data(self, df, date_column, value_column, context_length=100, prediction_length=365):
19
  """
20
  Prepare time series data from DataFrame
21
  """
 
 
 
 
 
22
  # Ensure data is sorted by date
23
  df = df.sort_values(by=date_column)
24
 
@@ -34,12 +39,15 @@ class TimeSeriesForecaster:
34
  # Convert to tensor
35
  self.context = torch.tensor(self.original_series[-context_length:], dtype=torch.float32)
36
 
37
- return self.context, context_length
38
 
39
- def forecast(self, context, prediction_length=365, num_samples=100):
40
  """
41
  Perform time series forecasting
42
  """
 
 
 
43
  forecasts = self.pipeline.predict(context, prediction_length, num_samples=num_samples)
44
  return forecasts
45
 
@@ -85,7 +93,7 @@ class TimeSeriesForecaster:
85
  label='90% Prediction Interval'
86
  )
87
 
88
- plt.title('Advanced Time Series Forecast', fontsize=18, fontweight='bold', color='#2C3E50')
89
  plt.xlabel('Time Steps', fontsize=12, color='#34495E')
90
  plt.ylabel('Value', fontsize=12, color='#34495E')
91
  plt.legend(frameon=False)
@@ -188,10 +196,10 @@ def main():
188
  with col4:
189
  prediction_length = st.slider(
190
  'Forecast Horizon',
191
- min_value=30,
192
- max_value=1000,
193
- value=365,
194
- help="Number of future time steps to predict"
195
  )
196
 
197
  # Forecast generation
@@ -201,7 +209,7 @@ def main():
201
  forecaster = TimeSeriesForecaster()
202
 
203
  # Preprocess data
204
- context, _ = forecaster.preprocess_data(
205
  df,
206
  date_column,
207
  value_column,
 
15
  self.original_series = None
16
  self.context = None
17
 
18
+ def preprocess_data(self, df, date_column, value_column, context_length=100, prediction_length=30):
19
  """
20
  Prepare time series data from DataFrame
21
  """
22
+ # Validasi panjang prediksi
23
+ if prediction_length > 30:
24
+ st.warning("Prediction length dibatasi maksimal 30 langkah. Akan disesuaikan.")
25
+ prediction_length = min(prediction_length, 30)
26
+
27
  # Ensure data is sorted by date
28
  df = df.sort_values(by=date_column)
29
 
 
39
  # Convert to tensor
40
  self.context = torch.tensor(self.original_series[-context_length:], dtype=torch.float32)
41
 
42
+ return self.context, prediction_length
43
 
44
+ def forecast(self, context, prediction_length=30, num_samples=100):
45
  """
46
  Perform time series forecasting
47
  """
48
+ # Pastikan prediksi tidak melebihi 30 langkah
49
+ prediction_length = min(prediction_length, 30)
50
+
51
  forecasts = self.pipeline.predict(context, prediction_length, num_samples=num_samples)
52
  return forecasts
53
 
 
93
  label='90% Prediction Interval'
94
  )
95
 
96
+ plt.title('Advanced Time Series Forecast (Max 30 Steps)', fontsize=18, fontweight='bold', color='#2C3E50')
97
  plt.xlabel('Time Steps', fontsize=12, color='#34495E')
98
  plt.ylabel('Value', fontsize=12, color='#34495E')
99
  plt.legend(frameon=False)
 
196
  with col4:
197
  prediction_length = st.slider(
198
  'Forecast Horizon',
199
+ min_value=1,
200
+ max_value=30, # Dibatasi maksimal 30
201
+ value=30, # Default 30
202
+ help="Number of future time steps to predict (max 30)"
203
  )
204
 
205
  # Forecast generation
 
209
  forecaster = TimeSeriesForecaster()
210
 
211
  # Preprocess data
212
+ context, prediction_length = forecaster.preprocess_data(
213
  df,
214
  date_column,
215
  value_column,