spotforecast2 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spotforecast2/.DS_Store +0 -0
- spotforecast2/__init__.py +2 -0
- spotforecast2/data/__init__.py +0 -0
- spotforecast2/data/data.py +130 -0
- spotforecast2/data/fetch_data.py +209 -0
- spotforecast2/exceptions.py +681 -0
- spotforecast2/forecaster/.DS_Store +0 -0
- spotforecast2/forecaster/__init__.py +7 -0
- spotforecast2/forecaster/base.py +448 -0
- spotforecast2/forecaster/metrics.py +527 -0
- spotforecast2/forecaster/recursive/__init__.py +4 -0
- spotforecast2/forecaster/recursive/_forecaster_equivalent_date.py +1075 -0
- spotforecast2/forecaster/recursive/_forecaster_recursive.py +939 -0
- spotforecast2/forecaster/recursive/_warnings.py +15 -0
- spotforecast2/forecaster/utils.py +954 -0
- spotforecast2/model_selection/__init__.py +5 -0
- spotforecast2/model_selection/bayesian_search.py +453 -0
- spotforecast2/model_selection/grid_search.py +314 -0
- spotforecast2/model_selection/random_search.py +151 -0
- spotforecast2/model_selection/split_base.py +357 -0
- spotforecast2/model_selection/split_one_step.py +245 -0
- spotforecast2/model_selection/split_ts_cv.py +634 -0
- spotforecast2/model_selection/utils_common.py +718 -0
- spotforecast2/model_selection/utils_metrics.py +103 -0
- spotforecast2/model_selection/validation.py +685 -0
- spotforecast2/preprocessing/__init__.py +30 -0
- spotforecast2/preprocessing/_binner.py +378 -0
- spotforecast2/preprocessing/_common.py +123 -0
- spotforecast2/preprocessing/_differentiator.py +123 -0
- spotforecast2/preprocessing/_rolling.py +136 -0
- spotforecast2/preprocessing/curate_data.py +254 -0
- spotforecast2/preprocessing/imputation.py +92 -0
- spotforecast2/preprocessing/outlier.py +114 -0
- spotforecast2/preprocessing/split.py +139 -0
- spotforecast2/py.typed +0 -0
- spotforecast2/utils/__init__.py +43 -0
- spotforecast2/utils/convert_to_utc.py +44 -0
- spotforecast2/utils/data_transform.py +208 -0
- spotforecast2/utils/forecaster_config.py +344 -0
- spotforecast2/utils/generate_holiday.py +70 -0
- spotforecast2/utils/validation.py +569 -0
- spotforecast2/weather/__init__.py +0 -0
- spotforecast2/weather/weather_client.py +288 -0
- spotforecast2-0.0.1.dist-info/METADATA +47 -0
- spotforecast2-0.0.1.dist-info/RECORD +46 -0
- spotforecast2-0.0.1.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Metrics calculation utilities for model selection."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _calculate_metrics_one_step_ahead(
|
|
8
|
+
forecaster: object,
|
|
9
|
+
metrics: list,
|
|
10
|
+
X_train: pd.DataFrame,
|
|
11
|
+
y_train: pd.Series | dict[int, pd.Series],
|
|
12
|
+
X_test: pd.DataFrame,
|
|
13
|
+
y_test: pd.Series | dict[int, pd.Series],
|
|
14
|
+
) -> list:
|
|
15
|
+
"""
|
|
16
|
+
Calculate metrics when predictions are one-step-ahead.
|
|
17
|
+
|
|
18
|
+
When forecaster is of type `ForecasterDirect`, only the estimator for step 1
|
|
19
|
+
is used.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
forecaster: Forecaster model.
|
|
23
|
+
metrics: List of metrics.
|
|
24
|
+
X_train: Predictor values used to train the model.
|
|
25
|
+
y_train: Target values related to each row of `X_train`.
|
|
26
|
+
X_test: Predictor values used to test the model.
|
|
27
|
+
y_test: Target values related to each row of `X_test`.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
List with metric values.
|
|
31
|
+
|
|
32
|
+
Notes:
|
|
33
|
+
When using this metric in validation, `y_train` doesn't include
|
|
34
|
+
the first window_size observations used to create the predictors and/or
|
|
35
|
+
rolling features.
|
|
36
|
+
|
|
37
|
+
Examples:
|
|
38
|
+
>>> import pandas as pd
|
|
39
|
+
>>> import numpy as np
|
|
40
|
+
>>> from sklearn.linear_model import LinearRegression
|
|
41
|
+
>>> from sklearn.metrics import mean_squared_error
|
|
42
|
+
>>> from spotforecast2.forecaster.recursive import ForecasterRecursive
|
|
43
|
+
>>> from spotforecast2.model_selection.utils_metrics import _calculate_metrics_one_step_ahead
|
|
44
|
+
>>>
|
|
45
|
+
>>> forecaster = ForecasterRecursive(LinearRegression(), lags=2)
|
|
46
|
+
>>>
|
|
47
|
+
>>> # Mock training data (already transformed into predictors/target)
|
|
48
|
+
>>> X_train = pd.DataFrame(np.array([[1, 2], [2, 3], [3, 4]]), columns=['lag_2', 'lag_1'])
|
|
49
|
+
>>> y_train = pd.Series(np.array([3, 4, 5]), name='y')
|
|
50
|
+
>>>
|
|
51
|
+
>>> # Mock test data
|
|
52
|
+
>>> X_test = pd.DataFrame(np.array([[4, 5]]), columns=['lag_2', 'lag_1'])
|
|
53
|
+
>>> y_test = pd.Series(np.array([6]), name='y')
|
|
54
|
+
>>>
|
|
55
|
+
>>> metrics = [mean_squared_error]
|
|
56
|
+
>>> result = _calculate_metrics_one_step_ahead(
|
|
57
|
+
... forecaster=forecaster,
|
|
58
|
+
... metrics=metrics,
|
|
59
|
+
... X_train=X_train,
|
|
60
|
+
... y_train=y_train,
|
|
61
|
+
... X_test=X_test,
|
|
62
|
+
... y_test=y_test
|
|
63
|
+
... )
|
|
64
|
+
>>> print(result)
|
|
65
|
+
[0.0]
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
if type(forecaster).__name__ == "ForecasterDirect":
|
|
69
|
+
|
|
70
|
+
step = 1 # Only the model for step 1 is optimized.
|
|
71
|
+
X_train, y_train = forecaster.filter_train_X_y_for_step(
|
|
72
|
+
step=step, X_train=X_train, y_train=y_train
|
|
73
|
+
)
|
|
74
|
+
X_test, y_test = forecaster.filter_train_X_y_for_step(
|
|
75
|
+
step=step, X_train=X_test, y_train=y_test
|
|
76
|
+
)
|
|
77
|
+
forecaster.estimators_[step].fit(X_train, y_train)
|
|
78
|
+
y_pred = forecaster.estimators_[step].predict(X_test)
|
|
79
|
+
|
|
80
|
+
else:
|
|
81
|
+
forecaster.estimator.fit(X_train, y_train)
|
|
82
|
+
y_pred = forecaster.estimator.predict(X_test)
|
|
83
|
+
|
|
84
|
+
y_true = y_test.to_numpy()
|
|
85
|
+
y_pred = y_pred.ravel()
|
|
86
|
+
y_train = y_train.to_numpy()
|
|
87
|
+
|
|
88
|
+
if forecaster.differentiation is not None:
|
|
89
|
+
y_true = forecaster.differentiator.inverse_transform_next_window(y_true)
|
|
90
|
+
y_pred = forecaster.differentiator.inverse_transform_next_window(y_pred)
|
|
91
|
+
y_train = forecaster.differentiator.inverse_transform_training(y_train)
|
|
92
|
+
|
|
93
|
+
if forecaster.transformer_y is not None:
|
|
94
|
+
y_true = forecaster.transformer_y.inverse_transform(y_true.reshape(-1, 1))
|
|
95
|
+
y_pred = forecaster.transformer_y.inverse_transform(y_pred.reshape(-1, 1))
|
|
96
|
+
y_train = forecaster.transformer_y.inverse_transform(y_train.reshape(-1, 1))
|
|
97
|
+
|
|
98
|
+
y_true = y_true.ravel()
|
|
99
|
+
y_pred = y_pred.ravel()
|
|
100
|
+
y_train = y_train.ravel()
|
|
101
|
+
metric_values = [m(y_true=y_true, y_pred=y_pred, y_train=y_train) for m in metrics]
|
|
102
|
+
|
|
103
|
+
return metric_values
|