Skip to content

Commit

Permalink
Merge pull request #376 from davidusb-geek/dependabot/pip/skforecast-…
Browse files Browse the repository at this point in the history
…0.14.0

Bump skforecast from 0.13.0 to 0.14.0
  • Loading branch information
davidusb-geek authored Dec 20, 2024
2 parents 019043a + 7dbbc29 commit f6fb517
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ beautifulsoup4>=4.9.3
pulp>=2.4
pyyaml>=5.4.1
tables==3.9.1
skforecast==0.13.0
skforecast==0.14.0
markupsafe==3.0.2
Jinja2<3.2
plotly>=5.6.0
Expand Down
43 changes: 25 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ authors = [
{name = "David HERNANDEZ", email = "[email protected]"},
]
license = {text = "MIT"}
homepage = "https://github.com/davidusb-geek/emhass"
keywords = ["energy", "management", "optimization", "hass"]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand All @@ -25,23 +24,28 @@ classifiers = [
"Operating System :: OS Independent",
]

[project.dependencies]
numpy = "==1.26.4"
scipy = "==1.12.0"
pandas = "<=2.0.3"
pvlib = ">=0.10.2"
protobuf = ">=3.0.0"
pytz = ">=2021.1"
requests = ">=2.25.1"
beautifulsoup4 = ">=4.9.3"
h5py = "==3.12.1"
pulp = ">=2.4"
pyyaml = ">=5.4.1"
tables = "<=3.9.1"
skforecast = "==0.13.0"
flask = ">=2.0.3"
waitress = ">=2.1.1"
plotly = ">=5.6.0"
dependencies = [
"numpy==1.26.4",
"scipy==1.12.0",
"pandas<=2.0.3",
"pvlib>=0.10.2",
"protobuf>=3.0.0",
"pytz>=2021.1",
"requests>=2.25.1",
"beautifulsoup4>=4.9.3",
"h5py==3.12.1",
"pulp>=2.4",
"pyyaml>=5.4.1",
"tables<=3.9.1",
"skforecast==0.14.0",
"flask>=2.0.3",
"waitress>=2.1.1",
"plotly>=5.6.0"
]

[project.optional-dependencies]
docs = ["sphinx", "sphinx-rtd-theme", "myst-parser"]
test = ["requests_mock","pytest","coverage","snakeviz","ruff"]

[tool.setuptools.packages]
find = {where = ["src"]}
Expand Down Expand Up @@ -69,3 +73,6 @@ emhass = [

[project.scripts]
emhass = "emhass.command_line:main"

[project.urls]
Homepage = "https://github.com/davidusb-geek/emhass"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ h5py==3.12.1
pulp>=2.4
pyyaml>=5.4.1
tables<=3.9.1
skforecast==0.13.0
skforecast==0.14.0
# web server packages
flask>=2.0.3
waitress>=2.1.1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
'pulp>=2.4',
'pyyaml>=5.4.1',
'tables<=3.9.1',
'skforecast==0.13.0',
'skforecast==0.14.0',
'flask>=2.0.3',
'waitress>=2.1.1',
'plotly>=5.6.0'
Expand Down
41 changes: 25 additions & 16 deletions src/emhass/machine_learning_forecaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import r2_score

from skforecast.ForecasterAutoreg import ForecasterAutoreg
from skforecast.model_selection import bayesian_search_forecaster
from skforecast.model_selection import backtesting_forecaster
from skforecast.recursive import ForecasterRecursive
from skforecast.model_selection import bayesian_search_forecaster, backtesting_forecaster, TimeSeriesFold

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
Expand Down Expand Up @@ -144,7 +143,7 @@ def fit(self, split_date_delta: Optional[str] = '48h', perform_backtest: Optiona
self.logger.error("Passed sklearn model "+self.sklearn_model+" is not valid. Defaulting to KNeighborsRegressor")
base_model = KNeighborsRegressor()
# Define the forecaster object
self.forecaster = ForecasterAutoreg(
self.forecaster = ForecasterRecursive(
regressor = base_model,
lags = self.num_lags
)
Expand All @@ -168,16 +167,22 @@ def fit(self, split_date_delta: Optional[str] = '48h', perform_backtest: Optiona
# Using backtesting tool to evaluate the model
self.logger.info("Performing simple backtesting of fitted model")
start_time = time.time()
cv = TimeSeriesFold(
steps = self.num_lags,
initial_train_size = None,
fixed_train_size = False,
gap = 0,
allow_incomplete_fold = True,
refit = False
)
metric, predictions_backtest = backtesting_forecaster(
forecaster = self.forecaster,
y = self.data_train[self.var_model],
exog = self.data_train.drop(self.var_model, axis=1),
initial_train_size = None,
fixed_train_size = False,
steps = self.num_lags,
cv = cv,
metric = MLForecaster.neg_r2_score,
refit = False,
verbose = False
verbose = False,
show_progress = True
)
self.logger.info(f"Elapsed backtesting time: {time.time() - start_time}")
self.logger.info(f"Backtest R2 score: {-metric}")
Expand Down Expand Up @@ -271,21 +276,25 @@ def search_space(trial):
# The optimization routine call
self.logger.info("Bayesian hyperparameter optimization with backtesting")
start_time = time.time()
cv = TimeSeriesFold(
steps = num_lags,
initial_train_size = len(self.data_exo.loc[:self.date_train]),
fixed_train_size = True,
gap = 0,
skip_folds = None,
allow_incomplete_fold = True,
refit = refit
)
self.optimize_results, self.optimize_results_object = bayesian_search_forecaster(
forecaster = self.forecaster,
y = self.data_train[self.var_model],
exog = self.data_train.drop(self.var_model, axis=1),
cv = cv,
search_space = search_space,
steps = num_lags,
metric = MLForecaster.neg_r2_score,
refit = refit,
initial_train_size = len(self.data_exo.loc[:self.date_train]),
fixed_train_size = True,
n_trials = 10,
random_state = 123,
return_best = True,
verbose = False,
engine = 'optuna'
return_best = True
)
self.logger.info(f"Elapsed time: {time.time() - start_time}")
self.is_tuned = True
Expand Down
6 changes: 3 additions & 3 deletions tests/test_machine_learning_forecaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pandas as pd
import numpy as np

from skforecast.ForecasterAutoreg import ForecasterAutoreg
from skforecast.recursive import ForecasterRecursive

from emhass.command_line import set_input_data_dict
from emhass.retrieve_hass import RetrieveHass
Expand Down Expand Up @@ -82,12 +82,12 @@ def setUp(self):

def test_fit(self):
df_pred, df_pred_backtest = self.mlf.fit()
self.assertIsInstance(self.mlf.forecaster, ForecasterAutoreg)
self.assertIsInstance(self.mlf.forecaster, ForecasterRecursive)
self.assertIsInstance(df_pred, pd.DataFrame)
self.assertTrue(df_pred_backtest == None)
# Refit with backtest evaluation
df_pred, df_pred_backtest = self.mlf.fit(perform_backtest=True)
self.assertIsInstance(self.mlf.forecaster, ForecasterAutoreg)
self.assertIsInstance(self.mlf.forecaster, ForecasterRecursive)
self.assertIsInstance(df_pred, pd.DataFrame)
self.assertIsInstance(df_pred_backtest, pd.DataFrame)

Expand Down

0 comments on commit f6fb517

Please sign in to comment.