Daniel Varga commited on
Commit
8ec3059
1 Parent(s): 7c98739

can add predictor backends

Browse files
Files changed (1) hide show
  1. demo_prophet.py +26 -14
demo_prophet.py CHANGED
@@ -4,6 +4,7 @@ import matplotlib.pyplot as plt
4
  from prophet import Prophet
5
  import holidays
6
  import logging
 
7
 
8
 
9
  # kW
@@ -14,12 +15,8 @@ hungarian_holidays = holidays.Hungary(years=range(2019, 2031))
14
  HOLIDAY_DF = pd.DataFrame(list(hungarian_holidays.items()), columns=['ds', 'holiday'])
15
 
16
 
17
- def prediction_task(df, split_date, forecast_horizon):
18
- # Split the data into training (past) and evaluation (future) sets
19
- train_data = df[df['ds'] <= split_date]
20
- eval_data = df[df['ds'] > split_date]
21
-
22
 
 
23
  # Initialize and train the Prophet model using the training data
24
  model = Prophet(seasonality_mode='multiplicative', growth='flat',
25
  yearly_seasonality=False, weekly_seasonality=True, daily_seasonality=True,
@@ -32,19 +29,33 @@ def prediction_task(df, split_date, forecast_horizon):
32
 
33
  # Make predictions for the evaluation period
34
  forecast = model.predict(future)
 
35
 
36
- # Calculate evaluation metrics (e.g., MAE, MSE, RMSE) by comparing eval_predictions with eval_data['y']
37
-
38
- # For example, you can calculate MAE as follows:
39
- from sklearn.metrics import mean_absolute_error
40
-
41
- eval_data = eval_data[eval_data['ds'] <= forecast['ds'].max()]
42
  for key in ('yhat', 'yhat_lower', 'yhat_upper'):
43
  forecast[key] = np.maximum(forecast[key], PREDICTION_LOWER_BOUND)
44
 
45
- mae = mean_absolute_error(eval_data['y'], forecast['yhat'])
 
 
 
 
 
 
 
 
 
 
46
 
47
- # Print or store the evaluation metrics
 
 
 
 
 
 
 
 
 
48
 
49
  do_vis = False
50
  if do_vis:
@@ -66,6 +77,7 @@ def prediction_task(df, split_date, forecast_horizon):
66
 
67
  fig2 = model.plot_components(forecast)
68
  plt.show()
 
69
  return mae, eval_data['y'].mean()
70
 
71
 
@@ -100,7 +112,7 @@ weekly_date_range = pd.date_range(start=start_date, end=end_date, freq='8d')
100
  maes = []
101
  mean_values = []
102
  for split_date in weekly_date_range:
103
- mae, mean_value = prediction_task(df, split_date, forecast_horizon)
104
  maes.append(mae)
105
  mean_values.append(mean_value)
106
  print(split_date, "Mean Absolute Error", mae, "MAE/true mean", mae / mean_value)
 
4
  from prophet import Prophet
5
  import holidays
6
  import logging
7
+ from sklearn.metrics import mean_absolute_error
8
 
9
 
10
  # kW
 
15
  HOLIDAY_DF = pd.DataFrame(list(hungarian_holidays.items()), columns=['ds', 'holiday'])
16
 
17
 
 
 
 
 
 
18
 
19
+ def prophet_backend(train_data, forecast_horizon):
20
  # Initialize and train the Prophet model using the training data
21
  model = Prophet(seasonality_mode='multiplicative', growth='flat',
22
  yearly_seasonality=False, weekly_seasonality=True, daily_seasonality=True,
 
29
 
30
  # Make predictions for the evaluation period
31
  forecast = model.predict(future)
32
+ assert len(forecast) == forecast_horizon
33
 
 
 
 
 
 
 
34
  for key in ('yhat', 'yhat_lower', 'yhat_upper'):
35
  forecast[key] = np.maximum(forecast[key], PREDICTION_LOWER_BOUND)
36
 
37
+ return forecast
38
+
39
+
40
+ def sklearn_backend(train_data, forecast_horizon):
41
+ dc = train_data[['y']]
42
+ # inserting new column with yesterday's consumption values
43
+ for i in range(1, 4 * 24 + 1):
44
+ dc.loc[:, 'd%02d' % i] = dc.loc[:,'y'].shift(4 * 24 + i) # t-2days to t-1day
45
+ dc.loc[:, 'w%02d' % i] = dc.loc[:,'y'].shift(7 * 4 * 24 + i) # t-7days to t-8days
46
+ dc.info()
47
+ exit()
48
 
49
+
50
+ def prediction_task(backend, df, split_date, forecast_horizon):
51
+ # Split the data into training (past) and evaluation (future) sets
52
+ train_data = df[df['ds'] <= split_date]
53
+ eval_data = df[df['ds'] > split_date]
54
+ eval_data = eval_data.head(forecast_horizon)
55
+
56
+ forecast = backend(train_data, forecast_horizon)
57
+
58
+ mae = mean_absolute_error(eval_data['y'], forecast['yhat'])
59
 
60
  do_vis = False
61
  if do_vis:
 
77
 
78
  fig2 = model.plot_components(forecast)
79
  plt.show()
80
+
81
  return mae, eval_data['y'].mean()
82
 
83
 
 
112
  maes = []
113
  mean_values = []
114
  for split_date in weekly_date_range:
115
+ mae, mean_value = prediction_task(prophet_backend, df, split_date, forecast_horizon)
116
  maes.append(mae)
117
  mean_values.append(mean_value)
118
  print(split_date, "Mean Absolute Error", mae, "MAE/true mean", mae / mean_value)