Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random_state to HyperparameterOptimization class - Fixes for #125 #131

Merged
merged 14 commits into from
Jan 31, 2024
Merged
4 changes: 2 additions & 2 deletions luminaire/optimization/hyperparameter_optimization.py
Original file line number Diff line number Diff line change
@@ -269,7 +269,7 @@ def _optimize(self, data, objective_part, algo=tpe.suggest, max_evals=50):
:return: Optimal hyperparameters
:rtype: dict
"""

import numpy as np
from functools import partial
from pykalman import KalmanFilter

@@ -305,7 +305,7 @@ def _optimize(self, data, objective_part, algo=tpe.suggest, max_evals=50):
raise ValueError('Only `detection_type=OutlierDetection` is supported in hyperparameter optimization right now')

# Calling the optimization function
hyper_param = fmin(objective, space=space, algo=algo, max_evals=max_evals, show_progressbar=True, rstate=self.random_state)
hyper_param = fmin(objective, space=space, algo=algo, max_evals=max_evals, show_progressbar=True, rstate=np.random.default_rng(self.random_state))
hyper_param['LuminaireModel'] = hyper_param_list[hyper_param['LuminaireModel']]['model']
if 'max_ft_freq' in hyper_param:
hyper_param['max_ft_freq'] = hyper_param['max_ft_freq'] + 2
11 changes: 9 additions & 2 deletions luminaire/tests/test_hyper.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,16 @@

class TestHyperparameterOptimization(object):

def test_run(self, test_data_with_missing):

def test_run1(self, test_data_with_missing):
"""Test using the default random_state=None"""
hyper_obj = HyperparameterOptimization(freq='D', detection_type='OutlierDetection')
hyper_parameters = hyper_obj.run(test_data_with_missing, max_evals=5)

assert isinstance(hyper_parameters, dict)

def test_run2(self, test_data_with_missing):
"""Test defining a random_state"""
hyper_obj = HyperparameterOptimization(freq='D', detection_type='OutlierDetection', random_state=42)
hyper_parameters = hyper_obj.run(test_data_with_missing, max_evals=5)

assert isinstance(hyper_parameters, dict)