├── .github └── workflows │ └── tests.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── hpsklearn ├── __init__.py ├── components │ ├── __init__.py │ ├── _base.py │ ├── cluster │ │ ├── __init__.py │ │ └── _kmeans.py │ ├── compose │ │ ├── __init__.py │ │ └── _target.py │ ├── covariance │ │ ├── __init__.py │ │ └── _elliptic_envelope.py │ ├── cross_decomposition │ │ ├── __init__.py │ │ └── _pls.py │ ├── decomposition │ │ ├── __init__.py │ │ └── _pca.py │ ├── discriminant_analysis.py │ ├── dummy.py │ ├── ensemble │ │ ├── __init__.py │ │ ├── _bagging.py │ │ ├── _forest.py │ │ ├── _gb.py │ │ ├── _hist_gradient_boosting.py │ │ ├── _iforest.py │ │ └── _weight_boosting.py │ ├── feature_extraction │ │ ├── __init__.py │ │ └── text.py │ ├── gaussian_process │ │ ├── __init__.py │ │ ├── _gpc.py │ │ └── _gpr.py │ ├── kernel_ridge.py │ ├── lagselectors.py │ ├── lightgbm.py │ ├── linear_model │ │ ├── __init__.py │ │ ├── _base.py │ │ ├── _bayes.py │ │ ├── _coordinate_descent.py │ │ ├── _glm.py │ │ ├── _huber.py │ │ ├── _least_angle.py │ │ ├── _logistic.py │ │ ├── _omp.py │ │ ├── _passive_aggressive.py │ │ ├── _perceptron.py │ │ ├── _quantile.py │ │ ├── _ransac.py │ │ ├── _ridge.py │ │ ├── _stochastic_gradient.py │ │ └── _theil_sen.py │ ├── mixture │ │ ├── __init__.py │ │ ├── _bayesian_mixture.py │ │ └── _gaussian_mixture.py │ ├── multiclass.py │ ├── naive_bayes.py │ ├── neighbors │ │ ├── __init__.py │ │ ├── _classification.py │ │ ├── _nearest_centroid.py │ │ └── _regression.py │ ├── neural_network │ │ ├── __init__.py │ │ └── _multilayer_perceptron.py │ ├── preprocessing │ │ ├── __init__.py │ │ ├── _data.py │ │ ├── _discretization.py │ │ ├── _encoders.py │ │ └── _polynomial.py │ ├── semi_supervised │ │ ├── __init__.py │ │ └── _label_propagation.py │ ├── svm │ │ ├── __init__.py │ │ └── _classes.py │ ├── tree │ │ ├── __init__.py │ │ └── _classes.py │ ├── vkmeans.py │ └── xgboost.py ├── estimator │ ├── __init__.py │ ├── _cost_fn.py │ ├── _pfit.py │ ├── _transform.py │ ├── _utils.py │ └── estimator.py └── objects │ ├── __init__.py │ ├── lagselectors.py │ └── vkmeans.py ├── pyproject.toml ├── requirements.txt ├── requirements_dev.txt ├── setup.cfg ├── setup.py ├── tests ├── __init__.py ├── test_components │ ├── __init__.py │ ├── test_cluster │ │ ├── __init__.py │ │ └── test_kmeans.py │ ├── test_compose │ │ ├── __init__.py │ │ └── test_target.py │ ├── test_covariance │ │ ├── __init__.py │ │ └── test_elliptic_envelope.py │ ├── test_cross_decomposition │ │ ├── __init__.py │ │ └── test_pls.py │ ├── test_decomposition │ │ ├── __init__.py │ │ └── test_pca.py │ ├── test_discriminant_analysis.py │ ├── test_dummy.py │ ├── test_ensemble │ │ ├── __init__.py │ │ ├── test_bagging.py │ │ ├── test_forest.py │ │ ├── test_gb.py │ │ ├── test_hist_gradient_boosting.py │ │ ├── test_iforest.py │ │ └── test_weight_boosting.py │ ├── test_feature_extraction │ │ ├── __init__.py │ │ └── test_text.py │ ├── test_gaussian_process │ │ ├── __init__.py │ │ ├── test_gpc.py │ │ └── test_gpr.py │ ├── test_kernel_ridge.py │ ├── test_lagselectors.py │ ├── test_lightgbm.py │ ├── test_linear_model │ │ ├── __init__.py │ │ ├── test_base.py │ │ ├── test_bayes.py │ │ ├── test_coordinate_descent.py │ │ ├── test_glm.py │ │ ├── test_huber.py │ │ ├── test_least_angle.py │ │ ├── test_logistic.py │ │ ├── test_omp.py │ │ ├── test_passive_aggressive.py │ │ ├── test_perceptron.py │ │ ├── test_quantile.py │ │ ├── test_ransac.py │ │ ├── test_ridge.py │ │ ├── test_stochastic_gradient.py │ │ └── test_theil_sen.py │ ├── test_mixture │ │ ├── __init__.py │ │ ├── test_bayesian_mixture.py │ │ └── test_gaussian_mixture.py │ ├── test_multiclass.py │ ├── test_naive_bayes.py │ ├── test_neighbors │ │ ├── __init__.py │ │ ├── test_classification.py │ │ ├── test_nearest_centroid.py │ │ └── test_regression.py │ ├── test_neural_network │ │ ├── __init__.py │ │ └── test_multilayer_perceptron.py │ ├── test_preprocessing │ │ ├── __init__.py │ │ ├── test_data.py │ │ ├── test_discretization.py │ │ ├── test_encoders.py │ │ └── test_polynomial.py │ ├── test_semi_supervised │ │ ├── __init__.py │ │ └── test_label_propagation.py │ ├── test_svm │ │ ├── __init__.py │ │ └── test_svm_classes.py │ ├── test_tree │ │ ├── __init__.py │ │ └── test_tree_classes.py │ ├── test_vkmeans.py │ └── test_xgboost.py ├── test_estimator │ ├── __init__.py │ └── test_estimator.py └── utils.py └── tox.ini /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | test: 9 | runs-on: ${{ matrix.os }} 10 | strategy: 11 | matrix: 12 | os: [ubuntu-latest, windows-latest] 13 | python-version: ['3.11', '3.12', '3.13'] 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Set up Python ${{ matrix.python-version }} 18 | uses: actions/setup-python@v2 19 | with: 20 | python-version: ${{ matrix.python-version }} 21 | - name: Install dependencies 22 | run: | 23 | python -m pip install --upgrade pip 24 | pip install tox tox-gh-actions 25 | - name: Test with tox 26 | run: tox 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | 37 | .idea 38 | notebooks/.ipynb_checkpoints 39 | /venv -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | LICENSE 2 | ======= 3 | 4 | Copyright (c) 2013, James Bergstra 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | * Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | * Neither the name of hyperopt-sklearn nor the names of its contributors 16 | may be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /hpsklearn/__init__.py: -------------------------------------------------------------------------------- 1 | from .estimator import hyperopt_estimator as HyperoptEstimator 2 | from .components import * # noqa 3 | from .components.multiclass import \ 4 | one_vs_rest_classifier, \ 5 | one_vs_one_classifier, \ 6 | output_code_classifier 7 | -------------------------------------------------------------------------------- /hpsklearn/components/_base.py: -------------------------------------------------------------------------------- 1 | import functools 2 | 3 | 4 | def validate(params, validation_test, msg): 5 | """ 6 | Validation decorator for parameter checks 7 | includes automatic value error raising 8 | allows for multiple usages per function 9 | """ 10 | def inner(func): 11 | @functools.wraps(func) 12 | def wrapper(*args, **kwargs): 13 | """ 14 | Iterate over keyword arguments 15 | if keyword in parameter perform lambda test 16 | """ 17 | for k, v in kwargs.items(): 18 | if k in params and not validation_test(v): 19 | raise ValueError(msg % (k, v)) 20 | 21 | return func(*args, **kwargs) 22 | return wrapper 23 | return inner 24 | -------------------------------------------------------------------------------- /hpsklearn/components/cluster/__init__.py: -------------------------------------------------------------------------------- 1 | from ._kmeans import \ 2 | k_means, \ 3 | mini_batch_k_means 4 | -------------------------------------------------------------------------------- /hpsklearn/components/cluster/_kmeans.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import cluster 7 | import numpy.typing as npt 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_KMeans(*args, **kwargs): 13 | return cluster.KMeans(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_MiniBatchKMeans(*args, **kwargs): 18 | return cluster.MiniBatchKMeans(*args, **kwargs) 19 | 20 | 21 | def _kmeans_n_clusters(name: str): 22 | """ 23 | Declaration search space 'n_clusters' parameter 24 | """ 25 | return scope.int(hp.uniform(name, 1, 20)) 26 | 27 | 28 | def _kmeans_init(name: str): 29 | """ 30 | Declaration search space 'init' parameter 31 | """ 32 | return hp.choice(name, ["k-means++", "random"]) 33 | 34 | 35 | def _kmeans_random_state(name: str): 36 | """ 37 | Declaration search space 'random_state' parameter 38 | """ 39 | return hp.randint(name, 5) 40 | 41 | 42 | def _kmeans_hp_space( 43 | name_func, 44 | n_clusters: typing.Union[int, Apply] = None, 45 | init: typing.Union[str, callable, npt.ArrayLike, Apply] = None, 46 | verbose: int = 0, 47 | random_state=None, 48 | **kwargs 49 | ): 50 | """ 51 | Hyper parameter search space for 52 | k means 53 | mini batch k means 54 | """ 55 | hp_space = dict( 56 | n_clusters=_kmeans_n_clusters(name_func("n_clusters")) if n_clusters is None else n_clusters, 57 | init=_kmeans_init(name_func("init")) if init is None else init, 58 | verbose=verbose, 59 | random_state=_kmeans_random_state(name_func("random_state")) if random_state is None else random_state, 60 | **kwargs 61 | ) 62 | return hp_space 63 | 64 | 65 | @validate(params=["algorithm"], 66 | validation_test=lambda param: not isinstance(param, str) or param in ["lloyd", "elkan"], 67 | msg="Invalid parameter '%s' with value '%s'. Value must be 'lloyd' or 'elkan'") 68 | def k_means(name: str, 69 | n_init: typing.Union[int, Apply] = None, 70 | max_iter: typing.Union[int, Apply] = None, 71 | tol: typing.Union[float, Apply] = None, 72 | copy_x: bool = True, 73 | algorithm: typing.Union[str, Apply] = None, 74 | **kwargs): 75 | """ 76 | Return a pyll graph with hyperparameters that will construct 77 | a sklearn.cluster.KMeans model. 78 | 79 | Args: 80 | name: name | str 81 | n_init: number of times to run k-means algorithm | int 82 | max_iter: maximum number of iterations | int 83 | tol: relative tolerance in regard to Frobenius norm | float 84 | copy_x: modify copy of data | bool 85 | algorithm: K-means algorithm to use | str 86 | 87 | See help(hpsklearn.components.cluster._kmeans._kmeans_hp_space) 88 | for info on additional available k means arguments. 89 | """ 90 | 91 | def _name(msg): 92 | return f"{name}.k_means_{msg}" 93 | 94 | hp_space = _kmeans_hp_space(_name, **kwargs) 95 | hp_space["n_init"] = scope.int(hp.uniform(_name("n_init"), 2, 25)) if n_init is None else n_init 96 | hp_space["max_iter"] = scope.int(hp.uniform(_name("max_iter"), 100, 500)) if max_iter is None else max_iter 97 | hp_space["tol"] = hp.uniform(_name("tol"), 1e-5, 1e-3) if tol is None else tol 98 | hp_space["copy_x"] = copy_x 99 | hp_space["algorithm"] = hp.choice(_name("algorithm"), ["lloyd", "elkan"]) if algorithm is None else algorithm 100 | 101 | return scope.sklearn_KMeans(**hp_space) 102 | 103 | 104 | def mini_batch_k_means(name: str, 105 | max_iter: typing.Union[int, Apply] = None, 106 | batch_size: typing.Union[int, Apply] = None, 107 | compute_labels: bool = True, 108 | tol: typing.Union[float, Apply] = None, 109 | max_no_improvement: typing.Union[int, Apply] = None, 110 | init_size: int = None, 111 | n_init: typing.Union[int, Apply] = None, 112 | reassignment_ratio: typing.Union[float, Apply] = None, 113 | **kwargs): 114 | """ 115 | Return a pyll graph with hyperparameters that will construct 116 | a sklearn.cluster.KMeans model. 117 | 118 | Args: 119 | name: name | str 120 | max_iter: maximum number of iterations | int 121 | batch_size: size of the mini batches | int 122 | compute_labels: compute label assignment and inertia | bool 123 | tol: relative tolerance with regards to Frobenius norm | float 124 | max_no_improvement: early stopping when no improvement found | int 125 | init_size: random samples for initialization | int 126 | n_init: number of times to run k-means algorithm | int 127 | reassignment_ratio: control the fraction for center reassignment | float 128 | 129 | See help(hpsklearn.components.cluster._kmeans._kmeans_hp_space) 130 | for info on additional available k means arguments. 131 | """ 132 | 133 | def _name(msg): 134 | return f"{name}.mini_batch_k_means_{msg}" 135 | 136 | hp_space = _kmeans_hp_space(_name, **kwargs) 137 | hp_space["max_iter"] = scope.int(hp.uniform(_name("max_iter"), 100, 300)) if max_iter is None else max_iter 138 | hp_space["batch_size"] = hp.choice(_name("batch_size"), [256, 512, 1024, 2048]) \ 139 | if batch_size is None else batch_size 140 | hp_space["compute_labels"] = compute_labels 141 | hp_space["tol"] = hp.uniform(_name("tol"), 1e-7, 1e-5) if tol is None else tol 142 | hp_space["max_no_improvement"] = scope.int(hp.uniform(_name("max_no_improvement"), 5, 25)) \ 143 | if max_no_improvement is None else max_no_improvement 144 | hp_space["init_size"] = init_size 145 | hp_space["n_init"] = hp.choice(_name("n_init"), [1, 2, 3, 4]) if n_init is None else n_init 146 | hp_space["reassignment_ratio"] = hp.uniform(_name("reassignment_ratio"), 0.001, 0.1) \ 147 | if reassignment_ratio is None else reassignment_ratio 148 | 149 | return scope.sklearn_MiniBatchKMeans(**hp_space) 150 | -------------------------------------------------------------------------------- /hpsklearn/components/compose/__init__.py: -------------------------------------------------------------------------------- 1 | from ._target import transformed_target_regressor 2 | -------------------------------------------------------------------------------- /hpsklearn/components/compose/_target.py: -------------------------------------------------------------------------------- 1 | from hyperopt.pyll import scope 2 | 3 | from sklearn import compose 4 | 5 | 6 | @scope.define 7 | def sklearn_TransformedTargetRegressor(*args, **kwargs): 8 | return compose.TransformedTargetRegressor(*args, **kwargs) 9 | 10 | 11 | def transformed_target_regressor(name: str, 12 | regressor: object = None, 13 | transformer: object = None, 14 | func: callable = None, 15 | inverse_func: callable = None, 16 | check_inverse: bool = True, 17 | **kwargs): 18 | """ 19 | Return a pyll graph with hyperparameters that will construct 20 | a sklearn.compose.TransformedTargetRegressor model. 21 | 22 | Args: 23 | name: name | str 24 | regressor: regressor object | object 25 | transformer: estimator object | object 26 | func: function to apply to `y` before fit | callable 27 | inverse_func: function to apply to prediction | callable 28 | check_inverse: check whether inverse leads to original targets | bool 29 | """ 30 | 31 | def _name(msg): 32 | return f"{name}.transformed_target_regressor_{msg}" 33 | 34 | # TODO: Try implementing np.exp and np.log | np.sqrt and np.square combinations 35 | hp_space = dict( 36 | regressor=regressor, 37 | transformer=transformer, 38 | func=func, 39 | inverse_func=inverse_func, 40 | check_inverse=check_inverse, 41 | **kwargs 42 | ) 43 | return scope.sklearn_TransformedTargetRegressor(**hp_space) 44 | -------------------------------------------------------------------------------- /hpsklearn/components/covariance/__init__.py: -------------------------------------------------------------------------------- 1 | from ._elliptic_envelope import elliptic_envelope 2 | -------------------------------------------------------------------------------- /hpsklearn/components/covariance/_elliptic_envelope.py: -------------------------------------------------------------------------------- 1 | import typing 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import covariance 7 | 8 | 9 | @scope.define 10 | def sklearn_EllipticEnvelope(*args, **kwargs): 11 | return covariance.EllipticEnvelope(*args, **kwargs) 12 | 13 | 14 | def elliptic_envelope(name: str, 15 | store_precision: bool = True, 16 | assume_centered: bool = False, 17 | support_fraction: typing.Union[float, Apply] = None, 18 | contamination: typing.Union[float, Apply] = 0.1, 19 | random_state=None, 20 | **kwargs): 21 | """ 22 | Return a pyll graph with hyperparameters that will construct 23 | a sklearn.covariance.EllipticEnvelope model. 24 | 25 | Args: 26 | name: name | str 27 | store_precision: whether precision is stored | bool 28 | assume_centered: whether to assume centered data | bool 29 | support_fraction: fraction to include in support | float 30 | contamination: contamination of data set | float 31 | random_state: random state for shuffling data | int 32 | """ 33 | 34 | def _name(msg): 35 | return f"{name}.elliptic_envelope_{msg}" 36 | 37 | hp_space = dict( 38 | store_precision=store_precision, 39 | assume_centered=assume_centered, 40 | support_fraction=hp.uniform(_name("support_fraction"), 0.05, 0.95) 41 | if support_fraction is None else support_fraction, 42 | contamination=hp.uniform(_name("contamination"), 0.0, 0.3) if contamination is None else contamination, 43 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 44 | **kwargs 45 | ) 46 | return scope.sklearn_EllipticEnvelope(**hp_space) 47 | -------------------------------------------------------------------------------- /hpsklearn/components/cross_decomposition/__init__.py: -------------------------------------------------------------------------------- 1 | from ._pls import \ 2 | cca, \ 3 | pls_canonical, \ 4 | pls_regression 5 | -------------------------------------------------------------------------------- /hpsklearn/components/cross_decomposition/_pls.py: -------------------------------------------------------------------------------- 1 | import typing 2 | 3 | from hpsklearn.components._base import validate 4 | 5 | from hyperopt.pyll import scope, Apply 6 | from hyperopt import hp 7 | 8 | from sklearn import cross_decomposition 9 | import numpy as np 10 | 11 | 12 | @scope.define 13 | def sklearn_CCA(*args, **kwargs): 14 | return cross_decomposition.CCA(*args, **kwargs) 15 | 16 | 17 | @scope.define 18 | def sklearn_PLSCanonical(*args, **kwargs): 19 | return cross_decomposition.PLSCanonical(*args, **kwargs) 20 | 21 | 22 | @scope.define 23 | def sklearn_PLSRegression(*args, **kwargs): 24 | return cross_decomposition.PLSRegression(*args, **kwargs) 25 | 26 | 27 | def _pls_n_components(name: str): 28 | """ 29 | Declaration search space 'n_components' parameter 30 | """ 31 | return hp.choice(name, [1, 2]) 32 | 33 | 34 | def _pls_max_iter(name: str): 35 | """ 36 | Declaration search space 'max_iter' parameter 37 | """ 38 | return scope.int(hp.uniform(name, 350, 650)) 39 | 40 | 41 | def _pls_tol(name: str): 42 | """ 43 | Declaration search space 'tol' parameter 44 | """ 45 | return hp.loguniform(name, np.log(1e-7), np.log(1e-5)) 46 | 47 | 48 | def _pls_hp_space( 49 | name_func, 50 | n_components: typing.Union[int, Apply] = None, 51 | scale: bool = True, 52 | max_iter: typing.Union[int, Apply] = None, 53 | tol: typing.Union[float, Apply] = None, 54 | copy: bool = True, 55 | **kwargs 56 | ): 57 | """ 58 | Hyper parameter search space for 59 | cca 60 | pls canonical 61 | pls regression 62 | """ 63 | hp_space = dict( 64 | n_components=_pls_n_components(name_func("n_components")) if n_components is None else n_components, 65 | scale=scale, 66 | max_iter=_pls_max_iter(name_func("max_iter")) if max_iter is None else max_iter, 67 | tol=_pls_tol(name_func("tol")) if tol is None else tol, 68 | copy=copy, 69 | **kwargs 70 | ) 71 | return hp_space 72 | 73 | 74 | def cca(name: str, **kwargs): 75 | """ 76 | Return a pyll graph with hyperparameters that will construct 77 | a sklearn.cross_decomposition.CCA model. 78 | 79 | Args: 80 | name: name | str 81 | 82 | See help(hpsklearn.components.cross_decomposition._pls._pls_hp_space) 83 | for info on additional available pls arguments. 84 | """ 85 | 86 | def _name(msg): 87 | return f"{name}.cca_{msg}" 88 | 89 | hp_space = _pls_hp_space(_name, **kwargs) 90 | 91 | return scope.sklearn_CCA(**hp_space) 92 | 93 | 94 | @validate(params=["algorithm"], 95 | validation_test=lambda param: not isinstance(param, str) or param in ["nipals", "svd"], 96 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['nipals', 'svd'].") 97 | def pls_canonical(name: str, algorithm: typing.Union[str, Apply] = None, **kwargs): 98 | """ 99 | Return a pyll graph with hyperparameters that will construct 100 | a sklearn.cross_decomposition.PLSCanonical model. 101 | 102 | Args: 103 | name: name | str 104 | algorithm: algorithm for first singular vectors | str 105 | 106 | See help(hpsklearn.components.cross_decomposition._pls._pls_hp_space) 107 | for info on additional available pls arguments. 108 | """ 109 | 110 | def _name(msg): 111 | return f"{name}.pls_canonical_{msg}" 112 | 113 | hp_space = _pls_hp_space(_name, **kwargs) 114 | hp_space["algorithm"] = hp.choice(_name("algorithm"), ["nipals", "svd"]) if algorithm is None else algorithm 115 | 116 | return scope.sklearn_PLSCanonical(**hp_space) 117 | 118 | 119 | def pls_regression(name: str, **kwargs): 120 | """ 121 | Return a pyll graph with hyperparameters that will construct 122 | a sklearn.cross_decomposition.PLSRegression model. 123 | 124 | Args: 125 | name: name | str 126 | 127 | See help(hpsklearn.components.cross_decomposition._pls._pls_hp_space) 128 | for info on additional available pls arguments. 129 | """ 130 | 131 | def _name(msg): 132 | return f"{name}.pls_regression_{msg}" 133 | 134 | hp_space = _pls_hp_space(_name, **kwargs) 135 | 136 | return scope.sklearn_PLSRegression(**hp_space) 137 | -------------------------------------------------------------------------------- /hpsklearn/components/decomposition/__init__.py: -------------------------------------------------------------------------------- 1 | from ._pca import pca 2 | -------------------------------------------------------------------------------- /hpsklearn/components/decomposition/_pca.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import decomposition 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_PCA(*args, **kwargs): 13 | return decomposition.PCA(*args, **kwargs) 14 | 15 | 16 | @validate(params=["n_components"], 17 | validation_test=lambda param: not isinstance(param, str) or param == "mle", 18 | msg="Invalid parameter '%s' with value '%s'. Choose 'mle', int or float.") 19 | def pca(name: str, 20 | n_components: typing.Union[float, str, Apply] = None, 21 | whiten: typing.Union[bool, Apply] = None, 22 | copy: bool = True): 23 | """ 24 | Return a pyll graph with hyperparameters that will construct 25 | a sklearn.decomposition.PCA transformer. 26 | 27 | Args: 28 | name: name | str 29 | n_components: number of components to keep | int 30 | whiten: whether to apply whitening | bool 31 | copy: whether to overwrite or copy | bool 32 | """ 33 | rval = scope.sklearn_PCA( 34 | # -- qloguniform is missing a "scale" parameter so we 35 | # lower the "high" parameter and multiply by 4 out front 36 | n_components=4 * scope.int(hp.qloguniform(name + ".n_components", low=np.log(0.51), high=np.log(30.5), q=1.0)) 37 | if n_components is None else n_components, 38 | whiten=hp.choice(name + ".whiten", [True, False]) if whiten is None else whiten, 39 | copy=copy, 40 | ) 41 | 42 | return rval 43 | -------------------------------------------------------------------------------- /hpsklearn/components/discriminant_analysis.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import discriminant_analysis 7 | import numpy.typing as npt 8 | import numpy as np 9 | import typing 10 | 11 | 12 | @scope.define 13 | def sklearn_LinearDiscriminantAnalysis(*args, **kwargs): 14 | return discriminant_analysis.LinearDiscriminantAnalysis(*args, **kwargs) 15 | 16 | 17 | @scope.define 18 | def sklearn_QuadraticDiscriminantAnalysis(*args, **kwargs): 19 | return discriminant_analysis.QuadraticDiscriminantAnalysis(*args, **kwargs) 20 | 21 | 22 | def _discriminant_analysis_tol(name: str): 23 | """ 24 | Declaration search space 'tol' parameter 25 | """ 26 | return hp.loguniform(name, np.log(1e-5), np.log(1e-2)) 27 | 28 | 29 | def _discriminant_analysis_hp_space( 30 | name_func, 31 | priors: npt.ArrayLike = None, 32 | store_covariance: bool = False, 33 | tol: float = None, 34 | **kwargs 35 | ): 36 | """ 37 | Common hyper parameter search space 38 | linear discriminant analysis 39 | quadratic discriminant analysis 40 | """ 41 | hp_space = dict( 42 | priors=priors, 43 | store_covariance=store_covariance, 44 | tol=_discriminant_analysis_tol(name_func("tol")) if tol is None else tol, 45 | **kwargs 46 | ) 47 | return hp_space 48 | 49 | 50 | @validate(params=["solver"], 51 | validation_test=lambda param: not isinstance(param, str) or param in ["svd", "lsqr", "eigen"], 52 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['svd', 'lsqr', 'eigen'].") 53 | @validate(params=["shrinkage"], 54 | validation_test=lambda param: not isinstance(param, str) or param == "auto", 55 | msg="Invalid parameter '%s' with value '%s'. Value must be 'auto' or float.") 56 | def linear_discriminant_analysis(name: str, 57 | solver: typing.Union[str, Apply] = None, 58 | shrinkage: typing.Union[float, str, Apply] = None, 59 | n_components: int = None, 60 | covariance_estimator: callable = None, 61 | **kwargs): 62 | """ 63 | Return a pyll graph with hyperparameters that will construct 64 | a sklearn.discriminant_analysis.LinearDiscriminantAnalysis model. 65 | 66 | Args: 67 | name: name | str 68 | solver: solver to use | str 69 | shrinkage: shrinkage parameter | str or float 70 | n_components: number of components | int 71 | covariance_estimator: covariance estimator to use | callable 72 | 73 | See help(hpsklearn.components.discriminant_analysis._discriminant_analysis_hp_space) 74 | for info on additional available discriminant analysis arguments. 75 | """ 76 | 77 | def _name(msg): 78 | return f"{name}.linear_discriminant_analysis_{msg}" 79 | 80 | hp_space = _discriminant_analysis_hp_space(_name, **kwargs) 81 | hp_space["solver"] = hp.choice(_name("solver"), ["svd", "lsqr", "eigen"]) if solver is None else solver 82 | hp_space["shrinkage"] = shrinkage 83 | hp_space["n_components"] = n_components 84 | hp_space["covariance_estimator"] = covariance_estimator 85 | 86 | return scope.sklearn_LinearDiscriminantAnalysis(**hp_space) 87 | 88 | 89 | def quadratic_discriminant_analysis(name: str, reg_param: typing.Union[float, Apply] = None, **kwargs): 90 | """ 91 | Return a pyll graph with hyperparameters that will construct 92 | a sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis model. 93 | 94 | Args: 95 | name: name | str 96 | reg_param: regularization parameter | float 97 | 98 | See help(hpsklearn.components.discriminant_analysis._discriminant_analysis_hp_space) 99 | for info on additional available discriminant analysis arguments. 100 | """ 101 | 102 | def _name(msg): 103 | return f"{name}.quadratic_discriminant_analysis_{msg}" 104 | 105 | hp_space = _discriminant_analysis_hp_space(_name, **kwargs) 106 | hp_space["reg_param"] = hp.uniform(_name("reg_param"), 0.0, 0.5) if reg_param is None else reg_param 107 | 108 | return scope.sklearn_QuadraticDiscriminantAnalysis(**hp_space) 109 | -------------------------------------------------------------------------------- /hpsklearn/components/dummy.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import dummy 7 | import numpy.typing as npt 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_DummyClassifier(*args, **kwargs): 13 | return dummy.DummyClassifier(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_DummyRegressor(*args, **kwargs): 18 | return dummy.DummyRegressor(*args, **kwargs) 19 | 20 | 21 | @validate(params=["strategy"], 22 | validation_test=lambda param: not isinstance(param, str) or param in ["stratified", "most_frequent", "prior", 23 | "uniform", "constant"], 24 | msg="Invalid parameter '%s' with value '%s'. " 25 | "Value must be in ['stratified', 'most_frequent', 'prior', 'uniform', 'constant'].") 26 | def dummy_classifier(name: str, 27 | strategy: typing.Union[str, Apply] = None, 28 | random_state=None, 29 | constant: typing.Union[int, str, npt.ArrayLike] = None, 30 | **kwargs): 31 | """ 32 | Return a pyll graph with hyperparameters that will construct 33 | a sklearn.dummy.DummyClassifier model. 34 | 35 | Args: 36 | name: name | str 37 | strategy: strategy to generate predictions | str 38 | random_state: randomness control predictions 39 | constant: constant for 'constant' strategy | int, str, npt.ArrayLike 40 | """ 41 | 42 | def _name(msg): 43 | return f"{name}.dummy_classifier_{msg}" 44 | 45 | hp_space = dict( 46 | strategy=hp.choice(_name("strategy"), ["stratified", "most_frequent", "prior", "uniform"]) 47 | if strategy is None else strategy, 48 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 49 | constant=constant, 50 | **kwargs 51 | ) 52 | return scope.sklearn_DummyClassifier(**hp_space) 53 | 54 | 55 | @validate(params=["strategy"], 56 | validation_test=lambda param: not isinstance(param, str) or param in ["mean", "median", "quantile", 57 | "constant"], 58 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['mean', 'median', 'quantile', 'constant'].") 59 | @validate(params=["quantile"], 60 | validation_test=lambda param: not isinstance(param, float) or 0 >= param >= 1, 61 | msg="Invalid parameter '%s' with value '%s'. Value must be between [0.0, 1.0].") 62 | def dummy_regressor(name: str, 63 | strategy: typing.Union[str, Apply] = None, 64 | constant: typing.Union[int, str, npt.ArrayLike] = None, 65 | quantile: float = None, 66 | **kwargs): 67 | """ 68 | Return a pyll graph with hyperparameters that will construct 69 | a sklearn.dummy.DummyRegressor model. 70 | 71 | Args: 72 | name: name | str 73 | strategy: strategy to generate predictions | str 74 | constant: constant for 'constant' strategy | int, str, npt.ArrayLike 75 | quantile: quantile for 'quantile' strategy | float 76 | """ 77 | 78 | def _name(msg): 79 | return f"{name}.dummy_regressor_{msg}" 80 | 81 | hp_space = dict( 82 | strategy=hp.choice(_name("strategy"), ["mean", "median"]) if strategy is None else strategy, 83 | constant=constant, 84 | quantile=quantile, 85 | **kwargs 86 | ) 87 | return scope.sklearn_DummyRegressor(**hp_space) 88 | -------------------------------------------------------------------------------- /hpsklearn/components/ensemble/__init__.py: -------------------------------------------------------------------------------- 1 | from ._forest import \ 2 | random_forest_classifier, \ 3 | random_forest_regressor, \ 4 | extra_trees_classifier, \ 5 | extra_trees_regressor, \ 6 | forest_classifiers, \ 7 | forest_regressors 8 | 9 | from ._bagging import \ 10 | bagging_classifier, \ 11 | bagging_regressor 12 | 13 | from ._iforest import \ 14 | isolation_forest 15 | 16 | from ._weight_boosting import \ 17 | ada_boost_classifier, \ 18 | ada_boost_regressor 19 | 20 | from ._gb import \ 21 | gradient_boosting_classifier, \ 22 | gradient_boosting_regressor 23 | 24 | from ._hist_gradient_boosting import \ 25 | hist_gradient_boosting_classifier, \ 26 | hist_gradient_boosting_regressor 27 | -------------------------------------------------------------------------------- /hpsklearn/components/ensemble/_bagging.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import ensemble 7 | import typing 8 | 9 | 10 | @scope.define 11 | def sklearn_BaggingClassifier(*args, **kwargs): 12 | return ensemble.BaggingClassifier(*args, **kwargs) 13 | 14 | 15 | @scope.define 16 | def sklearn_BaggingRegressor(*args, **kwargs): 17 | return ensemble.BaggingRegressor(*args, **kwargs) 18 | 19 | 20 | def _bagging_n_estimators(name: str): 21 | """ 22 | Declaration search space 'n_estimators' parameter 23 | """ 24 | return hp.pchoice(name, [ 25 | # create custom distribution 26 | (0.0625, 8), 27 | (0.175, 9), 28 | (0.525, 10), # most common choice 29 | (0.175, 11), 30 | (0.0625, 12), 31 | ]) 32 | 33 | 34 | def _bagging_max_samples(name: str): 35 | """ 36 | Declaration search space 'max_samples' parameter 37 | """ 38 | return hp.pchoice(name, [ 39 | (0.05, 0.8), 40 | (0.15, 0.9), 41 | (0.8, 1.0), # most common choice 42 | ]) 43 | 44 | 45 | def _bagging_max_features(name: str): 46 | """ 47 | Declaration search space 'max_features' parameter 48 | """ 49 | return hp.pchoice(name, [ 50 | (0.05, 0.8), 51 | (0.15, 0.9), 52 | (0.8, 1.0), # most common choice 53 | ]) 54 | 55 | 56 | def _bagging_bootstrap(name: str): 57 | """ 58 | Declaration search space 'bootstrap' parameter 59 | """ 60 | return hp.choice(name, [True, False]) 61 | 62 | 63 | def _bagging_bootstrap_features(name: str): 64 | """ 65 | Declaration search space 'bootstrap_features' parameter 66 | """ 67 | return hp.choice(name, [True, False]) 68 | 69 | 70 | def _bagging_random_state(name: str): 71 | """ 72 | Declaration search space 'random_state' parameter 73 | """ 74 | return hp.randint(name, 5) 75 | 76 | 77 | @validate(params=["max_samples", "max_features"], 78 | validation_test=lambda param: not isinstance(param, float) or param > 0, 79 | msg="Invalid parameter '%s' with value '%s'. Parameter value must be non-negative and greater than 0.") 80 | @validate(params=["n_estimators"], 81 | validation_test=lambda param: not isinstance(param, int) or param > 1, 82 | msg="Invalid parameter '%s' with value '%s'. Parameter value must exceed 1.") 83 | def _bagging_hp_space( 84 | name_func, 85 | estimator=None, 86 | n_estimators: typing.Union[int, Apply] = None, 87 | max_samples: typing.Union[float, Apply] = None, 88 | max_features: typing.Union[float, Apply] = None, 89 | bootstrap: typing.Union[bool, Apply] = None, 90 | bootstrap_features: typing.Union[bool, Apply] = None, 91 | oob_score: bool = False, 92 | warm_start: bool = False, 93 | n_jobs: int = 1, 94 | random_state=None, 95 | verbose: int = False, 96 | **kwargs, 97 | ): 98 | """ 99 | Hyper parameter search space for 100 | bagging classifier 101 | bagging regressor 102 | """ 103 | hp_space = dict( 104 | estimator=estimator, 105 | n_estimators=_bagging_n_estimators(name_func("n_estimators")) if n_estimators is None else n_estimators, 106 | max_samples=_bagging_max_samples(name_func("max_samples")) if max_samples is None else max_samples, 107 | max_features=_bagging_max_features(name_func("max_features")) if max_features is None else max_features, 108 | bootstrap=_bagging_bootstrap(name_func("bootstrap")) if bootstrap is None else bootstrap, 109 | bootstrap_features=_bagging_bootstrap_features(name_func("bootstrap_features")) 110 | if bootstrap_features is None else bootstrap_features, 111 | oob_score=oob_score, 112 | warm_start=warm_start, 113 | n_jobs=n_jobs, 114 | random_state=_bagging_random_state(name_func("random_state")) if random_state is None else random_state, 115 | verbose=verbose, 116 | **kwargs 117 | ) 118 | return hp_space 119 | 120 | 121 | def bagging_classifier(name: str, **kwargs): 122 | """ 123 | Return a pyll graph with hyperparameters that will construct 124 | a sklearn.ensemble.BaggingClassifier model. 125 | 126 | Args: 127 | name: name | str 128 | 129 | See help(hpsklearn.components.ensemble._bagging._bagging_hp_space) 130 | for info on additional available bagging arguments. 131 | """ 132 | 133 | def _name(msg): 134 | return f"{name}.bc_{msg}" 135 | 136 | hp_space = _bagging_hp_space(_name, **kwargs) 137 | 138 | return scope.sklearn_BaggingClassifier(**hp_space) 139 | 140 | 141 | def bagging_regressor(name: str, **kwargs): 142 | """ 143 | Return a pyll graph with hyperparameters that will construct 144 | a sklearn.ensemble.BaggingRegressor model. 145 | 146 | Args: 147 | name: name | str 148 | 149 | See help(hpsklearn.components.ensemble._bagging._bagging_hp_space) 150 | for info on additional available bagging arguments. 151 | """ 152 | 153 | def _name(msg): 154 | return f"{name}.br_{msg}" 155 | 156 | hp_space = _bagging_hp_space(_name, **kwargs) 157 | 158 | return scope.sklearn_BaggingRegressor(**hp_space) 159 | -------------------------------------------------------------------------------- /hpsklearn/components/ensemble/_iforest.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import ensemble 7 | import numpy as np 8 | 9 | import typing 10 | 11 | 12 | @scope.define 13 | def sklearn_IsolationForest(*args, **kwargs): 14 | return ensemble.IsolationForest(*args, **kwargs) 15 | 16 | 17 | def _iforest_n_estimators(name: str): 18 | """ 19 | Declaration search space 'n_estimators' parameter 20 | """ 21 | return scope.int(hp.qloguniform(name, np.log(9.5), np.log(3000.5), 1)) 22 | 23 | 24 | def _iforest_max_features(name: str): 25 | """ 26 | Declaration search space 'max_features' parameter 27 | """ 28 | return hp.pchoice(name, [ 29 | (0.05, 0.8), 30 | (0.15, 0.9), 31 | (0.8, 1.0), # most common choice 32 | ]) 33 | 34 | 35 | def _iforest_bootstrap(name: str): 36 | """ 37 | Declaration search space 'bootstrap' parameter 38 | """ 39 | return hp.choice(name, [True, False]) 40 | 41 | 42 | def _iforest_random_state(name: str): 43 | """ 44 | Declaration search space 'random_state' parameter 45 | """ 46 | return hp.randint(name, 5) 47 | 48 | 49 | @validate(params=["contamination"], 50 | validation_test=lambda param: not isinstance(param, float) or 0 < param <= .5, 51 | msg="Invalid parameter '%s' with value '%s'. Parameter value should be in the range (0, 0.5].") 52 | def _iforest_hp_space( 53 | name_func, 54 | n_estimators: typing.Union[int, Apply] = None, 55 | max_samples: typing.Union[str, float, Apply] = "auto", 56 | contamination: typing.Union[str, float, Apply] = "auto", 57 | max_features: typing.Union[float, Apply] = None, 58 | bootstrap: typing.Union[bool, Apply] = None, 59 | n_jobs: int = 1, 60 | random_state=None, 61 | verbose: int = False, 62 | warm_start: bool = False, 63 | **kwargs 64 | ): 65 | """ 66 | Hyper parameter search space for 67 | isolation forest algorithm 68 | """ 69 | hp_space = dict( 70 | n_estimators=_iforest_n_estimators(name_func("n_estimators")) if n_estimators is None else n_estimators, 71 | max_samples=max_samples, 72 | contamination=contamination, 73 | max_features=_iforest_max_features(name_func("max_features")) if max_features is None else max_features, 74 | bootstrap=_iforest_bootstrap(name_func("bootstrap")) if bootstrap is None else bootstrap, 75 | n_jobs=n_jobs, 76 | random_state=_iforest_random_state(name_func("random_state")) if random_state is None else random_state, 77 | verbose=verbose, 78 | warm_start=warm_start, 79 | **kwargs 80 | ) 81 | return hp_space 82 | 83 | 84 | def isolation_forest(name: str, **kwargs): 85 | """ 86 | Return a pyll graph with hyperparameters that will construct 87 | a sklearn.ensemble.IsolationForest model. 88 | 89 | Args: 90 | name: name | str 91 | 92 | See help(hpsklearn.components.ensemble._iforest._iforest_hp_space) 93 | for info on additional available bagging arguments. 94 | """ 95 | 96 | def _name(msg): 97 | return f"{name}.if_{msg}" 98 | 99 | hp_space = _iforest_hp_space(_name, **kwargs) 100 | 101 | return scope.sklearn_IsolationForest(**hp_space) 102 | -------------------------------------------------------------------------------- /hpsklearn/components/ensemble/_weight_boosting.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import ensemble 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_AdaBoostClassifier(*args, **kwargs): 13 | return ensemble.AdaBoostClassifier(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_AdaBoostRegressor(*args, **kwargs): 18 | return ensemble.AdaBoostRegressor(*args, **kwargs) 19 | 20 | 21 | def _weight_boosting_n_estimators(name: str): 22 | """ 23 | Declaration search space 'n_estimators' parameter 24 | """ 25 | return scope.int(hp.qloguniform(name, np.log(10.5), np.log(1000.5), 1)) 26 | 27 | 28 | def _weight_boosting_learning_rate(name: str): 29 | """ 30 | Declaration search space 'learning_rate' parameter 31 | """ 32 | return hp.lognormal(name, np.log(0.01), np.log(10.0)) 33 | 34 | 35 | def _weight_boosting_loss(name: str): 36 | """ 37 | Declaration search space 'loss' parameter 38 | """ 39 | return hp.choice(name, ["linear", "square", "exponential"]) 40 | 41 | 42 | def _weight_boosting_random_state(name: str): 43 | """ 44 | Declaration search space 'random_state' parameter 45 | """ 46 | return hp.randint(name, 5) 47 | 48 | 49 | @validate(params=["n_estimators", "learning_rate"], 50 | validation_test=lambda param: not isinstance(param, float) or param > 0, 51 | msg="Invalid parameter '%s' with value '%s'. Parameter value must be non-negative and greater than 0.") 52 | def _weight_boosting_hp_space( 53 | name_func, 54 | estimator=None, 55 | n_estimators: typing.Union[int, Apply] = None, 56 | learning_rate: typing.Union[float, Apply] = None, 57 | random_state=None, 58 | **kwargs 59 | ): 60 | """ 61 | Hyper parameter search space for 62 | AdaBoost classifier 63 | AdaBoost regressor 64 | """ 65 | hp_space = dict( 66 | estimator=estimator, 67 | n_estimators=_weight_boosting_n_estimators(name_func("n_estimators")) if n_estimators is None else n_estimators, 68 | learning_rate=_weight_boosting_learning_rate(name_func("learning_rate")) 69 | if learning_rate is None else learning_rate, 70 | random_state=_weight_boosting_random_state(name_func("random_state")) if random_state is None else random_state, 71 | **kwargs 72 | ) 73 | return hp_space 74 | 75 | 76 | def ada_boost_classifier(name: str, **kwargs): 77 | """ 78 | Return a pyll graph with hyperparameters that will construct 79 | a sklearn.ensemble.AdaBoostClassifier model. 80 | 81 | Args: 82 | name: name | str 83 | 84 | See help(hpsklearn.components.ensemble._weight_boosting._weight_boosting_hp_space) 85 | for info on additional available AdaBoost arguments. 86 | """ 87 | 88 | def _name(msg): 89 | return f"{name}.ada_boost_{msg}" 90 | 91 | hp_space = _weight_boosting_hp_space(_name, **kwargs) 92 | 93 | return scope.sklearn_AdaBoostClassifier(**hp_space) 94 | 95 | 96 | def ada_boost_regressor(name: str, loss: typing.Union[str, Apply] = None, **kwargs): 97 | """ 98 | Return a pyll graph with hyperparameters that will construct 99 | a sklearn.ensemble.AdaBoostClassifier model. 100 | 101 | Args: 102 | name: name | str 103 | loss: choose 'linear', 'square' or 'exponential' | str 104 | 105 | See help(hpsklearn.components.ensemble._weight_boosting._weight_boosting_hp_space) 106 | for info on additional available AdaBoost arguments. 107 | """ 108 | 109 | def _name(msg): 110 | return f"{name}.ada_boost_{msg}" 111 | 112 | hp_space = _weight_boosting_hp_space(_name, **kwargs) 113 | hp_space["loss"] = _weight_boosting_loss(_name("loss")) if loss is None else loss 114 | 115 | return scope.sklearn_AdaBoostRegressor(**hp_space) 116 | -------------------------------------------------------------------------------- /hpsklearn/components/feature_extraction/__init__.py: -------------------------------------------------------------------------------- 1 | from .text import \ 2 | tfidf_vectorizer, \ 3 | hashing_vectorizer, \ 4 | count_vectorizer 5 | -------------------------------------------------------------------------------- /hpsklearn/components/gaussian_process/__init__.py: -------------------------------------------------------------------------------- 1 | from ._gpc import gaussian_process_classifier 2 | from ._gpr import gaussian_process_regressor 3 | -------------------------------------------------------------------------------- /hpsklearn/components/gaussian_process/_gpc.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import gaussian_process 7 | import typing 8 | 9 | 10 | @scope.define 11 | def sklearn_GaussianProcessClassifier(*args, **kwargs): 12 | return gaussian_process.GaussianProcessClassifier(*args, **kwargs) 13 | 14 | 15 | @validate(params=["optimizer"], 16 | validation_test=lambda param: not isinstance(param, str) or param == "fmin_l_bfgs_b", 17 | msg="Invalid parameter '%s' with value '%s'. Value must be 'fmin_l_bfgs_b' or callable.") 18 | @validate(params=["multi_class"], 19 | validation_test=lambda param: not isinstance(param, str) or param in ["one_vs_rest", "one_vs_one"], 20 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['one_vs_rest', 'one_vs_one'].") 21 | def gaussian_process_classifier(name: str, 22 | kernel=None, 23 | optimizer: typing.Union[str, callable, Apply] = None, 24 | n_restarts_optimizer: typing.Union[int, Apply] = None, 25 | max_iter_predict: typing.Union[int, Apply] = None, 26 | warm_start: bool = False, 27 | copy_X_train: bool = True, 28 | random_state=None, 29 | multi_class: typing.Union[str, Apply] = None, 30 | n_jobs: int = 1, 31 | **kwargs): 32 | """ 33 | Return a pyll graph with hyperparameters that will construct 34 | a sklearn.gaussian_process.GaussianProcessClassifier model. 35 | 36 | Args: 37 | name: name | str 38 | kernel: kernel instance 39 | optimizer: optimizer for kernel parameter optimization | str, callable 40 | n_restarts_optimizer: number of restarts optimizer | int 41 | max_iter_predict: maximum number of iterations for prediction | int 42 | warm_start: reuse previous fit solution | bool 43 | copy_X_train: store persistent copy of training data | bool 44 | random_state: random seed for center initialization | int 45 | multi_class: how multi class problems are handled | str 46 | n_jobs: number of CPUs to use | int 47 | """ 48 | 49 | def _name(msg): 50 | return f"{name}.gaussian_process_classifier_{msg}" 51 | 52 | hp_space = dict( 53 | kernel=kernel, 54 | optimizer="fmin_l_bfgs_b" if optimizer is None else optimizer, 55 | n_restarts_optimizer=hp.pchoice(_name("n_restarts_optimizer"), [(0.5, 0), (0.10, 1), (0.10, 2), (0.10, 3), 56 | (0.10, 4), (0.10, 5)]) 57 | if n_restarts_optimizer is None else n_restarts_optimizer, 58 | max_iter_predict=scope.int(hp.uniform(_name("max_iter_predict"), 75, 225)) 59 | if max_iter_predict is None else max_iter_predict, 60 | warm_start=warm_start, 61 | copy_X_train=copy_X_train, 62 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 63 | multi_class=hp.choice(_name("multi_class"), ["one_vs_rest", "one_vs_one"]) 64 | if multi_class is None else multi_class, 65 | n_jobs=n_jobs, 66 | **kwargs 67 | ) 68 | return scope.sklearn_GaussianProcessClassifier(**hp_space) 69 | -------------------------------------------------------------------------------- /hpsklearn/components/gaussian_process/_gpr.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import gaussian_process 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_GaussianProcessRegressor(*args, **kwargs): 13 | return gaussian_process.GaussianProcessRegressor(*args, **kwargs) 14 | 15 | 16 | @validate(params=["optimizer"], 17 | validation_test=lambda param: not isinstance(param, str) or param == "fmin_l_bfgs_b", 18 | msg="Invalid parameter '%s' with value '%s'. Value must be 'fmin_l_bfgs_b' or callable.") 19 | def gaussian_process_regressor(name: str, 20 | kernel=None, 21 | alpha: typing.Union[float, np.ndarray, Apply] = None, 22 | optimizer: typing.Union[str, callable, Apply] = None, 23 | n_restarts_optimizer: typing.Union[int, Apply] = None, 24 | normalize_y: bool = False, 25 | copy_X_train: bool = True, 26 | random_state=None, 27 | **kwargs): 28 | """ 29 | Return a pyll graph with hyperparameters that will construct 30 | a sklearn.gaussian_process.GaussianProcessRegressor model. 31 | 32 | Args: 33 | name: name | str 34 | kernel: kernel instance 35 | alpha: add value to diagonal of kernel matrix during fitting | float, np.ndarray 36 | optimizer: optimizer for kernel parameter optimization | str, callable 37 | n_restarts_optimizer: number of restarts optimizer | int 38 | normalize_y: normalize target values | bool 39 | copy_X_train: store persistent copy of training data | bool 40 | random_state: random seed for center initialization | int 41 | """ 42 | 43 | def _name(msg): 44 | return f"{name}.gaussian_process_regressor_{msg}" 45 | 46 | hp_space = dict( 47 | kernel=kernel, 48 | alpha=hp.loguniform(_name("alpha"), np.log(1e-10), np.log(1e-2)) if alpha is None else alpha, 49 | optimizer="fmin_l_bfgs_b" if optimizer is None else optimizer, 50 | n_restarts_optimizer=hp.pchoice(_name("n_restarts_optimizer"), [(0.5, 0), (0.10, 1), (0.10, 2), (0.10, 3), 51 | (0.10, 4), (0.10, 5)]) 52 | if n_restarts_optimizer is None else n_restarts_optimizer, 53 | normalize_y=normalize_y, 54 | copy_X_train=copy_X_train, 55 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 56 | **kwargs 57 | ) 58 | return scope.sklearn_GaussianProcessRegressor(**hp_space) 59 | -------------------------------------------------------------------------------- /hpsklearn/components/kernel_ridge.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import kernel_ridge 7 | import numpy.typing as npt 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_KernelRidge(*args, **kwargs): 13 | return kernel_ridge.KernelRidge(*args, **kwargs) 14 | 15 | 16 | @validate(params=["kernel"], 17 | validation_test=lambda param: not isinstance(param, str) or 18 | param in ["additive_chi2", "chi2", "cosine", "linear", "poly", # noqa 19 | "polynomial", "rbf", "laplacian", "sigmoid"], 20 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['additive_chi2', 'chi2', 'cosine', 'linear', " 21 | "'poly', 'polynomial', 'rbf', 'laplacian', 'sigmoid'].") 22 | def hp_sklearn_kernel_ridge(name: str, 23 | alpha: typing.Union[float, npt.ArrayLike, Apply] = None, 24 | kernel: typing.Union[str, callable, Apply] = None, 25 | gamma: typing.Union[float, Apply] = None, 26 | degree: typing.Union[float, Apply] = None, 27 | coef0: typing.Union[float, Apply] = None, 28 | kernel_params: map = None, 29 | **kwargs): 30 | """ 31 | Return a pyll graph with hyperparameters that will construct 32 | a sklearn.kernel_ridge.KernelRidge model. 33 | 34 | Args: 35 | name: name | str 36 | alpha: regularization strength | float, ArrayLike 37 | kernel: kernel mapping | str, callable 38 | gamma: gamma parameter specific kernels | float 39 | degree: degree of polynomial kernel | float 40 | coef0: zero coefficient for poly and sigmoid kernel | float 41 | kernel_params: additional kernel parameters | map 42 | """ 43 | 44 | def _name(msg): 45 | return f"{name}.kernel_ridge_{msg}" 46 | 47 | hp_space = dict( 48 | alpha=hp.uniform(_name("alpha"), 0.0, 1.0) if alpha is None else alpha, 49 | kernel=hp.choice(_name("kernel"), ["additive_chi2", "chi2", "cosine", "linear", "poly", "polynomial", "rbf", 50 | "laplacian", "sigmoid"]) if kernel is None else kernel, 51 | gamma=hp.uniform(_name("gamma"), 0.0, 1.0) if gamma is None else gamma, 52 | degree=scope.int(hp.uniform(_name("degree"), 1, 7)) if degree is None else degree, 53 | coef0=hp.uniform(_name("coef0"), 0.0, 1.0) if coef0 is None else coef0, 54 | kernel_params=kernel_params, 55 | **kwargs 56 | ) 57 | return scope.sklearn_KernelRidge(**hp_space) 58 | -------------------------------------------------------------------------------- /hpsklearn/components/lagselectors.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.objects import LagSelector 2 | 3 | from hyperopt.pyll import scope 4 | from hyperopt import hp 5 | 6 | 7 | @scope.define 8 | def ts_LagSelector(*args, **kwargs): 9 | return LagSelector(*args, **kwargs) 10 | 11 | 12 | def ts_lagselector(name: str, lower_lags: float = 1, upper_lags: float = 1): 13 | """ 14 | Return a pyll graph with hyperparameters that will construct 15 | a hpsklearn.objects.LagSelector transformer. 16 | 17 | Args: 18 | name: name | str 19 | lower_lags: lower lag size | float 20 | upper_lags: upper lag size | float 21 | """ 22 | rval = scope.ts_LagSelector( 23 | lag_size=scope.int(hp.quniform(name + ".lags", lower_lags - .5, upper_lags + .5, 1)) 24 | ) 25 | return rval 26 | -------------------------------------------------------------------------------- /hpsklearn/components/lightgbm.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | import numpy as np 7 | import typing 8 | 9 | try: 10 | import lightgbm 11 | except ImportError: 12 | lightgbm = None 13 | 14 | 15 | @scope.define 16 | def sklearn_LGBMClassifier(*args, **kwargs): 17 | if lightgbm is None: 18 | raise ImportError("No module named lightgbm") 19 | return lightgbm.LGBMClassifier(*args, **kwargs) 20 | 21 | 22 | @scope.define 23 | def sklearn_LGBMRegressor(*args, **kwargs): 24 | if lightgbm is None: 25 | raise ImportError("No module named lightgbm") 26 | return lightgbm.LGBMRegressor(*args, **kwargs) 27 | 28 | 29 | def _lightgbm_max_depth(name): 30 | """ 31 | Declaration search space 'max_depth' parameter 32 | """ 33 | return scope.int(hp.uniform(name, 1, 11)) 34 | 35 | 36 | def _lightgbm_num_leaves(name): 37 | """ 38 | Declaration search space 'num_leaves' parameter 39 | """ 40 | return scope.int(hp.uniform(name, 2, 121)) 41 | 42 | 43 | def _lightgbm_learning_rate(name): 44 | """ 45 | Declaration search space 'learning_rate' parameter 46 | """ 47 | return hp.loguniform(name, np.log(0.0001), np.log(0.5)) - 0.0001 48 | 49 | 50 | def _lightgbm_n_estimators(name): 51 | """ 52 | Declaration search space 'n_estimators' parameter 53 | """ 54 | return scope.int(hp.quniform(name, 100, 6000, 200)) 55 | 56 | 57 | def _lightgbm_min_child_weight(name): 58 | """ 59 | Declaration search space 'min_child_weight' parameter 60 | """ 61 | return scope.int(hp.loguniform(name, np.log(1), np.log(100))) 62 | 63 | 64 | def _lightgbm_subsample(name): 65 | """ 66 | Declaration search space 'subsample' parameter 67 | """ 68 | return hp.uniform(name, 0.5, 1) 69 | 70 | 71 | def _lightgbm_colsample_bytree(name): 72 | """ 73 | Declaration search space 'colsample_bytree' parameter 74 | """ 75 | return hp.uniform(name, 0.5, 1) 76 | 77 | 78 | def _lightgbm_reg_alpha(name): 79 | """ 80 | Declaration search space 'reg_alpha' parameter 81 | """ 82 | return hp.loguniform(name, np.log(0.0001), np.log(1)) - 0.0001 83 | 84 | 85 | def _lightgbm_reg_lambda(name): 86 | """ 87 | Declaration search space 'reg_lambda' parameter 88 | """ 89 | return hp.loguniform(name, np.log(1), np.log(4)) 90 | 91 | 92 | def _lightgbm_boosting_type(name): 93 | """ 94 | Declaration search space 'boosting_type' parameter 95 | """ 96 | return hp.choice(name, ["gbdt", "dart", "goss"]) 97 | 98 | 99 | def _lightgbm_random_state(name: str): 100 | """ 101 | Declaration search space 'random_state' parameter 102 | """ 103 | return hp.randint(name, 5) 104 | 105 | 106 | @validate(params=["boosting_type"], 107 | validation_test=lambda param: not isinstance(param, str) or param in ["gbdt", "dart", "goss"], 108 | msg="Invalid parameter '%s' with value '%s'. Value must be one of ['gbdt', 'dart', 'goss'].") 109 | def _lightgbm_hp_space( 110 | name_func, 111 | max_depth: typing.Union[int, Apply] = None, 112 | num_leaves: typing.Union[int, Apply] = None, 113 | learning_rate: typing.Union[float, Apply] = None, 114 | n_estimators: typing.Union[int, Apply] = None, 115 | min_child_weight: typing.Union[float, Apply] = None, 116 | max_delta_step: float = 0, 117 | subsample: typing.Union[float, Apply] = None, 118 | colsample_bytree: typing.Union[float, Apply] = None, 119 | reg_alpha: typing.Union[float, Apply] = None, 120 | reg_lambda: typing.Union[float, Apply] = None, 121 | boosting_type: typing.Union[str, Apply] = None, 122 | scale_pos_weight: float = 1, 123 | random_state=None, 124 | **kwargs): 125 | """ 126 | Hyper parameter search space for 127 | lightgbm classifier 128 | lightgbm regressor 129 | """ 130 | hp_space = dict( 131 | # max_depth=_lightgbm_max_depth(name_func("max_depth")) if max_depth is None else max_depth, 132 | max_depth=-1, 133 | num_leaves=_lightgbm_num_leaves(name_func("num_leaves")) if num_leaves is None else num_leaves, 134 | learning_rate=_lightgbm_learning_rate(name_func("learning_rate")) if learning_rate is None else learning_rate, 135 | n_estimators=_lightgbm_n_estimators(name_func("n_estimators")) if n_estimators is None else n_estimators, 136 | min_child_weight=_lightgbm_min_child_weight(name_func("min_child_weight")) 137 | if min_child_weight is None else min_child_weight, 138 | # min_child_samples=5, 139 | max_delta_step=max_delta_step, 140 | subsample=_lightgbm_subsample(name_func("subsample")) if subsample is None else subsample, 141 | colsample_bytree=_lightgbm_colsample_bytree(name_func("colsample_bytree")) 142 | if colsample_bytree is None else colsample_bytree, 143 | reg_alpha=_lightgbm_reg_alpha(name_func("reg_alpha")) if reg_alpha is None else reg_alpha, 144 | reg_lambda=_lightgbm_reg_lambda(name_func("reg_lambda")) if reg_lambda is None else reg_lambda, 145 | boosting_type=_lightgbm_boosting_type(name_func("boosting_type")) if boosting_type is None else boosting_type, 146 | scale_pos_weight=scale_pos_weight, 147 | seed=_lightgbm_random_state(name_func("random_state")) if random_state is None else random_state, 148 | **kwargs 149 | ) 150 | return hp_space 151 | 152 | 153 | @validate(params=["objective"], 154 | validation_test=lambda param: not isinstance(param, str) or param in ["binary", "multiclass"], 155 | msg="Invalid parameter '%s' with value '%s'. Value must be 'binary' or 'multiclass'.") 156 | def lightgbm_classification(name: str, objective: str = None, **kwargs): 157 | """ 158 | Return a pyll graph with hyperparameters that will construct 159 | a lightgbm.LGBMClassifier model. 160 | 161 | Args: 162 | name: name | str 163 | objective: 'binary' or ' multiclass' | str 164 | 165 | See help(hpsklearn.components._lightgbm_hp_space) for info on 166 | additional available LightGBM arguments. 167 | """ 168 | 169 | def _name(msg): 170 | return f"{name}.lightgbm_clf_{msg}" 171 | 172 | hp_space = _lightgbm_hp_space(_name, **kwargs) 173 | hp_space["objective"] = "binary" if objective is None else objective 174 | 175 | return scope.sklearn_LGBMClassifier(**hp_space) 176 | 177 | 178 | def lightgbm_regression(name: str, **kwargs): 179 | """ 180 | Return a pyll graph with hyperparameters that will construct 181 | a lightgbm.LightGBMRegressor model. 182 | 183 | Args: 184 | name: name | str 185 | 186 | See help(hpsklearn.components._lightgbm_hp_space) for info on 187 | additional available LightGBM arguments. 188 | """ 189 | 190 | def _name(msg): 191 | return f"{name}.lightgbm_reg_{msg}" 192 | 193 | hp_space = _lightgbm_hp_space(_name, **kwargs) 194 | 195 | return scope.sklearn_LGBMRegressor(objective="regression", **hp_space) 196 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/__init__.py: -------------------------------------------------------------------------------- 1 | from ._base import \ 2 | linear_regression 3 | 4 | from ._bayes import \ 5 | bayesian_ridge, \ 6 | ard_regression 7 | 8 | from ._least_angle import \ 9 | lars, \ 10 | lasso_lars, \ 11 | lars_cv, \ 12 | lasso_lars_cv, \ 13 | lasso_lars_ic 14 | 15 | from ._coordinate_descent import \ 16 | lasso, \ 17 | elastic_net, \ 18 | lasso_cv, \ 19 | elastic_net_cv, \ 20 | multi_task_lasso, \ 21 | multi_task_elastic_net, \ 22 | multi_task_lasso_cv, \ 23 | multi_task_elastic_net_cv 24 | 25 | from ._glm import \ 26 | poisson_regressor, \ 27 | gamma_regressor, \ 28 | tweedie_regressor 29 | 30 | from ._huber import \ 31 | huber_regressor 32 | 33 | from ._stochastic_gradient import \ 34 | sgd_classifier, \ 35 | sgd_regressor, \ 36 | sgd_one_class_svm 37 | 38 | from ._ridge import \ 39 | ridge, \ 40 | ridge_cv, \ 41 | ridge_classifier, \ 42 | ridge_classifier_cv 43 | 44 | from ._logistic import \ 45 | logistic_regression, \ 46 | logistic_regression_cv 47 | 48 | from ._omp import \ 49 | orthogonal_matching_pursuit, \ 50 | orthogonal_matching_pursuit_cv 51 | 52 | from ._passive_aggressive import \ 53 | passive_aggressive_classifier, \ 54 | passive_aggressive_regressor 55 | 56 | from ._perceptron import \ 57 | perceptron 58 | 59 | from ._quantile import \ 60 | quantile_regression 61 | 62 | from ._ransac import \ 63 | ransac_regression 64 | 65 | from ._theil_sen import \ 66 | theil_sen_regressor 67 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_base.py: -------------------------------------------------------------------------------- 1 | from hyperopt.pyll import scope 2 | 3 | from sklearn import linear_model 4 | 5 | 6 | @scope.define 7 | def sklearn_LinearRegression(*args, **kwargs): 8 | return linear_model.LinearRegression(*args, **kwargs) 9 | 10 | 11 | def linear_regression(name: str, 12 | fit_intercept: bool = True, 13 | copy_X: bool = True, 14 | n_jobs: int = 1, 15 | positive: bool = False, 16 | **kwargs): 17 | """ 18 | Return a pyll graph with hyperparameters that will construct 19 | a sklearn.linear_model.LinearRegression model. 20 | 21 | Args: 22 | name: name | str 23 | fit_intercept: calculate intercept for model | bool 24 | copy_X: copy or overwrite X | bool 25 | n_jobs: number of jobs for computation | int 26 | positive: force coefficient to be positive | bool 27 | """ 28 | 29 | def _name(msg): 30 | return f"{name}.linear_regression_{msg}" 31 | 32 | hp_space = dict( 33 | fit_intercept=fit_intercept, 34 | copy_X=copy_X, 35 | n_jobs=n_jobs, 36 | positive=positive, 37 | **kwargs 38 | ) 39 | 40 | return scope.sklearn_LinearRegression(**hp_space) 41 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_bayes.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import linear_model 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_BayesianRidge(*args, **kwargs): 13 | return linear_model.BayesianRidge(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_ARDRegression(*args, **kwargs): 18 | return linear_model.ARDRegression(*args, **kwargs) 19 | 20 | 21 | def _bayes_max_iter(name: str): 22 | """ 23 | Declaration search space 'max_iter' parameter 24 | """ 25 | return scope.int(hp.qloguniform(name, low=np.log(150), high=np.log(450), q=1.0)) 26 | 27 | 28 | def _bayes_tol(name: str): 29 | """ 30 | Declaration search space 'tol' parameter 31 | """ 32 | return hp.lognormal(name, mu=np.log(1e-3), sigma=np.log(10)) 33 | 34 | 35 | def _bayes_alpha_lambda(name: str): 36 | """ 37 | Declaration search space 'alpha_1', 'alpha_2', 38 | 'lambda_1' and 'lambda_2' parameters 39 | """ 40 | return hp.lognormal(name, mu=np.log(1e-6), sigma=np.log(10)) 41 | 42 | 43 | @validate(params=["max_iter"], 44 | validation_test=lambda param: not isinstance(param, int) or param > 1, 45 | msg="Invalid parameter '%s' with value '%s'. Parameter value must exceed 1.") 46 | @validate(params=["alpha_1", "alpha_2", "lambda_1", "lambda_2"], 47 | validation_test=lambda param: not isinstance(param, float) or param >= 0, 48 | msg="Invalid parameter '%s' with value '%s'. Parameter value must be equal to or exceed 0.") 49 | def _bayes_hp_space( 50 | name_func, 51 | max_iter: typing.Union[int, Apply] = None, 52 | tol: typing.Union[float, Apply] = None, 53 | alpha_1: typing.Union[float, Apply] = None, 54 | alpha_2: typing.Union[float, Apply] = None, 55 | lambda_1: typing.Union[float, Apply] = None, 56 | lambda_2: typing.Union[float, Apply] = None, 57 | compute_score: bool = False, 58 | fit_intercept: bool = True, 59 | copy_X: bool = True, 60 | verbose: bool = False, 61 | **kwargs 62 | ): 63 | """ 64 | Hyper parameter search space for 65 | bayesian ridge 66 | ard regression 67 | """ 68 | hp_space = dict( 69 | max_iter=_bayes_max_iter(name_func("max_iter")) if max_iter is None else max_iter, 70 | tol=_bayes_tol(name_func("tol")) if tol is None else tol, 71 | alpha_1=_bayes_alpha_lambda(name_func("alpha_1")) if alpha_1 is None else alpha_1, 72 | alpha_2=_bayes_alpha_lambda(name_func("alpha_2")) if alpha_2 is None else alpha_2, 73 | lambda_1=_bayes_alpha_lambda(name_func("lambda_1")) if lambda_1 is None else lambda_1, 74 | lambda_2=_bayes_alpha_lambda(name_func("lambda_2")) if lambda_2 is None else lambda_2, 75 | compute_score=compute_score, 76 | fit_intercept=fit_intercept, 77 | copy_X=copy_X, 78 | verbose=verbose, 79 | **kwargs 80 | ) 81 | return hp_space 82 | 83 | 84 | def bayesian_ridge(name: str, 85 | alpha_init: float = None, 86 | lambda_init: float = None, 87 | **kwargs): 88 | """ 89 | Return a pyll graph with hyperparameters that will construct 90 | a sklearn.linear_model.BayesianRidge model. 91 | 92 | Args: 93 | name: name | str 94 | alpha_init: init precision of noise | float 95 | lambda_init: init precision of weights | float 96 | 97 | See help(hpsklearn.components.linear_model._bayes._bayes_hp_space) 98 | for info on additional available bayes arguments. 99 | """ 100 | 101 | def _name(msg): 102 | return f"{name}.bayesian_ridge_{msg}" 103 | 104 | hp_space = _bayes_hp_space(_name, **kwargs) 105 | hp_space["alpha_init"] = alpha_init 106 | hp_space["lambda_init"] = lambda_init 107 | 108 | return scope.sklearn_BayesianRidge(**hp_space) 109 | 110 | 111 | def ard_regression(name: str, threshold_lambda: float = 10000, **kwargs): 112 | """ 113 | Return a pyll graph with hyperparameters that will construct 114 | a sklearn.linear_model.ARDRegression model. 115 | 116 | Args: 117 | name: name | str 118 | threshold_lambda: threshold for removing weights | float 119 | 120 | See help(hpsklearn.components.linear_model._bayes._bayes_hp_space) 121 | for info on additional available bayes arguments. 122 | """ 123 | 124 | def _name(msg): 125 | return f"{name}.ard_regression_{msg}" 126 | 127 | hp_space = _bayes_hp_space(_name, **kwargs) 128 | hp_space["threshold_lambda"] = threshold_lambda 129 | 130 | return scope.sklearn_ARDRegression(**hp_space) 131 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_glm.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import linear_model 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_PoissonRegressor(*args, **kwargs): 13 | return linear_model.PoissonRegressor(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_GammaRegressor(*args, **kwargs): 18 | return linear_model.GammaRegressor(*args, **kwargs) 19 | 20 | 21 | @scope.define 22 | def sklearn_TweedieRegressor(*args, **kwargs): 23 | return linear_model.TweedieRegressor(*args, **kwargs) 24 | 25 | 26 | def _glm_max_iter(name: str): 27 | """ 28 | Declaration search space 'max_iter' parameter 29 | """ 30 | return scope.int(hp.uniform(name, low=80, high=120)) 31 | 32 | 33 | def _glm_tol(name: str): 34 | """ 35 | Declaration search space 'tol' parameter 36 | """ 37 | return hp.loguniform(name, low=np.log(1e-5), high=np.log(1e-2)) 38 | 39 | 40 | def _glm_power(name: str): 41 | """ 42 | Declaration search space 'power' parameter 43 | """ 44 | return hp.choice(name, [0, 1, 2, 3]) 45 | 46 | 47 | @validate(params=["max_iter"], 48 | validation_test=lambda param: not isinstance(param, int) or param > 1, 49 | msg="Invalid parameter '%s' with value '%s'. Parameter must exceed 1.") 50 | def _glm_hp_space( 51 | name_func, 52 | alpha: float = 1, 53 | fit_intercept: bool = True, 54 | max_iter: typing.Union[int, Apply] = 100, 55 | tol: typing.Union[float, Apply] = 1e-4, 56 | warm_start: bool = False, 57 | verbose: int = 0, 58 | **kwargs 59 | ): 60 | """ 61 | Hyper parameter search space for 62 | poisson regressor 63 | gamma regressor 64 | tweedie regressor 65 | """ 66 | hp_space = dict( 67 | alpha=alpha, 68 | fit_intercept=fit_intercept, 69 | max_iter=_glm_max_iter(name_func("max_iter")) if max_iter is None else max_iter, 70 | tol=_glm_tol(name_func("tol")) if tol is None else tol, 71 | warm_start=warm_start, 72 | verbose=verbose, 73 | **kwargs 74 | ) 75 | return hp_space 76 | 77 | 78 | def poisson_regressor(name: str, **kwargs): 79 | """ 80 | Return a pyll graph with hyperparameters that will construct 81 | a sklearn.linear_model.PoissonRegressor model. 82 | 83 | Args: 84 | name: name | str 85 | 86 | See help(hpsklearn.components.linear_model._glm._glm_hp_space) 87 | for info on additional available glm arguments. 88 | """ 89 | 90 | def _name(msg): 91 | return f"{name}.poisson_regressor_{msg}" 92 | 93 | hp_space = _glm_hp_space(_name, **kwargs) 94 | 95 | return scope.sklearn_PoissonRegressor(**hp_space) 96 | 97 | 98 | def gamma_regressor(name: str, **kwargs): 99 | """ 100 | Return a pyll graph with hyperparameters that will construct 101 | a sklearn.linear_model.GammaRegressor model. 102 | 103 | Args: 104 | name: name | str 105 | 106 | See help(hpsklearn.components.linear_model._glm._glm_hp_space) 107 | for info on additional available glm arguments. 108 | """ 109 | 110 | def _name(msg): 111 | return f"{name}.gamma_regressor_{msg}" 112 | 113 | hp_space = _glm_hp_space(_name, **kwargs) 114 | 115 | return scope.sklearn_GammaRegressor(**hp_space) 116 | 117 | 118 | @validate(params=["link"], 119 | validation_test=lambda param: not isinstance(param, str) or param in ["auto", "identity", "log"], 120 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['auto', 'identity', 'log'].") 121 | def tweedie_regressor(name: str, 122 | power: typing.Union[float, Apply] = None, 123 | link: str = "auto", 124 | **kwargs): 125 | """ 126 | Return a pyll graph with hyperparameters that will construct 127 | a sklearn.linear_model.TweedieRegressor model. 128 | 129 | Args: 130 | name: name | str 131 | power: target distribution | float 132 | link: link function of GLM | str 133 | 134 | See help(hpsklearn.components.linear_model._glm._glm_hp_space) 135 | for info on additional available glm arguments. 136 | """ 137 | 138 | def _name(msg): 139 | return f"{name}.tweedie_regressor_{msg}" 140 | 141 | hp_space = _glm_hp_space(_name, **kwargs) 142 | hp_space["power"] = _glm_power(_name("power")) if power is None else power 143 | hp_space["link"] = link 144 | 145 | return scope.sklearn_TweedieRegressor(**hp_space) 146 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_huber.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import linear_model 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_HuberRegressor(*args, **kwargs): 13 | return linear_model.HuberRegressor(*args, **kwargs) 14 | 15 | 16 | def _glm_epsilon(name: str): 17 | """ 18 | Declaration search space 'epsilon' parameter 19 | """ 20 | return hp.normal(name, mu=1.35, sigma=0.05) 21 | 22 | 23 | def _glm_max_iter(name: str): 24 | """ 25 | Declaration search space 'max_iter' parameter 26 | """ 27 | return scope.int(hp.uniform(name, low=80, high=120)) 28 | 29 | 30 | def _glm_alpha(name: str): 31 | """ 32 | Declaration search space 'alpha' parameter 33 | """ 34 | return hp.loguniform(name, low=np.log(1e-5), high=np.log(1e-2)) 35 | 36 | 37 | def _glm_tol(name: str): 38 | """ 39 | Declaration search space 'tol' parameter 40 | """ 41 | return hp.loguniform(name, low=np.log(1e-6), high=np.log(1e-4)) 42 | 43 | 44 | @validate(params=["epsilon", "max_iter"], 45 | validation_test=lambda param: not isinstance(param, float) or param > 1, 46 | msg="Invalid parameter '%s' with value '%s'. Parameter must exceed 1.") 47 | def huber_regressor(name: str, 48 | epsilon: typing.Union[float, Apply] = None, 49 | max_iter: typing.Union[int, Apply] = None, 50 | alpha: typing.Union[float, Apply] = None, 51 | warm_start: bool = False, 52 | fit_intercept: bool = True, 53 | tol: typing.Union[float, Apply] = None, 54 | **kwargs): 55 | """ 56 | Return a pyll graph with hyperparameters that will construct 57 | a sklearn.linear_model.HuberRegressor model. 58 | 59 | Args: 60 | name: name | str 61 | epsilon: samples classified as outliers | float 62 | max_iter: max number of iterations | int 63 | alpha: regularization parameter | float 64 | warm_start: reuse attributes | bool 65 | fit_intercept: whether to fit the intercept | bool 66 | tol: threshold to stop iteration | float 67 | """ 68 | def _name(msg): 69 | return f"{name}.huber_regressor_{msg}" 70 | 71 | hp_space = dict( 72 | epsilon=_glm_epsilon(_name("epsilon")) if tol is None else epsilon, 73 | max_iter=_glm_max_iter(_name("max_iter")) if tol is None else max_iter, 74 | alpha=_glm_alpha(_name("alpha")) if tol is None else alpha, 75 | warm_start=warm_start, 76 | fit_intercept=fit_intercept, 77 | tol=_glm_tol(_name("tol")) if tol is None else tol, 78 | **kwargs 79 | ) 80 | 81 | return scope.sklearn_HuberRegressor(**hp_space) 82 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_omp.py: -------------------------------------------------------------------------------- 1 | from hyperopt.pyll import scope, Apply 2 | from hyperopt import hp 3 | 4 | from sklearn import linear_model 5 | import numpy as np 6 | import typing 7 | 8 | 9 | @scope.define 10 | def sklearn_OrthogonalMatchingPursuit(*args, **kwargs): 11 | return linear_model.OrthogonalMatchingPursuit(*args, **kwargs) 12 | 13 | 14 | @scope.define 15 | def sklearn_OrthogonalMatchingPursuitCV(*args, **kwargs): 16 | return linear_model.OrthogonalMatchingPursuitCV(*args, **kwargs) 17 | 18 | 19 | def orthogonal_matching_pursuit(name: str, 20 | n_nonzero_coefs: int = None, 21 | tol: typing.Union[float, Apply] = None, 22 | fit_intercept: bool = True, 23 | precompute: typing.Union[str, bool] = "auto", 24 | **kwargs): 25 | """ 26 | Return a pyll graph with hyperparameters that will construct 27 | a sklearn.linear_model.OrthogonalMatchingPursuit model. 28 | 29 | Args: 30 | name: name | str 31 | n_nonzero_coefs: target number non-zero coefficients | int 32 | tol: maximum norm of residual | float 33 | fit_intercept: whether to calculate intercept for model | bool 34 | precompute: whether to use precomputed Gram and Xy matrix | str, bool 35 | """ 36 | 37 | def _name(msg): 38 | return f"{name}.orthogonal_matching_pursuit_{msg}" 39 | 40 | hp_space = dict( 41 | n_nonzero_coefs=n_nonzero_coefs, 42 | tol=hp.loguniform(_name("tol"), np.log(1e-5), np.log(1e-2)) if tol is None else tol, 43 | fit_intercept=fit_intercept, 44 | precompute=precompute, 45 | **kwargs 46 | ) 47 | 48 | return scope.sklearn_OrthogonalMatchingPursuit(**hp_space) 49 | 50 | 51 | def orthogonal_matching_pursuit_cv(name: str, 52 | copy: bool = True, 53 | fit_intercept: bool = True, 54 | max_iter: typing.Union[int, Apply] = None, 55 | cv: typing.Union[int, callable, typing.Generator, Apply] = None, 56 | n_jobs: int = 1, 57 | verbose: typing.Union[bool, int] = False, 58 | **kwargs): 59 | """ 60 | Return a pyll graph with hyperparameters that will construct 61 | a sklearn.linear_model.OrthogonalMatchingPursuitCV model. 62 | 63 | Args: 64 | name: name | str 65 | copy: whether design matrix must be copied | bool 66 | fit_intercept: whether to calculate intercept for model | bool 67 | max_iter: maximum number of iterations | int 68 | cv: cross-validation splitting strategy| int, callable or generator 69 | n_jobs: number of CPUs during cv | int 70 | verbose: verbosity amount | bool, int 71 | """ 72 | 73 | def _name(msg): 74 | return f"{name}.orthogonal_matching_pursuit_cv_{msg}" 75 | 76 | hp_space = dict( 77 | copy=copy, 78 | fit_intercept=fit_intercept, 79 | max_iter=max_iter, 80 | cv=hp.pchoice(_name("cv"), [(0.0625, 3), (0.175, 4), (0.525, 5), (0.175, 6), (0.0625, 7)]) 81 | if cv is None else cv, 82 | n_jobs=n_jobs, 83 | verbose=verbose, 84 | **kwargs 85 | ) 86 | 87 | return scope.sklearn_OrthogonalMatchingPursuitCV(**hp_space) 88 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_passive_aggressive.py: -------------------------------------------------------------------------------- 1 | from hyperopt.pyll import scope, Apply 2 | from hyperopt import hp 3 | 4 | from sklearn import linear_model 5 | import numpy as np 6 | import typing 7 | 8 | 9 | @scope.define 10 | def sklearn_PassiveAggressiveClassifier(*args, **kwargs): 11 | return linear_model.PassiveAggressiveClassifier(*args, **kwargs) 12 | 13 | 14 | @scope.define 15 | def sklearn_PassiveAggressiveRegressor(*args, **kwargs): 16 | return linear_model.PassiveAggressiveRegressor(*args, **kwargs) 17 | 18 | 19 | def _passive_aggressive_C(name: str): 20 | """ 21 | Declaration search space 'C' parameter 22 | """ 23 | return hp.uniform(name, 0.1, 2) 24 | 25 | 26 | def _passive_aggressive_max_iter(name: str): 27 | """ 28 | Declaration search space 'max_iter' parameter 29 | """ 30 | return scope.int(hp.uniform(name, low=750, high=1250)) 31 | 32 | 33 | def _passive_aggressive_tol(name: str): 34 | """ 35 | Declaration search space 'tol' parameter 36 | """ 37 | return hp.loguniform(name, np.log(1e-5), np.log(1e-2)) 38 | 39 | 40 | def _passive_aggressive_n_iter_no_change(name: str): 41 | """ 42 | Declaration search space 'n_iter_no_change' parameter 43 | """ 44 | return hp.pchoice(name, [ 45 | (0.25, 4), 46 | (0.50, 5), 47 | (0.25, 6) 48 | ]) 49 | 50 | 51 | def _passive_aggressive_random_state(name: str): 52 | """ 53 | Declaration search space 'random_state' parameter 54 | """ 55 | return hp.randint(name, 5) 56 | 57 | 58 | def _passive_aggressive_hp_space( 59 | name_func, 60 | C: typing.Union[float, Apply] = None, 61 | fit_intercept: bool = True, 62 | max_iter: typing.Union[int, Apply] = None, 63 | tol: typing.Union[float, Apply] = None, 64 | early_stopping: bool = False, 65 | validation_fraction: typing.Union[float, Apply] = None, 66 | n_iter_no_change: typing.Union[int, Apply] = None, 67 | shuffle: bool = True, 68 | verbose: int = 0, 69 | random_state=None, 70 | warm_start: bool = False, 71 | average: typing.Union[bool, int] = False, 72 | **kwargs 73 | ): 74 | """ 75 | Hyper parameter search space for 76 | passive aggressive classifier 77 | passive aggressive regressor 78 | """ 79 | hp_space = dict( 80 | C=_passive_aggressive_C(name_func("C")) if C is None else C, 81 | fit_intercept=fit_intercept, 82 | max_iter=_passive_aggressive_max_iter(name_func("max_iter")) if max_iter is None else max_iter, 83 | tol=_passive_aggressive_tol(name_func("tol")) if tol is None else tol, 84 | early_stopping=early_stopping, 85 | validation_fraction=0.1 if validation_fraction is None else validation_fraction, 86 | n_iter_no_change=_passive_aggressive_n_iter_no_change(name_func("n_iter_no_change")) 87 | if n_iter_no_change is None else n_iter_no_change, 88 | shuffle=shuffle, 89 | verbose=verbose, 90 | random_state=_passive_aggressive_random_state(name_func("random_state")) 91 | if random_state is None else random_state, 92 | warm_start=warm_start, 93 | average=average, 94 | **kwargs 95 | ) 96 | return hp_space 97 | 98 | 99 | def passive_aggressive_classifier(name: str, 100 | loss: typing.Union[str, Apply] = None, 101 | n_jobs: int = 1, 102 | class_weight: typing.Union[dict, str] = None, 103 | **kwargs): 104 | """ 105 | Return a pyll graph with hyperparameters that will construct 106 | a sklearn.linear_model.PassiveAggressiveClassifier model. 107 | 108 | Args: 109 | name: name | str 110 | loss: loss function to use | str 111 | n_jobs: number of CPUs to use | int 112 | class_weight: class_weight fit parameters | dict or str 113 | 114 | See help(hpsklearn.components.linear_model._passive_aggressive._passive_aggressive_hp_space) 115 | for info on additional available passive aggressive arguments. 116 | """ 117 | 118 | def _name(msg): 119 | return f"{name}.passive_aggressive_classifier{msg}" 120 | 121 | hp_space = _passive_aggressive_hp_space(_name, **kwargs) 122 | hp_space["loss"] = hp.choice(_name("loss"), ["hinge", "squared_hinge"]) if loss is None else loss 123 | hp_space["n_jobs"] = n_jobs 124 | hp_space["class_weight"] = class_weight 125 | 126 | return scope.sklearn_PassiveAggressiveClassifier(**hp_space) 127 | 128 | 129 | def passive_aggressive_regressor(name: str, 130 | loss: typing.Union[str, Apply] = None, 131 | epsilon: typing.Union[float, Apply] = None, 132 | **kwargs): 133 | """ 134 | Return a pyll graph with hyperparameters that will construct 135 | a sklearn.linear_model.PassiveAggressiveRegressor model. 136 | 137 | Args: 138 | name: name | str 139 | loss: loss function to use | str 140 | epsilon: threshold prediction and correct | float 141 | 142 | See help(hpsklearn.components.linear_model._passive_aggressive._passive_aggressive_hp_space) 143 | for info on additional available passive aggressive arguments. 144 | """ 145 | 146 | def _name(msg): 147 | return f"{name}.passive_aggressive_classifier{msg}" 148 | 149 | hp_space = _passive_aggressive_hp_space(_name, **kwargs) 150 | hp_space["loss"] = hp.choice(_name("loss"), ["epsilon_insensitive", "squared_epsilon_insensitive"]) \ 151 | if loss is None else loss 152 | hp_space["epsilon"] = hp.uniform(_name("epsilon"), 0.05, 0.2) if epsilon is None else epsilon 153 | 154 | return scope.sklearn_PassiveAggressiveRegressor(**hp_space) 155 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_perceptron.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import linear_model 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_Perceptron(*args, **kwargs): 13 | return linear_model.Perceptron(*args, **kwargs) 14 | 15 | 16 | @validate(params=["penalty"], 17 | validation_test=lambda param: not isinstance(param, str) or param in ["l1", "l2", "elasticnet"], 18 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['l1', 'l2', 'elasticnet'].") 19 | def perceptron(name: str, 20 | penalty: typing.Union[str, Apply] = None, 21 | alpha: typing.Union[float, Apply] = None, 22 | l1_ratio: typing.Union[float, Apply] = None, 23 | fit_intercept: typing.Union[bool, Apply] = True, 24 | max_iter: typing.Union[int, Apply] = None, 25 | tol: typing.Union[float, Apply] = None, 26 | shuffle: bool = True, 27 | verbose: int = 0, 28 | eta0: typing.Union[float, Apply] = None, 29 | n_jobs: int = 1, 30 | random_state=None, 31 | early_stopping: bool = False, 32 | validation_fraction: typing.Union[float, Apply] = None, 33 | n_iter_no_change: typing.Union[int, Apply] = 5, 34 | class_weight: typing.Union[dict, str] = None, 35 | warm_start: bool = False, 36 | **kwargs): 37 | """ 38 | Return a pyll graph with hyperparameters that will construct 39 | a sklearn.linear_model.Perceptron model. 40 | 41 | Args: 42 | name: name | str 43 | penalty: penalty or regularization term to use | str 44 | alpha: constant to multiply regularization term | float 45 | l1_ratio: elastic net mixing parameter | float 46 | fit_intercept: whether to estimate intercept | bool 47 | max_iter: maximum epochs or iterations | int 48 | tol: stopping criterion | float 49 | shuffle: shuffle training data after each epoch | bool 50 | verbose: verbosity level | int 51 | eta0: constant by which updates are multiplied | float 52 | n_jobs: number of CPUs to use | int 53 | random_state: used to shuffle training data | int 54 | early_stopping: whether to use early stopping | bool 55 | validation_fraction: validation set for early stopping | float 56 | n_iter_no_change: iterations with no improvement | int 57 | class_weight: class_weight fit parameters | dict or str 58 | warm_start: reuse previous call as fit | bool 59 | """ 60 | 61 | def _name(msg): 62 | return f"{name}.perceptron_{msg}" 63 | 64 | hp_space = dict( 65 | penalty=hp.choice(_name("penalty"), ["l1", "l2", "elasticnet"]) if penalty is None else penalty, 66 | alpha=hp.loguniform(_name("alpha"), np.log(1e-6), np.log(1e-1)) if alpha is None else alpha, 67 | l1_ratio=hp.loguniform(_name("l1_ratio"), np.log(1e-7), np.log(1)) if l1_ratio is None else l1_ratio, 68 | fit_intercept=hp.choice(_name("fit_intercept"), [True, False]) if fit_intercept is None else fit_intercept, 69 | max_iter=scope.int(hp.uniform(_name("max_iter"), 750, 1250)) if max_iter is None else max_iter, 70 | tol=hp.loguniform(_name("tol"), np.log(1e-5), np.log(1e-2)) if tol is None else tol, 71 | shuffle=shuffle, 72 | verbose=verbose, 73 | eta0=hp.normal(_name("eta0"), mu=1.0, sigma=0.1) if eta0 is None else eta0, 74 | n_jobs=n_jobs, 75 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 76 | early_stopping=early_stopping, 77 | validation_fraction=0.1 if validation_fraction is None else validation_fraction, 78 | n_iter_no_change=hp.pchoice(_name("n_iter_no_change"), [(0.25, 4), (0.50, 5), (0.25, 6)]) 79 | if n_iter_no_change is None else n_iter_no_change, 80 | class_weight=class_weight, 81 | warm_start=warm_start, 82 | **kwargs 83 | ) 84 | 85 | return scope.sklearn_Perceptron(**hp_space) 86 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_quantile.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import linear_model 7 | import typing 8 | 9 | 10 | @scope.define 11 | def sklearn_QuantileRegressor(*args, **kwargs): 12 | return linear_model.QuantileRegressor(*args, **kwargs) 13 | 14 | 15 | @validate(params=["solver"], 16 | validation_test=lambda param: not isinstance(param, str) or 17 | param in ["highs-ds", "highs-ipm", "highs", "revised simplex"], 18 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['highs-ds', 'highs-ipm', 'highs', " 19 | "'revised simplex'].") 20 | def quantile_regression(name: str, 21 | quantile: typing.Union[float, Apply] = None, 22 | alpha: typing.Union[float, Apply] = None, 23 | fit_intercept: typing.Union[bool, Apply] = None, 24 | solver: typing.Union[str, Apply] = None, 25 | solver_options: dict = None, 26 | **kwargs): 27 | """ 28 | Return a pyll graph with hyperparameters that will construct 29 | a sklearn.linear_model.QuantileRegression model. 30 | 31 | Args: 32 | name: name | str 33 | quantile: quantile the model tries to predict | float 34 | alpha: constant to multiply regularization term | float 35 | fit_intercept: whether to estimate intercept | bool 36 | solver: solver used for linear programming formulation | str 37 | solver_options: additional parameters for solver | dict 38 | """ 39 | 40 | def _name(msg): 41 | return f"{name}.quantile_regression_{msg}" 42 | 43 | hp_space = dict( 44 | quantile=hp.normal(_name("quantile"), 0.5, 0.075) if quantile is None else quantile, 45 | alpha=hp.normal(_name("alpha"), mu=1.0, sigma=0.1) if alpha is None else alpha, 46 | fit_intercept=hp.choice(_name("fit_intercept"), [True, False]) if fit_intercept is None else fit_intercept, 47 | solver=hp.choice(_name("solver"), ["highs-ds", "highs-ipm", "highs", "revised simplex"]) 48 | if solver is None else solver, 49 | solver_options=solver_options, 50 | **kwargs 51 | ) 52 | 53 | return scope.sklearn_QuantileRegressor(**hp_space) 54 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_ransac.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import linear_model 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_RANSACRegressor(*args, **kwargs): 13 | return linear_model.RANSACRegressor(*args, **kwargs) 14 | 15 | 16 | @validate(params=["loss"], 17 | validation_test=lambda param: not isinstance(param, str) or param in ["absolute_error", "squared_error"], 18 | msg="Invalid parameter '%s' with value '%s'. Value must be in ['absolute_error', 'squared_error'].") 19 | def ransac_regression(name: str, 20 | estimator=None, 21 | min_samples: float = None, 22 | residual_threshold: float = None, 23 | is_data_valid: callable = None, 24 | is_model_valid: callable = None, 25 | max_trials: typing.Union[int, Apply] = None, 26 | max_skips: typing.Union[int, Apply] = None, 27 | stop_n_inliers: typing.Union[int, Apply] = None, 28 | stop_score: typing.Union[float, Apply] = None, 29 | stop_probability: typing.Union[float, Apply] = None, 30 | loss: typing.Union[callable, str, Apply] = None, 31 | random_state=None, 32 | **kwargs): 33 | """ 34 | Return a pyll graph with hyperparameters that will construct 35 | a sklearn.linear_model.RANSACRegressor model. 36 | 37 | Args: 38 | name: name | str 39 | estimator: base estimator object 40 | min_samples: minimum number of samples chosen | float 41 | residual_threshold: maximum residual | float 42 | is_data_valid: function called before model is fitted | callable 43 | is_model_valid: function called with estimated model | callable 44 | max_trials: maximum number of iterations sample selection | int 45 | max_skips: maximum skipped iterations due to finding zero inliers | int 46 | stop_n_inliers: stop iteration if this number of inliers found | int 47 | stop_score: stop iteration if score is greater equal to value | float 48 | stop_probability: confidence param of N-samples RANSAC | float 49 | loss: loss function to use | str 50 | random_state: random seed | int 51 | """ 52 | 53 | def _name(msg): 54 | return f"{name}.ransac_regression_{msg}" 55 | 56 | hp_space = dict( 57 | estimator=estimator, 58 | min_samples=min_samples, # default None fits linear model with X.shape[1] + 1 59 | residual_threshold=residual_threshold, 60 | is_data_valid=is_data_valid, 61 | is_model_valid=is_model_valid, 62 | max_trials=scope.int(hp.uniform(_name("max_trials"), 50, 150)) if max_trials is None else max_trials, 63 | max_skips=np.inf if max_skips is None else max_skips, 64 | stop_n_inliers=np.inf if stop_n_inliers is None else stop_n_inliers, 65 | stop_score=np.inf if stop_score is None else stop_score, 66 | stop_probability=hp.uniform(_name("stop_probability"), 0.90, 0.99) 67 | if stop_probability is None else stop_probability, 68 | loss=hp.choice(_name("loss"), ["absolute_error", "squared_error"]) if loss is None else loss, 69 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 70 | **kwargs 71 | ) 72 | 73 | return scope.sklearn_RANSACRegressor(**hp_space) 74 | -------------------------------------------------------------------------------- /hpsklearn/components/linear_model/_theil_sen.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import linear_model 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_TheilSenRegressor(*args, **kwargs): 13 | return linear_model.TheilSenRegressor(*args, **kwargs) 14 | 15 | 16 | @validate(params=["max_iter"], 17 | validation_test=lambda param: not isinstance(param, int) or param > 0, 18 | msg="Invalid parameter '%s' with value '%s'. Value must be a positive integer.") 19 | def theil_sen_regressor( 20 | name: str, 21 | fit_intercept: bool = True, 22 | max_subpopulation: typing.Union[int, Apply] = None, 23 | n_subsamples: int = None, 24 | max_iter: typing.Union[int, Apply] = None, 25 | tol: typing.Union[float, Apply] = None, 26 | random_state=None, 27 | n_jobs: int = 1, 28 | verbose: bool = False, 29 | **kwargs): 30 | """ 31 | Return a pyll graph with hyperparameters that will construct 32 | a sklearn.linear_model.TheilSenRegressor model. 33 | 34 | Args: 35 | name: name | str 36 | fit_intercept: whether to calculate the intercept | bool 37 | max_subpopulation: consider stochastic subpopulation | int 38 | n_subsamples: number of samples to calculate parameters | int 39 | max_iter: maximum number of iterations | int 40 | tol: tolerance when calculating spatial median | float 41 | random_state: random state | int 42 | n_jobs: number of CPUs to use | int 43 | verbose: verbosity level | bool 44 | """ 45 | 46 | def _name(msg): 47 | return f"{name}.theil_sen_regressor_{msg}" 48 | 49 | hp_space = dict( 50 | fit_intercept=fit_intercept, 51 | max_subpopulation=scope.int(hp.uniform(_name("max_subpopulation"), 7500, 12500)) 52 | if max_subpopulation is None else max_subpopulation, 53 | n_subsamples=n_subsamples, 54 | max_iter=scope.int(hp.uniform(_name("max_iter"), 200, 400)) if max_iter is None else max_iter, 55 | tol=hp.loguniform(_name("tol"), np.log(1e-5), np.log(1e-2)) if tol is None else tol, 56 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 57 | n_jobs=n_jobs, 58 | verbose=verbose, 59 | **kwargs 60 | ) 61 | return scope.sklearn_TheilSenRegressor(**hp_space) 62 | -------------------------------------------------------------------------------- /hpsklearn/components/mixture/__init__.py: -------------------------------------------------------------------------------- 1 | from ._bayesian_mixture import bayesian_gaussian_mixture 2 | from ._gaussian_mixture import gaussian_mixture 3 | -------------------------------------------------------------------------------- /hpsklearn/components/mixture/_bayesian_mixture.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import mixture 7 | import numpy.typing as npt 8 | import numpy as np 9 | import typing 10 | 11 | 12 | @scope.define 13 | def sklearn_BayesianGaussianMixture(*args, **kwargs): 14 | return mixture.BayesianGaussianMixture(*args, **kwargs) 15 | 16 | 17 | @validate(params=["covariance_type"], 18 | validation_test=lambda param: not isinstance(param, str) or param in ["full", "tied", "diag", "spherical"], 19 | msg="Invalid parameter '%s' with value '%s'. Value must be one of 'full', 'tied', 'diag', or 'spherical'") 20 | @validate(params=["init_params"], 21 | validation_test=lambda param: not isinstance(param, str) or param in ["kmeans", "random"], 22 | msg="Invalid parameter '%s' with value '%s'. Value must be one of 'kmeans' or 'random'") 23 | @validate(params=["weight_concentration_prior_type"], 24 | validation_test=lambda param: not isinstance(param, str) or param in ["dirichlet_process", 25 | "dirichlet_distribution"], 26 | msg="Invalid parameter '%s' with value '%s'. Value must be 'dirichlet_process' or 'dirichlet_distribution'") 27 | def bayesian_gaussian_mixture(name: str, 28 | n_components: typing.Union[int, Apply] = None, 29 | covariance_type: typing.Union[str, Apply] = None, 30 | tol: typing.Union[float, Apply] = None, 31 | reg_covar: typing.Union[float, Apply] = None, 32 | max_iter: typing.Union[int, Apply] = None, 33 | n_init: typing.Union[int, Apply] = None, 34 | init_params: typing.Union[str, Apply] = None, 35 | weight_concentration_prior_type: typing.Union[str, Apply] = None, 36 | weight_concentration_prior: float = None, 37 | mean_precision_prior: float = None, 38 | mean_prior: npt.ArrayLike = None, 39 | degrees_of_freedom_prior: float = None, 40 | covariance_prior: typing.Union[npt.ArrayLike, float] = None, 41 | random_state=None, 42 | warm_start: bool = False, 43 | verbose: int = 0, 44 | verbose_interval: int = 10, 45 | **kwargs): 46 | """ 47 | Return a pyll graph with hyperparameters that will construct 48 | a sklearn.mixture.BayesianGaussianMixture model. 49 | 50 | Args: 51 | name: name | str 52 | n_components: number of mixture components | int 53 | covariance_type: type of the covariance parameters | str 54 | tol: convergence threshold (stopping criterion) | float 55 | reg_covar: regularization added to the diagonal of covariance | float 56 | max_iter: maximum number of EM iterations | int 57 | n_init: number of initializations | int 58 | init_params: weights, means and covariance init method | str 59 | weight_concentration_prior_type: prior type for weight concentration | str 60 | weight_concentration_prior: dirichlet concentration | float 61 | mean_precision_prior: precision prior of mean distribution | float 62 | mean_prior: prior on the mean distribution | npt.ArrayLike 63 | degrees_of_freedom_prior: prior of degrees of freedom | float 64 | covariance_prior: prior on the covariance distribution | npt.ArrayLike 65 | random_state: random seed | int 66 | warm_start: reuse previous initialization if available | bool 67 | verbose: verbosity level | int 68 | verbose_interval: interval between log messages | int 69 | """ 70 | 71 | def _name(msg): 72 | return f"{name}.bayesian_gaussian_mixture_{msg}" 73 | 74 | hp_space = dict( 75 | n_components=scope.int(hp.uniform(_name("n_components"), 1, 5)) if n_components is None else n_components, 76 | covariance_type=hp.choice(_name("covariance_type"), ["full", "tied", "diag", "spherical"]) 77 | if covariance_type is None else covariance_type, 78 | tol=hp.loguniform(_name("tol"), np.log(1e-5), np.log(1e-2)) if tol is None else tol, 79 | reg_covar=hp.loguniform(_name("reg_covar"), np.log(1e-7), np.log(1e-5)) if reg_covar is None else reg_covar, 80 | max_iter=scope.int(hp.uniform(_name("max_iter"), 100, 300)) if max_iter is None else max_iter, 81 | n_init=hp.choice(_name("n_init"), [1, 2]) if n_init is None else n_init, 82 | init_params=hp.choice(_name("init_params"), ["kmeans", "random"]) if init_params is None else init_params, 83 | weight_concentration_prior_type=hp.choice(_name("weight_concentration_prior_type"), ["dirichlet_process", 84 | "dirichlet_distribution"]) 85 | if weight_concentration_prior_type is None else weight_concentration_prior_type, 86 | weight_concentration_prior=weight_concentration_prior, 87 | mean_precision_prior=mean_precision_prior, 88 | mean_prior=mean_prior, 89 | degrees_of_freedom_prior=degrees_of_freedom_prior, 90 | covariance_prior=covariance_prior, 91 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 92 | warm_start=warm_start, 93 | verbose=verbose, 94 | verbose_interval=verbose_interval, 95 | **kwargs, 96 | ) 97 | return scope.sklearn_BayesianGaussianMixture(**hp_space) 98 | -------------------------------------------------------------------------------- /hpsklearn/components/mixture/_gaussian_mixture.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import mixture 7 | import numpy.typing as npt 8 | import numpy as np 9 | import typing 10 | 11 | 12 | @scope.define 13 | def sklearn_GaussianMixture(*args, **kwargs): 14 | return mixture.GaussianMixture(*args, **kwargs) 15 | 16 | 17 | @validate(params=["covariance_type"], 18 | validation_test=lambda param: not isinstance(param, str) or param in ["full", "tied", "diag", "spherical"], 19 | msg="Invalid parameter '%s' with value '%s'. Value must be one of 'full', 'tied', 'diag', or 'spherical'") 20 | @validate(params=["init_params"], 21 | validation_test=lambda param: not isinstance(param, str) or param in ["kmeans", "random"], 22 | msg="Invalid parameter '%s' with value '%s'. Value must be one of 'kmeans' or 'random'") 23 | def gaussian_mixture(name: str, 24 | n_components: typing.Union[int, Apply] = None, 25 | covariance_type: typing.Union[str, Apply] = None, 26 | tol: typing.Union[float, Apply] = None, 27 | reg_covar: typing.Union[float, Apply] = None, 28 | max_iter: typing.Union[int, Apply] = None, 29 | n_init: typing.Union[int, Apply] = None, 30 | init_params: typing.Union[str, Apply] = None, 31 | weights_init: npt.ArrayLike = None, 32 | means_init: npt.ArrayLike = None, 33 | precisions_init: npt.ArrayLike = None, 34 | random_state=None, 35 | warm_start: bool = False, 36 | verbose: int = 0, 37 | verbose_interval: int = 10, 38 | **kwargs): 39 | """ 40 | Return a pyll graph with hyperparameters that will construct 41 | a sklearn.mixture.GaussianMixture model. 42 | 43 | Args: 44 | name: name | str 45 | n_components: number of mixture components | int 46 | covariance_type: type of the covariance parameters | str 47 | tol: convergence threshold (stopping criterion) | float 48 | reg_covar: regularization added to the diagonal of covariance | float 49 | max_iter: maximum number of EM iterations | int 50 | n_init: number of initializations | int 51 | init_params: weights, means and covariance init method | str 52 | weights_init: initial weights | npt.ArrayLike 53 | means_init: initial means | npt.ArrayLike 54 | precisions_init: initial precisions | npt.ArrayLike 55 | random_state: random seed | int 56 | warm_start: reuse previous initialization if available | bool 57 | verbose: verbosity level | int 58 | verbose_interval: interval between log messages | int 59 | """ 60 | 61 | def _name(msg): 62 | return f"{name}.gaussian_mixture_{msg}" 63 | 64 | hp_space = dict( 65 | n_components=scope.int(hp.uniform(_name("n_components"), 1, 5)) if n_components is None else n_components, 66 | covariance_type=hp.choice(_name("covariance_type"), ["full", "tied", "diag", "spherical"]) 67 | if covariance_type is None else covariance_type, 68 | tol=hp.loguniform(_name("tol"), np.log(1e-5), np.log(1e-2)) if tol is None else tol, 69 | reg_covar=hp.loguniform(_name("reg_covar"), np.log(1e-7), np.log(1e-5)) if reg_covar is None else reg_covar, 70 | max_iter=scope.int(hp.uniform(_name("max_iter"), 100, 300)) if max_iter is None else max_iter, 71 | n_init=hp.choice(_name("n_init"), [1, 2]) if n_init is None else n_init, 72 | init_params=hp.choice(_name("init_params"), ["kmeans", "random"]) if init_params is None else init_params, 73 | weights_init=weights_init, 74 | means_init=means_init, 75 | precisions_init=precisions_init, 76 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 77 | warm_start=warm_start, 78 | verbose=verbose, 79 | verbose_interval=verbose_interval, 80 | **kwargs, 81 | ) 82 | return scope.sklearn_GaussianMixture(**hp_space) 83 | -------------------------------------------------------------------------------- /hpsklearn/components/multiclass.py: -------------------------------------------------------------------------------- 1 | from hyperopt.pyll import scope, Apply 2 | from hyperopt import hp 3 | 4 | from sklearn import multiclass 5 | from . import any_classifier 6 | import typing 7 | 8 | 9 | @scope.define 10 | def sklearn_OneVsRestClassifier(*args, **kwargs): 11 | return multiclass.OneVsRestClassifier(*args, **kwargs) 12 | 13 | 14 | @scope.define 15 | def sklearn_OneVsOneClassifier(*args, **kwargs): 16 | return multiclass.OneVsOneClassifier(*args, **kwargs) 17 | 18 | 19 | @scope.define 20 | def sklearn_OutputCodeClassifier(*args, **kwargs): 21 | return multiclass.OutputCodeClassifier(*args, **kwargs) 22 | 23 | 24 | def one_vs_rest_classifier(name: str, 25 | estimator: typing.Union[object, Apply] = None, 26 | n_jobs: int = 1, 27 | **kwargs): 28 | """ 29 | Return a pyll graph with hyperparameters that will construct 30 | a sklearn.multiclass.OneVsRestClassifier model. 31 | 32 | Args: 33 | name: name | str 34 | estimator: estimator object | object 35 | n_jobs: number of CPUs to use | int 36 | """ 37 | 38 | def _name(msg): 39 | return f"{name}.one_vs_rest_{msg}" 40 | 41 | hp_space = dict( 42 | estimator=any_classifier(_name("estimator")) if estimator is None else estimator, 43 | n_jobs=n_jobs, 44 | **kwargs 45 | ) 46 | return scope.sklearn_OneVsRestClassifier(**hp_space) 47 | 48 | 49 | def one_vs_one_classifier(name: str, 50 | estimator: typing.Union[object, Apply] = None, 51 | n_jobs: int = 1, 52 | **kwargs): 53 | """ 54 | Return a pyll graph with hyperparameters that will construct 55 | a sklearn.multiclass.OneVsOneClassifier model. 56 | 57 | Args: 58 | name: name | str 59 | estimator: estimator object | object 60 | n_jobs: number of CPUs to use | int 61 | """ 62 | 63 | def _name(msg): 64 | return f"{name}.one_vs_one_{msg}" 65 | 66 | hp_space = dict( 67 | estimator=any_classifier(_name("estimator")) if estimator is None else estimator, 68 | n_jobs=n_jobs, 69 | **kwargs 70 | ) 71 | return scope.sklearn_OneVsOneClassifier(**hp_space) 72 | 73 | 74 | def output_code_classifier(name: str, 75 | estimator: typing.Union[object, Apply] = None, 76 | code_size: typing.Union[float, Apply] = None, 77 | random_state=None, 78 | n_jobs: int = 1, 79 | **kwargs): 80 | """ 81 | Return a pyll graph with hyperparameters that will construct 82 | a sklearn.multiclass.OutputCodeClassifier model. 83 | 84 | Args: 85 | name: name | str 86 | estimator: estimator object | object 87 | code_size: percentage number of classes | float 88 | random_state: random state | int 89 | n_jobs: number of CPUs to use | int 90 | """ 91 | 92 | def _name(msg): 93 | return f"{name}.output_code_classifier{msg}" 94 | 95 | hp_space = dict( 96 | estimator=any_classifier(_name("estimator")) if estimator is None else estimator, 97 | code_size=hp.uniform(_name("code_size"), 1, 2) if code_size is None else code_size, 98 | n_jobs=n_jobs, 99 | random_state=hp.randint(_name("random_state"), 5) if random_state is None else random_state, 100 | **kwargs 101 | ) 102 | return scope.sklearn_OutputCodeClassifier(**hp_space) 103 | -------------------------------------------------------------------------------- /hpsklearn/components/naive_bayes.py: -------------------------------------------------------------------------------- 1 | from hyperopt.pyll import scope, Apply 2 | from hyperopt import hp 3 | 4 | from sklearn import naive_bayes 5 | 6 | import numpy.typing as npt 7 | import typing 8 | 9 | 10 | @scope.define 11 | def sklearn_BernoulliNB(*args, **kwargs): 12 | return naive_bayes.BernoulliNB(*args, **kwargs) 13 | 14 | 15 | @scope.define 16 | def sklearn_CategoricalNB(*args, **kwargs): 17 | return naive_bayes.CategoricalNB(*args, **kwargs) 18 | 19 | 20 | @scope.define 21 | def sklearn_ComplementNB(*args, **kwargs): 22 | return naive_bayes.ComplementNB(*args, **kwargs) 23 | 24 | 25 | @scope.define 26 | def sklearn_GaussianNB(*args, **kwargs): 27 | return naive_bayes.GaussianNB(*args, **kwargs) 28 | 29 | 30 | @scope.define 31 | def sklearn_MultinomialNB(*args, **kwargs): 32 | return naive_bayes.MultinomialNB(*args, **kwargs) 33 | 34 | 35 | def _nb_hp_space( 36 | name_func, 37 | alpha: typing.Union[float, Apply] = None, 38 | fit_prior: typing.Union[bool, Apply] = None, 39 | class_prior: npt.ArrayLike = None, 40 | **kwargs 41 | ): 42 | """ 43 | Hyper parameter search space for 44 | bernoulli nb 45 | categorical nb 46 | complement nb 47 | multinomial nb 48 | """ 49 | hp_space = dict( 50 | alpha=hp.quniform(name_func("alpha"), 0, 1, 0.001) if alpha is None else alpha, 51 | fit_prior=hp.choice(name_func("fit_prior"), [True, False]) if fit_prior is None else fit_prior, 52 | class_prior=class_prior, 53 | **kwargs 54 | ) 55 | return hp_space 56 | 57 | 58 | def bernoulli_nb(name: str, binarize: typing.Union[float, None] = 0.0, **kwargs): 59 | """ 60 | Return a pyll graph with hyperparameters that will construct 61 | a sklearn.naive_bayes.BernoulliNB model. 62 | 63 | Args: 64 | name: name | str 65 | binarize: threshold for binarizing | float, None 66 | 67 | See help(hpsklearn.components.naive_bayes._nb_hp_space) 68 | for info on additional available naive bayes arguments. 69 | """ 70 | 71 | def _name(msg): 72 | return f"{name}.bernoulli_nb_{msg}" 73 | 74 | hp_space = _nb_hp_space(_name, **kwargs) 75 | hp_space["binarize"] = binarize 76 | 77 | return scope.sklearn_BernoulliNB(**hp_space) 78 | 79 | 80 | def categorical_nb(name: str, min_categories: typing.Union[int, npt.ArrayLike] = None, **kwargs): 81 | """ 82 | Return a pyll graph with hyperparameters that will construct 83 | a sklearn.naive_bayes.CategoricalNB model. 84 | 85 | Args: 86 | name: name | str 87 | min_categories: minimum number of categories per feature | int, ArrayLike 88 | 89 | See help(hpsklearn.components.naive_bayes._nb_hp_space) 90 | for info on additional available naive bayes arguments. 91 | """ 92 | 93 | def _name(msg): 94 | return f"{name}.categorical_nb_{msg}" 95 | 96 | hp_space = _nb_hp_space(_name, **kwargs) 97 | hp_space["min_categories"] = min_categories 98 | 99 | return scope.sklearn_CategoricalNB(**hp_space) 100 | 101 | 102 | def complement_nb(name: str, norm: typing.Union[bool, Apply] = None, **kwargs): 103 | """ 104 | Return a pyll graph with hyperparameters that will construct 105 | a sklearn.naive_bayes.ComplementNB model. 106 | 107 | Args: 108 | name: name | str 109 | norm: whether a second normalization of weights is performed | bool 110 | 111 | See help(hpsklearn.components.naive_bayes._nb_hp_space) 112 | for info on additional available naive bayes arguments. 113 | """ 114 | 115 | def _name(msg): 116 | return f"{name}.complement_nb_{msg}" 117 | 118 | hp_space = _nb_hp_space(_name, **kwargs) 119 | hp_space["norm"] = hp.choice(_name("norm"), [True, False]) if norm is None else norm 120 | 121 | return scope.sklearn_ComplementNB(**hp_space) 122 | 123 | 124 | def gaussian_nb(name: str, var_smoothing: float = 1e-9, **kwargs): 125 | """ 126 | Return a pyll graph with hyperparameters that will construct 127 | a sklearn.naive_bayes.GaussianNB model. 128 | 129 | Args: 130 | name: name | str 131 | var_smoothing: portion of largest variance | float 132 | """ 133 | 134 | def _name(msg): 135 | return f"{name}.gaussian_nb_{msg}" 136 | 137 | hp_space = dict( 138 | var_smoothing=var_smoothing, 139 | **kwargs 140 | ) 141 | return scope.sklearn_GaussianNB(**hp_space) 142 | 143 | 144 | def multinomial_nb(name: str, **kwargs): 145 | """ 146 | Return a pyll graph with hyperparameters that will construct 147 | a sklearn.naive_bayes.MultinomialNB model. 148 | 149 | Args: 150 | name: name | str 151 | 152 | See help(hpsklearn.components.naive_bayes._nb_hp_space) 153 | for info on additional available naive bayes arguments. 154 | """ 155 | 156 | def _name(msg): 157 | return f"{name}.multinomial_nb_{msg}" 158 | 159 | hp_space = _nb_hp_space(_name, **kwargs) 160 | 161 | return scope.sklearn_MultinomialNB(**hp_space) 162 | -------------------------------------------------------------------------------- /hpsklearn/components/neighbors/__init__.py: -------------------------------------------------------------------------------- 1 | from ._classification import \ 2 | k_neighbors_classifier, \ 3 | radius_neighbors_classifier 4 | 5 | from ._nearest_centroid import nearest_centroid 6 | 7 | from ._regression import \ 8 | k_neighbors_regressor, \ 9 | radius_neighbors_regressor 10 | -------------------------------------------------------------------------------- /hpsklearn/components/neighbors/_classification.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from ._regression import neighbors_hp_space 7 | from sklearn import neighbors 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_KNeighborsClassifier(*args, **kwargs): 13 | return neighbors.KNeighborsClassifier(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_RadiusNeighborsClassifier(*args, **kwargs): 18 | return neighbors.RadiusNeighborsClassifier(*args, **kwargs) 19 | 20 | 21 | def k_neighbors_classifier(name: str, 22 | n_neighbors: typing.Union[int, Apply] = None, 23 | **kwargs): 24 | """ 25 | Return a pyll graph with hyperparameters that will construct 26 | a sklearn.neighbors.KNeighborsClassifier model. 27 | 28 | Args: 29 | name: name | str 30 | n_neighbors: number of neighbors | int 31 | 32 | See help(hpsklearn.components.neighbors._regression._neighbors_regression_hp_space) 33 | for info on additional available neighbors arguments. 34 | """ 35 | 36 | def _name(msg): 37 | return f"{name}.k_neighbors_classifier_{msg}" 38 | 39 | hp_space = neighbors_hp_space(_name, **kwargs) 40 | hp_space["n_neighbors"] = scope.int(hp.uniform(_name("n_neighbors"), 1, 15)) if n_neighbors is None else n_neighbors 41 | 42 | return scope.sklearn_KNeighborsClassifier(**hp_space) 43 | 44 | 45 | @validate(params=["outlier_label"], 46 | validation_test=lambda param: not isinstance(param, str) or param == "most_frequent", 47 | msg="Invalid parameter '%s' with value '%s'. Value must be 'most_frequent'.") 48 | def radius_neighbors_classifier(name: str, 49 | radius: typing.Union[float, Apply] = None, 50 | outlier_label: typing.Union[int, str, Apply] = None, 51 | **kwargs): 52 | """ 53 | Return a pyll graph with hyperparameters that will construct 54 | a sklearn.neighbors.RadiusNeighborsClassifier model. 55 | 56 | Args: 57 | name: name | str 58 | radius: range of parameter space | float 59 | outlier_label: label for outlier samples | int | str 60 | 61 | See help(hpsklearn.components.neighbors._regression._neighbors_regression_hp_space) 62 | for info on additional available neighbors arguments. 63 | """ 64 | 65 | def _name(msg): 66 | return f"{name}.radius_neighbors_classifier_{msg}" 67 | 68 | hp_space = neighbors_hp_space(_name, **kwargs) 69 | hp_space["radius"] = hp.uniform(_name("radius"), 25, 100) if radius is None else radius # very dependent on data 70 | hp_space["outlier_label"] = outlier_label 71 | 72 | return scope.sklearn_RadiusNeighborsClassifier(**hp_space) 73 | -------------------------------------------------------------------------------- /hpsklearn/components/neighbors/_nearest_centroid.py: -------------------------------------------------------------------------------- 1 | from hyperopt.pyll import scope, Apply 2 | from hyperopt import hp 3 | 4 | from sklearn import neighbors 5 | import typing 6 | 7 | 8 | @scope.define 9 | def sklearn_NearestCentroid(*args, **kwargs): 10 | return neighbors.NearestCentroid(*args, **kwargs) 11 | 12 | 13 | def nearest_centroid(name: str, 14 | metric: typing.Union[str, Apply] = None, 15 | shrink_threshold: float = None, 16 | **kwargs): 17 | """ 18 | Return a pyll graph with hyperparameters that will construct 19 | a sklearn.neighbors.NearestCentroid model. 20 | 21 | Args: 22 | name: name | str 23 | metric: metric to use | str, callable 24 | shrink_threshold: shrink threshold | float 25 | """ 26 | 27 | def _name(msg): 28 | return f"{name}.nearest_centroid_{msg}" 29 | 30 | hp_space = dict( 31 | metric=hp.choice(_name("metric"), ["euclidean", "manhattan"]) 32 | if metric is None else metric, 33 | shrink_threshold=shrink_threshold, 34 | **kwargs, 35 | ) 36 | return scope.sklearn_NearestCentroid(**hp_space) 37 | -------------------------------------------------------------------------------- /hpsklearn/components/neighbors/_regression.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import neighbors 7 | import typing 8 | 9 | 10 | @scope.define 11 | def sklearn_KNeighborsRegressor(*args, **kwargs): 12 | return neighbors.KNeighborsRegressor(*args, **kwargs) 13 | 14 | 15 | @scope.define 16 | def sklearn_RadiusNeighborsRegressor(*args, **kwargs): 17 | return neighbors.RadiusNeighborsRegressor(*args, **kwargs) 18 | 19 | 20 | def _neighbors_weights(name: str): 21 | """ 22 | Declaration search space 'weights' parameter 23 | """ 24 | return hp.choice(name, ["uniform", "distance"]) 25 | 26 | 27 | def _neighbors_algorithm(name: str): 28 | """ 29 | Declaration search space 'algorithm' parameter 30 | """ 31 | return hp.choice(name, ["auto", "ball_tree", "kd_tree", "brute"]) 32 | 33 | 34 | def _neighbors_leaf_size(name: str): 35 | """ 36 | Declaration search space 'leaf_size' parameter 37 | """ 38 | return scope.int(hp.uniform(name, 20, 40)) 39 | 40 | 41 | def _neighbors_p(name: str): 42 | """ 43 | Declaration search space 'p' parameter 44 | """ 45 | return hp.uniform(name, 1, 5) 46 | 47 | 48 | def _neighbors_metric(name: str): 49 | """ 50 | Declaration search space 'metric' parameter 51 | """ 52 | return hp.choice(name, ["cityblock", "l1", "l2", "minkowski", "euclidean", "manhattan"]) 53 | 54 | 55 | @validate(params=["weights"], 56 | validation_test=lambda param: not isinstance(param, str) or param in ["uniform", "distance"], 57 | msg="Invalid parameter '%s' with value '%s'. Value must be 'uniform' or 'distance'.") 58 | @validate(params=["algorithm"], 59 | validation_test=lambda param: not isinstance(param, str) or param in ["auto", "ball_tree", "kd_tree", 60 | "brute"], 61 | msg="Invalid parameter '%s' with value '%s'. Value must be 'auto', 'ball_tree', 'kd_tree', or 'brute'.") 62 | def neighbors_hp_space( 63 | name_func, 64 | weights: typing.Union[str, callable, Apply] = None, 65 | algorithm: typing.Union[str, Apply] = None, 66 | leaf_size: typing.Union[int, Apply] = None, 67 | p: typing.Union[int, Apply] = None, 68 | metric: typing.Union[str, callable, Apply] = None, 69 | metric_params: dict = None, 70 | n_jobs: int = 1, 71 | **kwargs): 72 | """ 73 | Hyper parameter search space for 74 | k neighbors regressor 75 | radius neighbors regressor 76 | """ 77 | hp_space = dict( 78 | weights=_neighbors_weights(name_func("weights")) if weights is None else weights, 79 | algorithm=_neighbors_algorithm(name_func("algorithm")) if algorithm is None else algorithm, 80 | leaf_size=_neighbors_leaf_size(name_func("leaf_size")) if leaf_size is None else leaf_size, 81 | p=_neighbors_p(name_func("p")) if p is None else p, 82 | metric=_neighbors_metric(name_func("metric")) if metric is None else metric, 83 | metric_params=metric_params, 84 | n_jobs=n_jobs, 85 | **kwargs 86 | ) 87 | return hp_space 88 | 89 | 90 | def k_neighbors_regressor(name: str, 91 | n_neighbors: typing.Union[int, Apply] = None, 92 | **kwargs): 93 | """ 94 | Return a pyll graph with hyperparameters that will construct 95 | a sklearn.neighbors.KNeighborsRegressor model. 96 | 97 | Args: 98 | name: name | str 99 | n_neighbors: number of neighbors | int 100 | 101 | See help(hpsklearn.components.neighbors._regression._neighbors_hp_space) 102 | for info on additional available neighbors regression arguments. 103 | """ 104 | 105 | def _name(msg): 106 | return f"{name}.k_neighbors_regressor_{msg}" 107 | 108 | hp_space = neighbors_hp_space(_name, **kwargs) 109 | hp_space["n_neighbors"] = scope.int(hp.uniform(_name("n_neighbors"), 1, 15)) if n_neighbors is None else n_neighbors 110 | 111 | return scope.sklearn_KNeighborsRegressor(**hp_space) 112 | 113 | 114 | def radius_neighbors_regressor(name: str, 115 | radius: typing.Union[float, Apply] = None, 116 | **kwargs): 117 | """ 118 | Return a pyll graph with hyperparameters that will construct 119 | a sklearn.neighbors.RadiusNeighborsRegressor model. 120 | 121 | Args: 122 | name: name | str 123 | radius: range of parameter space | float 124 | 125 | See help(hpsklearn.components.neighbors._regression._neighbors_hp_space) 126 | for info on additional available neighbors arguments. 127 | """ 128 | 129 | def _name(msg): 130 | return f"{name}.radius_neighbors_regressor_{msg}" 131 | 132 | hp_space = neighbors_hp_space(_name, **kwargs) 133 | hp_space["radius"] = hp.uniform(_name("radius"), 25, 100) if radius is None else radius # very dependent on data 134 | 135 | return scope.sklearn_RadiusNeighborsRegressor(**hp_space) 136 | -------------------------------------------------------------------------------- /hpsklearn/components/neural_network/__init__.py: -------------------------------------------------------------------------------- 1 | from ._multilayer_perceptron import \ 2 | mlp_classifier, \ 3 | mlp_regressor 4 | -------------------------------------------------------------------------------- /hpsklearn/components/preprocessing/__init__.py: -------------------------------------------------------------------------------- 1 | from ._data import \ 2 | binarizer, \ 3 | min_max_scaler, \ 4 | max_abs_scaler, \ 5 | normalizer, \ 6 | robust_scaler, \ 7 | standard_scaler, \ 8 | quantile_transformer, \ 9 | power_transformer 10 | 11 | from ._encoders import \ 12 | one_hot_encoder, \ 13 | ordinal_encoder 14 | 15 | from ._polynomial import \ 16 | polynomial_features, \ 17 | spline_transformer 18 | 19 | from ._discretization import k_bins_discretizer 20 | -------------------------------------------------------------------------------- /hpsklearn/components/preprocessing/_discretization.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import preprocessing 7 | import numpy.typing as npt 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_KBinsDiscretizer(*args, **kwargs): 13 | return preprocessing.KBinsDiscretizer(*args, **kwargs) 14 | 15 | 16 | @validate(params=["n_bins"], 17 | validation_test=lambda param: not isinstance(param, int) or param >= 2, 18 | msg="Invalid parameter '%s' with value '%s'. Value must be 2 or higher.") 19 | @validate(params=["encode"], 20 | validation_test=lambda param: not isinstance(param, str) or param in ["onehot", "onehot-dense", "ordinal"], 21 | msg="Invalid parameter '%s' with value '%s'. Choose 'onehot', 'onehot-dense' or 'ordinal'.") 22 | @validate(params=["strategy"], 23 | validation_test=lambda param: not isinstance(param, str) or param in ["uniform", "quantile", "kmeans"], 24 | msg="Invalid parameter '%s' with value '%s'. Choose 'uniform', 'quantile' or 'kmeans'.") 25 | def k_bins_discretizer(name: str, 26 | n_bins: typing.Union[int, npt.ArrayLike, Apply] = None, 27 | encode: typing.Union[str, Apply] = None, 28 | strategy: typing.Union[str, Apply] = None, 29 | subsample: typing.Union[int, None, Apply] = None, 30 | dtype=None): 31 | """ 32 | Return a pyll graph with hyperparameters that will construct 33 | a sklearn.preprocessing.KBinsDiscretizer transformer. 34 | 35 | Args: 36 | name: name | str 37 | n_bins: number of bins | int, npt.ArrayLike 38 | encode: encoding method | str 39 | strategy: strategy used to define width of bins | str 40 | subsample: subsample size of training data | int, None 41 | dtype: dtype of output | type 42 | """ 43 | rval = scope.sklearn_KBinsDiscretizer( 44 | n_bins=scope.int(hp.uniform(name + ".n_bins", 2, 20)) if n_bins is None else n_bins, 45 | encode=hp.choice(name + ".encode", ["onehot-dense", "ordinal"]) if encode is None else encode, 46 | strategy=hp.choice(name + ".strategy", ["uniform", "quantile", "kmeans"]) if strategy is None else strategy, 47 | subsample=hp.choice(name + ".subsample", [200000, None] if subsample is None else subsample), 48 | dtype=dtype 49 | ) 50 | 51 | return rval 52 | -------------------------------------------------------------------------------- /hpsklearn/components/preprocessing/_encoders.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import preprocessing 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_OneHotEncoder(*args, **kwargs): 13 | return preprocessing.OneHotEncoder(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_OrdinalEncoder(*args, **kwargs): 18 | return preprocessing.OrdinalEncoder(*args, **kwargs) 19 | 20 | 21 | @validate(params=["categories"], 22 | validation_test=lambda param: not isinstance(param, str) or param == "auto", 23 | msg="Invalid parameter '%s' with value '%s'. Choose 'auto' or a list of array-like.") 24 | @validate(params=["drop"], 25 | validation_test=lambda param: not isinstance(param, str) or param in ("first", "if_binary"), 26 | msg="Invalid parameter '%s' with value '%s'. Choose 'first' or 'if_binary'.") 27 | def one_hot_encoder(name: str, 28 | categories: typing.Union[str, list] = "auto", 29 | drop: typing.Union[str, np.ndarray, Apply] = None, 30 | sparse_output: bool = True, 31 | dtype: type = np.float64): 32 | """ 33 | Return a pyll graph with hyperparameters that will construct 34 | a sklearn.preprocessing.OneHotEncoder transformer. 35 | 36 | Args: 37 | name: name | str 38 | categories: categories per feature | str or list 39 | drop: choose 'first' or 'if_binary' | str or np.ndarray 40 | sparse_output: return sparse_output matrix or array | bool 41 | dtype: desired dtype of output | type 42 | """ 43 | rval = scope.sklearn_OneHotEncoder( 44 | categories=categories, 45 | drop=hp.choice(name + ".drop", ["first", "if_binary"]) if drop is None else drop, 46 | sparse_output=sparse_output, 47 | dtype=dtype 48 | ) 49 | 50 | return rval 51 | 52 | 53 | @validate(params=["categories"], 54 | validation_test=lambda param: not isinstance(param, str) or param == "auto", 55 | msg="Invalid parameter '%s' with value '%s'. Choose 'auto' or a list of array-like.") 56 | @validate(params=["handle_unknown"], 57 | validation_test=lambda param: not isinstance(param, str) or param in ("error", "use_encoded_value"), 58 | msg="Invalid parameter '%s' with value '%s'. Choose 'error' or 'use_encoded_value'.") 59 | def ordinal_encoder(name: str, 60 | categories: typing.Union[str, list] = "auto", 61 | dtype: type = np.float64, 62 | handle_unknown: str = "error", 63 | unknown_value: float = None): 64 | """ 65 | Return a pyll graph with hyperparameters that will construct 66 | a sklearn.preprocessing.OrdinalEncoder transformer. 67 | 68 | Args: 69 | name: name | str 70 | categories: categories per feature | str or list 71 | dtype: desired dtype of output | type 72 | handle_unknown: choose 'error' or 'use_encoded_value' | str 73 | unknown_value: value for unknown categories | int or np.nan 74 | """ 75 | rval = scope.sklearn_OrdinalEncoder( 76 | categories=categories, 77 | dtype=dtype, 78 | handle_unknown=handle_unknown, 79 | unknown_value=unknown_value 80 | ) 81 | 82 | return rval 83 | -------------------------------------------------------------------------------- /hpsklearn/components/preprocessing/_polynomial.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import preprocessing 7 | import numpy.typing as npt 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_PolynomialFeatures(*args, **kwargs): 13 | return preprocessing.PolynomialFeatures(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_SplineTransformer(*args, **kwargs): 18 | return preprocessing.SplineTransformer(*args, **kwargs) 19 | 20 | 21 | @validate(params=["order"], 22 | validation_test=lambda param: not isinstance(param, str) or param in ["C", "F"], 23 | msg="Invalid parameter '%s' with value '%s'. Value must be either 'C' or 'F'.") 24 | def polynomial_features(name: str, 25 | degree: typing.Union[int, tuple, Apply] = None, 26 | interaction_only: typing.Union[bool, Apply] = None, 27 | include_bias: typing.Union[bool, Apply] = None, 28 | order: typing.Union[str, Apply] = None): 29 | """ 30 | Return a pyll graph with hyperparameters that will construct 31 | a sklearn.preprocessing.PolynomialFeatures transformer. 32 | 33 | Args: 34 | name: name | str 35 | degree : degree of polynomial features | int 36 | interaction_only: produce only interaction features | bool 37 | include_bias: whether to include a bias column | bool 38 | order: order of output array in dense case | str 39 | """ 40 | rval = scope.sklearn_PolynomialFeatures( 41 | degree=scope.int(hp.uniform(name + ".degree", 1, 5)) if degree is None else degree, 42 | interaction_only=hp.choice(name + ".interaction_only", [True, False]) 43 | if interaction_only is None else interaction_only, 44 | include_bias=hp.choice(name + ".include_bias", [True, False]) if include_bias is None else include_bias, 45 | order=hp.choice(name + ".order", ["C", "F"]) if order is None else order 46 | ) 47 | 48 | return rval 49 | 50 | 51 | @validate(params=["knots"], 52 | validation_test=lambda param: not isinstance(param, str) or param in ["uniform", "quantile"], 53 | msg="Invalid parameter '%s' with value '%s'. Value must be either 'uniform' or 'quantile'.") 54 | @validate(params=["extrapolation"], 55 | validation_test=lambda param: not isinstance(param, str) or param in ["error", "constant", "linear", 56 | "continue", "periodic"], 57 | msg="Invalid parameter '%s' with value '%s'. " 58 | "Value must be either 'error', 'constant', 'linear', 'continue', or 'periodic'.") 59 | @validate(params=["order"], 60 | validation_test=lambda param: not isinstance(param, str) or param in ["C", "F"], 61 | msg="Invalid parameter '%s' with value '%s'. Value must be either 'C' or 'F'.") 62 | def spline_transformer(name: str, 63 | n_knots: typing.Union[int, Apply] = None, 64 | degree: typing.Union[int, Apply] = None, 65 | knots: typing.Union[str, npt.ArrayLike, Apply] = None, 66 | extrapolation: typing.Union[str, Apply] = None, 67 | include_bias: typing.Union[bool, Apply] = None, 68 | order: typing.Union[str, Apply] = None): 69 | """ 70 | Return a pyll graph with hyperparameters that will construct 71 | a sklearn.preprocessing.SplineTransformer transformer. 72 | 73 | Args: 74 | name: name | str 75 | n_knots: number of knots of splines | int 76 | degree: polynomial degree of spline | int 77 | knots: knot positions | str, npt.ArrayLike 78 | extrapolation: extrapolation method | str 79 | include_bias: whether to include a bias column | bool 80 | order: order of output array in dense case | str 81 | """ 82 | rval = scope.sklearn_SplineTransformer( 83 | n_knots=scope.int(hp.uniform(name + ".n_knots", 5, 7)) if n_knots is None else n_knots, 84 | degree=scope.int(hp.uniform(name + ".degree", 2, 4)) if degree is None else degree, 85 | knots=hp.choice(name + ".knots", ["uniform", "quantile"]) if knots is None else knots, 86 | extrapolation=hp.choice(name + ".extrapolation", ["constant", "linear", "continue", "periodic"]) 87 | if extrapolation is None else extrapolation, 88 | include_bias=hp.choice(name + ".include_bias", [True, False]) if include_bias is None else include_bias, 89 | order=hp.choice(name + ".order", ["C", "F"]) if order is None else order 90 | ) 91 | 92 | return rval 93 | -------------------------------------------------------------------------------- /hpsklearn/components/semi_supervised/__init__.py: -------------------------------------------------------------------------------- 1 | from ._label_propagation import \ 2 | label_propagation, \ 3 | label_spreading 4 | -------------------------------------------------------------------------------- /hpsklearn/components/semi_supervised/_label_propagation.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | 3 | from hyperopt.pyll import scope, Apply 4 | from hyperopt import hp 5 | 6 | from sklearn import semi_supervised 7 | import numpy as np 8 | import typing 9 | 10 | 11 | @scope.define 12 | def sklearn_LabelPropagation(*args, **kwargs): 13 | return semi_supervised.LabelPropagation(*args, **kwargs) 14 | 15 | 16 | @scope.define 17 | def sklearn_LabelSpreading(*args, **kwargs): 18 | return semi_supervised.LabelSpreading(*args, **kwargs) 19 | 20 | 21 | def _label_propagation_kernel(name: str): 22 | """ 23 | Declaration search space 'kernel' parameter 24 | """ 25 | return hp.choice(name, ["knn", "rbf"]) 26 | 27 | 28 | def _label_propagation_gamma(name: str): 29 | """ 30 | Declaration search space 'gamma' parameter 31 | """ 32 | return hp.uniform(name, 10, 30) 33 | 34 | 35 | def _label_propagation_n_neighbors(name: str): 36 | """ 37 | Declaration search space 'n_neighbors' parameter 38 | """ 39 | return scope.int(hp.uniform(name, 3, 11)) 40 | 41 | 42 | def _label_propagation_n_tol(name: str): 43 | """ 44 | Declaration search space 'tol' parameter 45 | """ 46 | return hp.loguniform(name, np.log(1e-5), np.log(1e-2)) 47 | 48 | 49 | @validate(params=["gamma", "n_neighbors", "tol"], 50 | validation_test=lambda param: not isinstance(param, float) or param > 0, 51 | msg="Invalid parameter '%s' with value '%s'. Parameter value must be non-negative and greater than 0.") 52 | def _label_propagation_hp_space( 53 | name_func, 54 | kernel: typing.Union[str, callable, Apply] = None, 55 | gamma: typing.Union[float, Apply] = None, 56 | n_neighbors: typing.Union[int, Apply] = None, 57 | tol: typing.Union[float, Apply] = None, 58 | n_jobs: int = 1, 59 | **kwargs 60 | ): 61 | """ 62 | Hyper parameter search space for 63 | label propagation 64 | label spreading 65 | """ 66 | hp_space = dict( 67 | kernel=_label_propagation_kernel(name_func("kernel")) if kernel is None else kernel, 68 | gamma=_label_propagation_gamma(name_func("gamma")) if gamma is None else gamma, 69 | n_neighbors=_label_propagation_n_neighbors(name_func("n_neighbors")) if n_neighbors is None else n_neighbors, 70 | tol=_label_propagation_n_tol(name_func("tol")) if tol is None else tol, 71 | n_jobs=n_jobs, 72 | **kwargs 73 | ) 74 | return hp_space 75 | 76 | 77 | def label_propagation(name: str, max_iter: typing.Union[int, Apply] = None, **kwargs): 78 | """ 79 | Return a pyll graph with hyperparameters that will construct 80 | a sklearn.semi_supervised.LabelPropagation model. 81 | 82 | Args: 83 | name: name | str 84 | max_iter: maximum iterations | int 85 | 86 | See help(hpsklearn.components.semi_supervised._label_propagation._label_propagation_hp_space) 87 | for info on additional available label propagation arguments. 88 | """ 89 | 90 | def _name(msg): 91 | return f"{name}.label_propagation_{msg}" 92 | 93 | hp_space = _label_propagation_hp_space(_name, **kwargs) 94 | hp_space["max_iter"] = scope.int(hp.uniform(_name("max_iter"), 750, 1250)) if max_iter is None else max_iter 95 | 96 | return scope.sklearn_LabelPropagation(**hp_space) 97 | 98 | 99 | def label_spreading(name: str, 100 | alpha: typing.Union[float, Apply] = None, 101 | max_iter: typing.Union[int, Apply] = None, 102 | **kwargs): 103 | """ 104 | Return a pyll graph with hyperparameters that will construct 105 | a sklearn.semi_supervised.LabelPropagation model. 106 | 107 | Args: 108 | name: name | str 109 | alpha: clamping factor | float 110 | max_iter: maximum iterations | int 111 | 112 | See help(hpsklearn.components.semi_supervised._label_propagation._label_propagation_hp_space) 113 | for info on additional available label propagation arguments. 114 | """ 115 | 116 | def _name(msg): 117 | return f"{name}.label_spreading_{msg}" 118 | 119 | hp_space = _label_propagation_hp_space(_name, **kwargs) 120 | hp_space["alpha"] = hp.uniform(_name("alpha"), 0.1, 0.9) if alpha is None else alpha 121 | hp_space["max_iter"] = scope.int(hp.uniform(_name("max_iter"), 10, 50)) if max_iter is None else max_iter 122 | 123 | return scope.sklearn_LabelSpreading(**hp_space) 124 | -------------------------------------------------------------------------------- /hpsklearn/components/svm/__init__.py: -------------------------------------------------------------------------------- 1 | from ._classes import \ 2 | linear_svc, \ 3 | linear_svr, \ 4 | nu_svc, \ 5 | nu_svr, \ 6 | one_class_svm, \ 7 | svc, \ 8 | svr 9 | -------------------------------------------------------------------------------- /hpsklearn/components/tree/__init__.py: -------------------------------------------------------------------------------- 1 | from ._classes import \ 2 | decision_tree_classifier, \ 3 | decision_tree_regressor, \ 4 | extra_tree_classifier, \ 5 | extra_tree_regressor 6 | -------------------------------------------------------------------------------- /hpsklearn/components/vkmeans.py: -------------------------------------------------------------------------------- 1 | from hpsklearn.components._base import validate 2 | from hpsklearn.objects import ColumnKMeans 3 | 4 | from hyperopt.pyll import scope, Apply 5 | from hyperopt import hp 6 | 7 | import numpy.typing as npt 8 | import numpy as np 9 | import typing 10 | 11 | 12 | @scope.define 13 | def sklearn_ColumnKMeans(*args, **kwargs): 14 | return ColumnKMeans(*args, **kwargs) 15 | 16 | 17 | @validate(params=["max_iter", "n_clusters"], 18 | validation_test=lambda param: param > 0, 19 | msg="Invalid parameter '%s' with value '%s'. Value must be 0 or higher.") 20 | def colkmeans(name: str, 21 | n_clusters: typing.Union[int, Apply] = None, 22 | init: typing.Union[callable, npt.ArrayLike, str, Apply] = None, 23 | n_init: typing.Union[int, Apply] = None, 24 | max_iter: typing.Union[int, Apply] = None, 25 | tol: typing.Union[float, Apply] = None, 26 | verbose: int = 0, 27 | random_state=None, 28 | copy_x: bool = True): 29 | """ 30 | Return a pyll graph with hyperparameters that will construct 31 | a hpsklearn.objects.ColumnKMeans transformer. 32 | 33 | Args: 34 | name: name | str 35 | n_clusters: number of clusters | int 36 | init: initialization method for the centroids | callable, array-like, str 37 | n_init: number of times to run k-means algorithm | int 38 | max_iter: maximum number of iterations | int 39 | tol: relative tolerance in regard to Frobenius norm | float 40 | precompute_distances: precompute distances | bool 41 | verbose: verbosity level | int 42 | random_state: random seed | int 43 | copy_x: modify copy of data | bool 44 | n_jobs: number of jobs to run in parallel | int 45 | """ 46 | 47 | rval = scope.sklearn_ColumnKMeans( 48 | n_clusters=scope.int(hp.qloguniform(name + ".n_clusters", low=np.log(1.51), high=np.log(19.5), q=1.0)) 49 | if n_clusters is None else n_clusters, 50 | init=hp.choice(name + ".init", ["k-means++", "random"]) if init is None else init, 51 | n_init=hp.choice(name + ".n_init", [1, 2, 10, 20]) if n_init is None else n_init, 52 | max_iter=scope.int(hp.qlognormal(name + ".max_iter", np.log(300), np.log(10), q=1)) 53 | if max_iter is None else max_iter, 54 | tol=hp.lognormal(name + ".tol", np.log(0.0001), np.log(10)) if tol is None else tol, 55 | verbose=verbose, 56 | random_state=random_state, 57 | copy_x=copy_x, 58 | ) 59 | return rval 60 | -------------------------------------------------------------------------------- /hpsklearn/estimator/__init__.py: -------------------------------------------------------------------------------- 1 | from .estimator import hyperopt_estimator 2 | -------------------------------------------------------------------------------- /hpsklearn/estimator/_transform.py: -------------------------------------------------------------------------------- 1 | from ._utils import _NonFiniteFeature 2 | 3 | import numpy as np 4 | import scipy.sparse 5 | from sklearn.decomposition import PCA 6 | 7 | 8 | def _transform_combine_XEX(Xfit, 9 | info: callable = print, 10 | en_pps=None, 11 | Xval=None, 12 | EXfit_list: list = None, 13 | ex_pps_list=None, 14 | EXval_list: list = None, 15 | fit_preproc: bool = True): 16 | """ 17 | Transform endogenous and exogenous datasets and combine them into a 18 | single dataset for training and testing. 19 | 20 | Args: 21 | Xfit: 22 | Indices of X from endogenous dataset. 23 | 24 | info: callable, default is print 25 | Callable to handle information with during the loss calculation 26 | process. 27 | 28 | en_pps: default is None 29 | This should evaluate to a list of sklearn-style preprocessing 30 | modules (may include hyperparameters). When None, a random 31 | preprocessing module will be used. 32 | 33 | Xval: default is None 34 | Values of X from endogenous dataset. 35 | 36 | EXfit_list: list, default is None 37 | Indices of X from exogenous dataset. 38 | 39 | ex_pps_list: default is None 40 | This should evaluate to a list of lists of sklearn-style 41 | preprocessing modules for each exogenous dataset. When None, no 42 | preprocessing will be applied to exogenous data. 43 | 44 | EXval_list: list, default is None 45 | Values of X from exogenous dataset. 46 | 47 | fit_preproc: bool, default is True 48 | Whether to fit the preprocessing algorithm 49 | """ 50 | if not ex_pps_list: 51 | ex_pps_list = list() 52 | 53 | if not en_pps: 54 | en_pps = list() 55 | 56 | transformed_XEX_list = list() 57 | en_pps_list, ex_pps_list = list(en_pps), list(ex_pps_list) 58 | 59 | if not ex_pps_list and EXfit_list is not None: 60 | ex_pps_list = [[]] * len(EXfit_list) 61 | xex_pps_list = [en_pps_list] + ex_pps_list 62 | 63 | if EXfit_list is None: 64 | EXfit_list = list() 65 | assert EXval_list is None 66 | EXval_list = list() 67 | elif EXval_list is None: 68 | EXval_list = [None] * len(EXfit_list) 69 | 70 | EXfit_list, EXval_list = list(EXfit_list), list(EXval_list) 71 | XEXfit_list, XEXval_list = [Xfit] + EXfit_list, [Xval] + EXval_list 72 | 73 | for pps, dfit, dval in zip(xex_pps_list, XEXfit_list, XEXval_list): 74 | if pps: 75 | dfit, dval = _run_preprocs(preprocessing=pps, 76 | Xfit=dfit, Xval=dval, 77 | info=info, fit_preproc=fit_preproc) 78 | if dval is not None: 79 | transformed_XEX_list.append((dfit, dval)) 80 | else: 81 | transformed_XEX_list.append(dfit) 82 | 83 | if Xval is None: 84 | XEXfit = _safe_concatenate(transformed_XEX_list) 85 | return XEXfit 86 | else: 87 | XEXfit_list, XEXval_list = zip(*transformed_XEX_list) 88 | XEXfit = _safe_concatenate(XEXfit_list) 89 | XEXval = _safe_concatenate(XEXval_list) 90 | return XEXfit, XEXval 91 | 92 | 93 | def _run_preprocs(preprocessing: list, 94 | Xfit, 95 | Xval=None, 96 | info: callable = print, 97 | fit_preproc: bool = True): 98 | """ 99 | Run all preprocessing steps in a pipeline 100 | 101 | Args: 102 | preprocessing: list 103 | All preprocessing steps 104 | 105 | Xfit: 106 | Indices of X from combined endogenous and exogenous 107 | datasets. 108 | 109 | Xval: 110 | Values of X from combined endogenous and exogenous 111 | datasets. 112 | 113 | info: callable, default is print 114 | Callable to handle information with during the loss 115 | calculation process. 116 | 117 | fit_preproc: bool, default is True 118 | Whether to fit the preprocessing algorithm 119 | """ 120 | for pp_algo in preprocessing: 121 | info(f"Fitting {pp_algo} to X of shape {Xfit.shape}") 122 | if isinstance(pp_algo, PCA): 123 | n_components = pp_algo.get_params()["n_components"] 124 | n_components = min([n_components] + list(Xfit.shape)) 125 | pp_algo.set_params(n_components=n_components) 126 | info(f"Limited PCA n_components at {n_components}") 127 | 128 | if fit_preproc: 129 | pp_algo.fit(Xfit) 130 | 131 | info(f"Transforming Xfit {Xfit.shape}") 132 | Xfit = pp_algo.transform(Xfit) 133 | 134 | # np.isfinite() does not work on sparse matrices 135 | if not (scipy.sparse.issparse(Xfit) or np.all(np.isfinite(Xfit))): 136 | raise _NonFiniteFeature(pp_algo) 137 | 138 | if Xval is not None: 139 | info(f"Transforming Xval {Xval.shape}") 140 | Xval = pp_algo.transform(Xval) 141 | if not (scipy.sparse.issparse(Xval) or np.all(np.isfinite(Xval))): 142 | raise _NonFiniteFeature(pp_algo) 143 | 144 | return Xfit, Xval 145 | 146 | 147 | def _safe_concatenate(XS: list): 148 | """ 149 | Safely concatenate lists 150 | use np.concatenate or scipy concatenation 151 | 152 | Args: 153 | XS: list 154 | List to concatenate. 155 | """ 156 | if not any(scipy.sparse.issparse(x) for x in XS): 157 | return np.concatenate(XS, axis=1) 158 | 159 | XS = [x if scipy.sparse.issparse(x) else scipy.sparse.csr_matrix(x) for x in XS] 160 | 161 | return scipy.sparse.hstack(XS) 162 | -------------------------------------------------------------------------------- /hpsklearn/estimator/_utils.py: -------------------------------------------------------------------------------- 1 | import hyperopt 2 | import time 3 | import typing 4 | 5 | 6 | class _NonFiniteFeature(Exception): 7 | """ 8 | Called after finite check 9 | used for exception handling 10 | """ 11 | 12 | 13 | def _custom_handler(str_exc: str, t_start: float, exc) -> typing.Tuple[Exception, str]: 14 | """ 15 | Custom exception handler to reduce duplicated code. 16 | """ 17 | if str_exc in str(exc): 18 | rval = { 19 | "status": hyperopt.STATUS_FAIL, 20 | "failure": str(exc), 21 | "duration": time.time() - t_start 22 | } 23 | rtype = "return" 24 | else: 25 | rval = exc 26 | rtype = "raise" 27 | 28 | return rval, rtype 29 | -------------------------------------------------------------------------------- /hpsklearn/objects/__init__.py: -------------------------------------------------------------------------------- 1 | from .lagselectors import LagSelector 2 | from .vkmeans import ColumnKMeans 3 | -------------------------------------------------------------------------------- /hpsklearn/objects/lagselectors.py: -------------------------------------------------------------------------------- 1 | """ 2 | Lag selectors that subset time series predictors 3 | 4 | This module defines lag selectors with specified lag sizes for endogenous and 5 | exogenous predictors, using the same style as the sklearn transformers. They 6 | can be used in hpsklearn as preprocessors. The module is well suited for time 7 | series data. 8 | 9 | When use a lag size of a positive integer, it is assumed that lag=1, 2, ... 10 | predictors are located at the 1st, 2nd, ... columns. When use a negative 11 | integer, the predictors are located at the N-th, (N - 1)th, ... columns. 12 | """ 13 | from sklearn.base import BaseEstimator, TransformerMixin 14 | 15 | 16 | class LagSelector(BaseEstimator, TransformerMixin): 17 | """ 18 | Subset time series features by choosing the most recent lags 19 | 20 | Parameters 21 | ---------- 22 | lag_size : int, None by default 23 | If None, use all features. If positive integer, use features by 24 | subsetting the X as [:, :lag_size]. If negative integer, use features 25 | by subsetting the X as [:, lag_size:]. If 0, discard the features 26 | from this dataset. 27 | 28 | Attributes 29 | ---------- 30 | max_lag_size_ : int 31 | The largest allowed lag size inferred from input. 32 | """ 33 | def __init__(self, lag_size: int = None): 34 | self.lag_size = lag_size 35 | 36 | def _reset(self): 37 | """ 38 | Reset internal data-dependent state of the selector, if necessary. 39 | 40 | __init__ parameters are not touched. 41 | """ 42 | if hasattr(self, "max_lag_size_"): 43 | del self.max_lag_size_ 44 | 45 | def fit(self, X, y=None): 46 | """ 47 | Infer the maximum lag size. 48 | 49 | Parameters 50 | ---------- 51 | X : {array-like, sparse matrix}, shape [n_samples, n_features] 52 | The input time series data with lagged predictors as features. 53 | 54 | y : None 55 | Ignored. This parameter exists only for compatibility with 56 | ``sklearn.pipeline.Pipeline``. 57 | """ 58 | # Reset internal state before fitting 59 | self._reset() 60 | self.max_lag_size_ = X.shape[1] 61 | 62 | def transform(self, X, y=None): 63 | """ 64 | Perform standardization by centering and scaling 65 | 66 | Parameters 67 | ---------- 68 | X : array-like, shape [n_samples, n_features] 69 | The input time series data with lagged predictors as features. 70 | 71 | y : None 72 | Ignored. This parameter exists only for compatibility with 73 | ``sklearn.pipeline.Pipeline``. 74 | """ 75 | proofed_lag_size = min(self.max_lag_size_, abs(self.lag_size)) 76 | if self.lag_size >= 0: 77 | return X[:, :proofed_lag_size] 78 | else: 79 | return X[:, -proofed_lag_size:] 80 | -------------------------------------------------------------------------------- /hpsklearn/objects/vkmeans.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.cluster import KMeans 3 | 4 | 5 | class ColumnKMeans(object): 6 | def __init__(self, 7 | n_clusters, 8 | init="k-means++", 9 | n_init=10, 10 | max_iter=300, 11 | tol=1e-4, 12 | verbose=0, 13 | random_state=None, 14 | copy_x=True, 15 | ): 16 | self.n_clusters = n_clusters 17 | self.init = init 18 | self.n_init = n_init 19 | self.max_iter = max_iter 20 | self.tol = tol 21 | self.verbose = verbose 22 | self.random_state = random_state 23 | self.copy_x = copy_x 24 | self.output_dtype = None 25 | 26 | def fit(self, X): 27 | """ 28 | Compute k-means clustering. 29 | 30 | Parameters 31 | ---------- 32 | X : array-like or sparse matrix, shape=(n_samples, n_features) 33 | Training instances to cluster. 34 | 35 | Returns 36 | ------- 37 | self : object 38 | Fitted estimator. 39 | """ 40 | rows, cols = X.shape 41 | self.col_models = [] 42 | 43 | for jj in range(cols): 44 | col_model = KMeans( 45 | n_clusters=self.n_clusters, 46 | init=self.init, 47 | n_init=self.n_init, 48 | max_iter=self.max_iter, 49 | tol=self.tol, 50 | verbose=self.verbose, 51 | random_state=self.random_state, 52 | copy_x=self.copy_x, 53 | ) 54 | col_model.fit(X[:, jj:jj + 1]) 55 | self.col_models.append(col_model) 56 | 57 | def transform(self, X): 58 | """ 59 | Transform X to a cluster-distance space. 60 | 61 | In the new space, each dimension is the distance to the cluster 62 | centers. Note that even if X is sparse, the array returned by 63 | `transform` will typically be dense. 64 | 65 | Parameters 66 | ---------- 67 | X : {array-like, sparse matrix}, shape = [n_samples, n_features] 68 | New data to transform. 69 | 70 | Returns 71 | ------- 72 | X_new : array, shape [n_samples, k] 73 | X transformed in the new space. 74 | """ 75 | rows, cols = X.shape 76 | if self.output_dtype is None: 77 | output_dtype = X.dtype # XXX 78 | else: 79 | output_dtype = self.output_dtype 80 | rval = np.empty((rows, cols, self.n_clusters), 81 | dtype=output_dtype) 82 | 83 | for jj in range(cols): 84 | Xj = X[:, jj:jj + 1] 85 | dists = self.col_models[jj].transform(Xj) 86 | feats = np.exp(-(dists ** 2)) 87 | # -- normalize features by row 88 | rval[:, jj, :] = feats / (feats.sum(axis=1)[:, None]) 89 | 90 | assert np.all(np.isfinite(rval)) 91 | 92 | return rval.reshape((rows, cols * self.n_clusters)) 93 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=71.0.0", "wheel"] 3 | build-backend = "setuptools.build_meta" 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | hyperopt==0.2.7 2 | numpy>=2.0.0,<=2.3.0 3 | scikit-learn>=1.5,<=1.7 4 | scipy>=1.15.0,<=1.15.3 5 | pandas>=2.1.0,<=2.3.0 6 | setuptools>=71.0.0 -------------------------------------------------------------------------------- /requirements_dev.txt: -------------------------------------------------------------------------------- 1 | tox>=4.20.0 2 | xgboost>=2.0.0,<=2.2.0 3 | lightgbm==4.6.0 4 | coverage==7.6.12 5 | -r requirements.txt 6 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = hyperopt-sklearn 3 | version = 1.1.1 4 | description = Hyperparameter Optimization for sklearn 5 | long_description = file: README.md 6 | long_description_content_type = text/markdown 7 | url = https://github.com/hyperopt/hyperopt-sklearn/ 8 | author = James Bergstra 9 | author_email = anon@anon.com 10 | maintainer = Pim Tholhuijsen 11 | maintainer_email = anon@anon.com 12 | license = BSD 13 | license_files = LICENSE.txt 14 | platforms = Linux, OS-X, Windows 15 | keywords = hyperopt, hyperparameter, sklearn 16 | classifiers = 17 | Development Status :: 3 - Alpha 18 | Intended Audience :: Education 19 | Intended Audience :: Science/Research 20 | Intended Audience :: Developers 21 | Environment :: Console 22 | License :: OSI Approved :: BSD License 23 | Operating System :: MacOS :: MacOS X 24 | Operating System :: Microsoft :: Windows 25 | Operating System :: POSIX 26 | Operating System :: Unix 27 | Programming Language :: Python :: 3 28 | Programming Language :: Python :: 3 :: Only 29 | Programming Language :: Python :: 3.11 30 | Programming Language :: Python :: 3.12 31 | Programming Language :: Python :: 3.13 32 | Topic :: Scientific/Engineering 33 | Topic :: Software Development 34 | 35 | [options] 36 | packages = find: 37 | install_requires = 38 | hyperopt==0.2.7 39 | numpy>=2.0.0,<=2.3.0 40 | scikit-learn>=1.5,<=1.7 41 | scipy>=1.15.0,<=1.15.3 42 | pandas>=2.1.0,<=2.3.0 43 | setuptools>=71.0.0 44 | python_requires = >=3.11 45 | zip_safe = False 46 | 47 | [options.extras_require] 48 | xgboost = xgboost>=2.0.0,<=2.2.0 49 | lightgbm = lightgbm==4.6.0 50 | testing = 51 | tox>=4.20.0 52 | coverage==7.6.12 -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | 4 | if __name__ == '__main__': 5 | setup() 6 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_components/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_cluster/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_cluster/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_cluster/test_kmeans.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | k_means, \ 5 | mini_batch_k_means 6 | from tests.utils import \ 7 | generate_attributes, \ 8 | IrisTest 9 | 10 | 11 | class TestKMeansIris(IrisTest): 12 | """ 13 | Class for _kmeans iris testing 14 | """ 15 | 16 | 17 | generate_attributes( 18 | TestClass=TestKMeansIris, 19 | fn_list=[k_means, mini_batch_k_means], 20 | is_classif=False, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_compose/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_compose/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_compose/test_target.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import transformed_target_regressor 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestTransformedTargetRegression(StandardRegressorTest): 10 | """ 11 | Class for _target regression testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestTransformedTargetRegression, 17 | fn_list=[transformed_target_regressor], 18 | is_classif=False, 19 | non_negative_input=True, 20 | non_negative_output=True, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_covariance/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_covariance/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_covariance/test_elliptic_envelope.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import elliptic_envelope 4 | from tests.utils import \ 5 | IrisTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestEllipticEnvelopeClassifier(IrisTest): 10 | """ 11 | Class for _elliptic_envelope classification testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestEllipticEnvelopeClassifier, 17 | fn_list=[elliptic_envelope], 18 | is_classif=True, 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_cross_decomposition/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_cross_decomposition/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_cross_decomposition/test_pls.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | HyperoptEstimator, \ 5 | cca, \ 6 | pls_canonical, \ 7 | pls_regression 8 | from tests.utils import \ 9 | StandardRegressorTest, \ 10 | generate_attributes, \ 11 | TrialsExceptionHandler 12 | from hyperopt import rand 13 | 14 | 15 | class TestPLSRegression(StandardRegressorTest): 16 | """ 17 | Class for _pls regression testing 18 | """ 19 | @TrialsExceptionHandler 20 | def test_cca(self): 21 | """ 22 | Instantiate cca hyperopt estimator model 23 | fit and score model 24 | """ 25 | model = HyperoptEstimator( 26 | regressor=cca(name="cca", n_components=1), 27 | preprocessing=[], 28 | algo=rand.suggest, 29 | trial_timeout=10.0, 30 | max_evals=5, 31 | ) 32 | model.fit(self.X_train, self.Y_train) 33 | model.score(self.X_test, self.Y_test) 34 | 35 | @TrialsExceptionHandler 36 | def test_pls_canonical(self): 37 | """ 38 | Instantiate pls canonical hyperopt estimator model 39 | fit and score model 40 | """ 41 | model = HyperoptEstimator( 42 | regressor=pls_canonical(name="pls_canonical", n_components=1), 43 | preprocessing=[], 44 | algo=rand.suggest, 45 | trial_timeout=10.0, 46 | max_evals=5, 47 | ) 48 | model.fit(self.X_train, self.Y_train) 49 | model.score(self.X_test, self.Y_test) 50 | 51 | 52 | generate_attributes( 53 | TestClass=TestPLSRegression, 54 | fn_list=[pls_regression], 55 | is_classif=False, 56 | ) 57 | 58 | 59 | if __name__ == '__main__': 60 | unittest.main() 61 | -------------------------------------------------------------------------------- /tests/test_components/test_decomposition/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_decomposition/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_decomposition/test_pca.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | pca, \ 5 | gaussian_nb 6 | from tests.utils import \ 7 | StandardPreprocessingTest, \ 8 | generate_preprocessor_attributes 9 | 10 | 11 | class TestPCA(StandardPreprocessingTest): 12 | """ 13 | Class for _pca preprocessing testing 14 | """ 15 | 16 | 17 | generate_preprocessor_attributes( 18 | TestClass=TestPCA, 19 | preprocessor_list=[pca], 20 | classifier=gaussian_nb, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_discriminant_analysis.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | linear_discriminant_analysis, \ 5 | quadratic_discriminant_analysis 6 | from tests.utils import \ 7 | StandardClassifierTest, \ 8 | generate_attributes 9 | 10 | 11 | class TestLinearDiscriminantAnalysisClassifier(StandardClassifierTest): 12 | """ 13 | Class for _discriminant_analysis classification testing 14 | """ 15 | 16 | 17 | generate_attributes( 18 | TestClass=TestLinearDiscriminantAnalysisClassifier, 19 | fn_list=[linear_discriminant_analysis, quadratic_discriminant_analysis], 20 | is_classif=True, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_dummy.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | dummy_classifier, \ 5 | dummy_regressor 6 | 7 | from tests.utils import \ 8 | StandardRegressorTest, \ 9 | StandardClassifierTest, \ 10 | generate_attributes 11 | 12 | 13 | class TestDummyClassifier(StandardClassifierTest): 14 | """ 15 | Class for _dummy classification testing 16 | """ 17 | 18 | 19 | class TestDummyRegression(StandardRegressorTest): 20 | """ 21 | Class for _dummy regression testing 22 | """ 23 | 24 | 25 | generate_attributes( 26 | TestClass=TestDummyClassifier, 27 | fn_list=[dummy_classifier], 28 | is_classif=True, 29 | ) 30 | 31 | 32 | generate_attributes( 33 | TestClass=TestDummyRegression, 34 | fn_list=[dummy_regressor], 35 | is_classif=False, 36 | ) 37 | 38 | 39 | if __name__ == '__main__': 40 | unittest.main() 41 | -------------------------------------------------------------------------------- /tests/test_components/test_ensemble/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_ensemble/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_ensemble/test_bagging.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from tests.utils import \ 4 | StandardClassifierTest, \ 5 | StandardRegressorTest, \ 6 | generate_attributes 7 | from hpsklearn import \ 8 | bagging_classifier, \ 9 | bagging_regressor 10 | 11 | 12 | class TestBaggingClassification(StandardClassifierTest): 13 | """ 14 | Class for _bagging classification testing 15 | """ 16 | 17 | 18 | class TestBaggingRegression(StandardRegressorTest): 19 | """ 20 | Class for _bagging regression testing 21 | """ 22 | 23 | 24 | generate_attributes( 25 | TestClass=TestBaggingClassification, 26 | fn_list=[bagging_classifier], 27 | is_classif=True 28 | ) 29 | 30 | 31 | generate_attributes( 32 | TestClass=TestBaggingRegression, 33 | fn_list=[bagging_regressor], 34 | is_classif=False 35 | ) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /tests/test_components/test_ensemble/test_forest.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import numpy as np 3 | 4 | from hyperopt import rand 5 | 6 | from hpsklearn import \ 7 | HyperoptEstimator, \ 8 | random_forest_classifier, \ 9 | random_forest_regressor, \ 10 | extra_trees_classifier, \ 11 | extra_trees_regressor 12 | from tests.utils import \ 13 | StandardClassifierTest, \ 14 | StandardRegressorTest, \ 15 | generate_attributes, \ 16 | TrialsExceptionHandler 17 | 18 | 19 | class TestForestClassification(StandardClassifierTest): 20 | """ 21 | Class for _forest classification testing 22 | """ 23 | 24 | 25 | class TestForestRegression(StandardRegressorTest): 26 | """ 27 | Class for _forest regression testing 28 | """ 29 | @TrialsExceptionHandler 30 | def test_poisson_function(self): 31 | """ 32 | Instantiate random forest regressor hyperopt estimator model 33 | define 'criterion' = 'poisson' 34 | fit and score model 35 | """ 36 | model = HyperoptEstimator( 37 | regressor=random_forest_regressor(name="poisson_regressor", 38 | criterion="poisson"), 39 | preprocessing=[], 40 | algo=rand.suggest, 41 | trial_timeout=10.0, 42 | max_evals=5, 43 | ) 44 | model.fit(np.abs(self.X_train), np.abs(self.Y_train)) 45 | model.score(np.abs(self.X_test), np.abs(self.Y_test)) 46 | 47 | test_poisson_function.__name__ = f"test_{random_forest_regressor.__name__}" 48 | 49 | 50 | generate_attributes( 51 | TestClass=TestForestClassification, 52 | fn_list=[random_forest_classifier, extra_trees_classifier], 53 | is_classif=True 54 | ) 55 | 56 | 57 | generate_attributes( 58 | TestClass=TestForestRegression, 59 | fn_list=[random_forest_regressor, extra_trees_regressor], 60 | is_classif=False 61 | ) 62 | 63 | 64 | if __name__ == '__main__': 65 | unittest.main() 66 | -------------------------------------------------------------------------------- /tests/test_components/test_ensemble/test_gb.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | gradient_boosting_classifier, \ 5 | gradient_boosting_regressor 6 | from tests.utils import \ 7 | StandardClassifierTest, \ 8 | StandardRegressorTest, \ 9 | generate_attributes 10 | 11 | 12 | class TestGradientBoostingClassification(StandardClassifierTest): 13 | """ 14 | Class for _gb classification testing 15 | """ 16 | 17 | 18 | class TestGradientBoostingRegression(StandardRegressorTest): 19 | """ 20 | Class for _gb regression testing 21 | """ 22 | 23 | 24 | generate_attributes( 25 | TestClass=TestGradientBoostingClassification, 26 | fn_list=[gradient_boosting_classifier], 27 | is_classif=True, 28 | ) 29 | 30 | 31 | generate_attributes( 32 | TestClass=TestGradientBoostingRegression, 33 | fn_list=[gradient_boosting_regressor], 34 | is_classif=False, 35 | ) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /tests/test_components/test_ensemble/test_hist_gradient_boosting.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import numpy as np 3 | 4 | from hyperopt import rand 5 | 6 | from hpsklearn import \ 7 | HyperoptEstimator, \ 8 | hist_gradient_boosting_classifier, \ 9 | hist_gradient_boosting_regressor 10 | from tests.utils import \ 11 | StandardClassifierTest, \ 12 | StandardRegressorTest, \ 13 | generate_attributes, \ 14 | TrialsExceptionHandler 15 | 16 | 17 | class TestHistGradientBoostingClassification(StandardClassifierTest): 18 | """ 19 | Class for _hist_gradient_boosting classification testing 20 | """ 21 | 22 | 23 | class TestHistGradientBoostingRegression(StandardRegressorTest): 24 | """ 25 | Class for _hist_gradient_boosting regression testing 26 | """ 27 | @TrialsExceptionHandler 28 | def test_poisson_function(self): 29 | """ 30 | Instantiate hist gradient boosting hyperopt estimator model 31 | define 'criterion' = 'poisson' 32 | fit and score model 33 | """ 34 | model = HyperoptEstimator( 35 | regressor=hist_gradient_boosting_regressor(name="poisson_regressor", 36 | loss="poisson"), 37 | preprocessing=[], 38 | algo=rand.suggest, 39 | trial_timeout=10.0, 40 | max_evals=5, 41 | ) 42 | model.fit(np.abs(self.X_train), np.abs(self.Y_train)) 43 | model.score(np.abs(self.X_test), np.abs(self.Y_test)) 44 | 45 | test_poisson_function.__name__ = f"test_{hist_gradient_boosting_regressor.__name__}" 46 | 47 | 48 | generate_attributes( 49 | TestClass=TestHistGradientBoostingClassification, 50 | fn_list=[hist_gradient_boosting_classifier], 51 | is_classif=True 52 | ) 53 | 54 | 55 | generate_attributes( 56 | TestClass=TestHistGradientBoostingRegression, 57 | fn_list=[hist_gradient_boosting_regressor], 58 | is_classif=False 59 | ) 60 | 61 | 62 | if __name__ == '__main__': 63 | unittest.main() 64 | -------------------------------------------------------------------------------- /tests/test_components/test_ensemble/test_iforest.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hyperopt import rand 4 | from hpsklearn import HyperoptEstimator, isolation_forest 5 | from sklearn.metrics import accuracy_score 6 | from tests.utils import \ 7 | IrisTest, \ 8 | TrialsExceptionHandler 9 | 10 | 11 | class TestIsolationForest(IrisTest): 12 | """ 13 | Class for _iforest testing 14 | """ 15 | @TrialsExceptionHandler 16 | def test_isolation_forest(self): 17 | """ 18 | Instantiate isolation forest classifier hyperopt estimator model 19 | fit and score model 20 | """ 21 | model = HyperoptEstimator( 22 | regressor=isolation_forest(name="i_forest"), 23 | preprocessing=[], 24 | algo=rand.suggest, 25 | trial_timeout=10.0, 26 | max_evals=5, 27 | ) 28 | model.fit(self.X_train, self.Y_train) 29 | accuracy_score(y_true=self.Y_test, y_pred=model.predict(self.X_test)) 30 | 31 | test_isolation_forest.__name__ = f"test_{isolation_forest.__name__}" 32 | 33 | 34 | if __name__ == '__main__': 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /tests/test_components/test_ensemble/test_weight_boosting.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | ada_boost_classifier, \ 5 | ada_boost_regressor 6 | from tests.utils import \ 7 | StandardClassifierTest, \ 8 | StandardRegressorTest, \ 9 | generate_attributes 10 | 11 | 12 | class TestWeightBoostingClassification(StandardClassifierTest): 13 | """ 14 | Class for _weight_boosting classification testing 15 | """ 16 | 17 | 18 | class TestWeightBoostingRegression(StandardRegressorTest): 19 | """ 20 | Class for _weight_boosting regression testing 21 | """ 22 | 23 | 24 | generate_attributes( 25 | TestClass=TestWeightBoostingClassification, 26 | fn_list=[ada_boost_classifier], 27 | is_classif=True 28 | ) 29 | 30 | 31 | generate_attributes( 32 | TestClass=TestWeightBoostingRegression, 33 | fn_list=[ada_boost_regressor], 34 | is_classif=False 35 | ) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /tests/test_components/test_feature_extraction/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_feature_extraction/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_feature_extraction/test_text.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | HyperoptEstimator, \ 5 | multinomial_nb, \ 6 | sgd_classifier, \ 7 | tfidf_vectorizer, \ 8 | hashing_vectorizer, \ 9 | count_vectorizer 10 | from tests.utils import TrialsExceptionHandler 11 | from hyperopt import rand 12 | 13 | import numpy as np 14 | 15 | 16 | class TestTfidf(unittest.TestCase): 17 | """ 18 | Class for TfidfVectorizer testing 19 | """ 20 | def setUp(self): 21 | """ 22 | Set up text data 23 | """ 24 | self.X_test = np.array([ 25 | "This is the first document.", 26 | "This document is the second document.", 27 | "And this is the third one.", 28 | "Is this the first document?", 29 | ]) 30 | 31 | self.Y_test = np.array([0, 1, 2, 0]) 32 | 33 | @TrialsExceptionHandler 34 | def test_tfidf_vectorizer(self): 35 | """ 36 | Instantiate multinomial_nb hyperopt_estimator model 37 | add TfidfVectorizer preprocessor 38 | fit and score model 39 | """ 40 | model = HyperoptEstimator( 41 | classifier=multinomial_nb("classifier"), 42 | preprocessing=[tfidf_vectorizer("preprocessing")], 43 | algo=rand.suggest, 44 | trial_timeout=10.0, 45 | max_evals=5, 46 | ) 47 | model.fit(self.X_test, self.Y_test) 48 | model.score(self.X_test, self.Y_test) 49 | 50 | @TrialsExceptionHandler 51 | def test_hashing_vectorizer(self): 52 | """ 53 | Instantiate sgd_classifier hyperopt_estimator model 54 | add HashingVectorizer preprocessor 55 | fit and score model 56 | """ 57 | model = HyperoptEstimator( 58 | classifier=sgd_classifier("classifier"), 59 | preprocessing=[hashing_vectorizer("preprocessing")], 60 | algo=rand.suggest, 61 | trial_timeout=10.0, 62 | max_evals=5, 63 | ) 64 | model.fit(self.X_test, self.Y_test) 65 | 66 | @TrialsExceptionHandler 67 | def test_count_vectorizer(self): 68 | """ 69 | Instantiate multinomial_nb hyperopt_estimator model 70 | add CountVectorizer preprocessor 71 | fit and score model 72 | """ 73 | model = HyperoptEstimator( 74 | classifier=multinomial_nb("classifier"), 75 | preprocessing=[count_vectorizer("preprocessing")], 76 | algo=rand.suggest, 77 | trial_timeout=10.0, 78 | max_evals=5, 79 | ) 80 | model.fit(self.X_test, self.Y_test) 81 | 82 | test_tfidf_vectorizer.__name__ = f"test_{tfidf_vectorizer.__name__}" 83 | test_hashing_vectorizer.__name__ = f"test_{hashing_vectorizer.__name__}" 84 | test_count_vectorizer.__name__ = f"test_{count_vectorizer.__name__}" 85 | 86 | 87 | if __name__ == "__main__": 88 | unittest.main() 89 | -------------------------------------------------------------------------------- /tests/test_components/test_gaussian_process/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_gaussian_process/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_gaussian_process/test_gpc.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import gaussian_process_classifier 4 | from tests.utils import \ 5 | StandardClassifierTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestGaussianProcessClassifier(StandardClassifierTest): 10 | """ 11 | Class for _gpc classification testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestGaussianProcessClassifier, 17 | fn_list=[gaussian_process_classifier], 18 | is_classif=True 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_gaussian_process/test_gpr.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import gaussian_process_regressor 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestGaussianProcessRegressor(StandardRegressorTest): 10 | """ 11 | Class for _gpr regression testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestGaussianProcessRegressor, 17 | fn_list=[gaussian_process_regressor], 18 | is_classif=False 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_kernel_ridge.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import hp_sklearn_kernel_ridge 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestKernelRidgeRegression(StandardRegressorTest): 10 | """ 11 | Class for _kernel_ridge regression testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestKernelRidgeRegression, 17 | fn_list=[hp_sklearn_kernel_ridge], 18 | is_classif=False, 19 | non_negative_input=True, 20 | ) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /tests/test_components/test_lightgbm.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | lightgbm_classification, \ 5 | lightgbm_regression 6 | from tests.utils import \ 7 | StandardRegressorTest, \ 8 | StandardClassifierTest, \ 9 | generate_attributes 10 | 11 | 12 | class TestLightGBMClassifier(StandardClassifierTest): 13 | """ 14 | Class for _lightgbm classification testing 15 | """ 16 | 17 | 18 | class TestLightGBMRegression(StandardRegressorTest): 19 | """ 20 | Class for _lightgbm regression testing 21 | """ 22 | 23 | 24 | generate_attributes( 25 | TestClass=TestLightGBMClassifier, 26 | fn_list=[lightgbm_classification], 27 | is_classif=True, 28 | ) 29 | 30 | 31 | generate_attributes( 32 | TestClass=TestLightGBMRegression, 33 | fn_list=[lightgbm_regression], 34 | is_classif=False, 35 | ) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_linear_model/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_base.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from tests.utils import \ 4 | StandardRegressorTest, \ 5 | generate_attributes 6 | from hpsklearn import linear_regression 7 | 8 | 9 | class TestBaseRegression(StandardRegressorTest): 10 | """ 11 | Class for _base regression testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestBaseRegression, 17 | fn_list=[linear_regression], 18 | is_classif=False 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_bayes.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from tests.utils import \ 4 | StandardRegressorTest, \ 5 | generate_attributes 6 | from hpsklearn import \ 7 | bayesian_ridge, \ 8 | ard_regression 9 | 10 | 11 | class TestBayesRegression(StandardRegressorTest): 12 | """ 13 | Class for _bayes regression testing 14 | """ 15 | 16 | 17 | generate_attributes( 18 | TestClass=TestBayesRegression, 19 | fn_list=[bayesian_ridge, ard_regression], 20 | is_classif=False 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_coordinate_descent.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | lasso, \ 5 | elastic_net, \ 6 | lasso_cv, \ 7 | elastic_net_cv, \ 8 | multi_task_lasso, \ 9 | multi_task_elastic_net, \ 10 | multi_task_lasso_cv, \ 11 | multi_task_elastic_net_cv 12 | from tests.utils import \ 13 | StandardRegressorTest, \ 14 | MultiTaskRegressorTest, \ 15 | generate_attributes 16 | 17 | 18 | class TestCoordinateDescentRegression(StandardRegressorTest): 19 | """ 20 | Class for _coordinate_descent regression testing 21 | """ 22 | 23 | 24 | class TestCoordinateDescentMultiTaskRegression(MultiTaskRegressorTest): 25 | """ 26 | Class for _coordinate_descent multi task regression testing 27 | """ 28 | 29 | 30 | # List of _coordinate_descent regressors to test 31 | regressors = [ 32 | lasso, 33 | elastic_net, 34 | lasso_cv, 35 | elastic_net_cv 36 | ] 37 | 38 | # List of _coordinate_descent multi task regressors to test 39 | multi_task_regressors = [ 40 | multi_task_lasso, 41 | multi_task_elastic_net, 42 | multi_task_lasso_cv, 43 | multi_task_elastic_net_cv 44 | ] 45 | 46 | 47 | generate_attributes( 48 | TestClass=TestCoordinateDescentRegression, 49 | fn_list=regressors, 50 | is_classif=False 51 | ) 52 | 53 | 54 | generate_attributes( 55 | TestClass=TestCoordinateDescentMultiTaskRegression, 56 | fn_list=multi_task_regressors, 57 | is_classif=False 58 | ) 59 | 60 | 61 | if __name__ == '__main__': 62 | unittest.main() 63 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_glm.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | poisson_regressor, \ 5 | gamma_regressor, \ 6 | tweedie_regressor 7 | from tests.utils import \ 8 | StandardRegressorTest, \ 9 | generate_attributes 10 | 11 | 12 | class TestGLMRegression(StandardRegressorTest): 13 | """ 14 | Class for _glm regression testing 15 | """ 16 | 17 | 18 | # List of _glm regressors to test 19 | regressors = [ 20 | poisson_regressor, 21 | gamma_regressor, 22 | tweedie_regressor 23 | ] 24 | 25 | 26 | generate_attributes( 27 | TestClass=TestGLMRegression, 28 | fn_list=regressors, 29 | is_classif=False, 30 | non_negative_output=True 31 | ) 32 | 33 | 34 | if __name__ == '__main__': 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_huber.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | huber_regressor 5 | from tests.utils import \ 6 | StandardRegressorTest, \ 7 | generate_attributes 8 | 9 | 10 | class TestHuberRegression(StandardRegressorTest): 11 | """ 12 | Class for _huber regression testing 13 | """ 14 | 15 | 16 | generate_attributes( 17 | TestClass=TestHuberRegression, 18 | fn_list=[huber_regressor], 19 | is_classif=False 20 | ) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_least_angle.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | lars, \ 5 | lasso_lars, \ 6 | lars_cv, \ 7 | lasso_lars_cv, \ 8 | lasso_lars_ic 9 | from tests.utils import \ 10 | StandardRegressorTest, \ 11 | generate_attributes 12 | 13 | 14 | class TestLeastAngleRegression(StandardRegressorTest): 15 | """ 16 | Class for _least_angle regression testing 17 | """ 18 | 19 | 20 | # List of _least_angle regressors to test 21 | regressors = [ 22 | lars, 23 | lasso_lars, 24 | lars_cv, 25 | lasso_lars_cv, 26 | lasso_lars_ic 27 | ] 28 | 29 | 30 | generate_attributes( 31 | TestClass=TestLeastAngleRegression, 32 | fn_list=regressors, 33 | is_classif=False 34 | ) 35 | 36 | 37 | if __name__ == '__main__': 38 | unittest.main() 39 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_logistic.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | logistic_regression, \ 5 | logistic_regression_cv 6 | from tests.utils import \ 7 | IrisTest, \ 8 | TrialsExceptionHandler 9 | from hyperopt import rand 10 | from hpsklearn import HyperoptEstimator 11 | from sklearn.metrics import accuracy_score 12 | 13 | 14 | class TestLogisticRegression(IrisTest): 15 | """ 16 | Class for _logistic regression testing 17 | """ 18 | 19 | 20 | def create_regression_attr(fn: callable): 21 | """ 22 | Instantiate hyperopt estimator model 23 | 24 | Args: 25 | fn: estimator to test | callable 26 | 27 | fit and score model 28 | """ 29 | @TrialsExceptionHandler 30 | def test_regressor(self): 31 | model = HyperoptEstimator( 32 | regressor=fn(name="regressor"), 33 | preprocessing=[], 34 | algo=rand.suggest, 35 | trial_timeout=10.0, 36 | max_evals=5, 37 | ) 38 | model.fit(self.X_train, self.Y_train) 39 | accuracy_score(y_true=self.Y_test, y_pred=model.predict(self.X_test)) 40 | 41 | test_regressor.__name__ = f"test_{fn.__name__}" 42 | return test_regressor 43 | 44 | 45 | for reg_fn in [logistic_regression, logistic_regression_cv]: 46 | setattr( 47 | TestLogisticRegression, 48 | f"test_{reg_fn.__name__}", 49 | create_regression_attr(reg_fn) 50 | ) 51 | 52 | 53 | if __name__ == '__main__': 54 | unittest.main() 55 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_omp.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | orthogonal_matching_pursuit, \ 5 | orthogonal_matching_pursuit_cv 6 | from tests.utils import \ 7 | StandardRegressorTest, \ 8 | generate_attributes 9 | 10 | 11 | class TestOMPRegression(StandardRegressorTest): 12 | """ 13 | Class for _omp regression testing 14 | """ 15 | 16 | 17 | generate_attributes( 18 | TestClass=TestOMPRegression, 19 | fn_list=[orthogonal_matching_pursuit, orthogonal_matching_pursuit_cv], 20 | is_classif=False, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_passive_aggressive.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | passive_aggressive_classifier, \ 5 | passive_aggressive_regressor 6 | from tests.utils import \ 7 | StandardRegressorTest, \ 8 | StandardClassifierTest, \ 9 | generate_attributes 10 | 11 | 12 | class TestPassiveAggressiveClassifier(StandardClassifierTest): 13 | """ 14 | Class for _passive_aggressive classification testing 15 | """ 16 | 17 | 18 | class TestPassiveAggressiveRegression(StandardRegressorTest): 19 | """ 20 | Class for _passive_aggressive regression testing 21 | """ 22 | 23 | 24 | generate_attributes( 25 | TestClass=TestPassiveAggressiveClassifier, 26 | fn_list=[passive_aggressive_classifier], 27 | is_classif=True, 28 | ) 29 | 30 | 31 | generate_attributes( 32 | TestClass=TestPassiveAggressiveRegression, 33 | fn_list=[passive_aggressive_regressor], 34 | is_classif=False, 35 | ) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_perceptron.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import perceptron 4 | from tests.utils import \ 5 | StandardClassifierTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestPerceptronClassifier(StandardClassifierTest): 10 | """ 11 | Class for _perceptron classification testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestPerceptronClassifier, 17 | fn_list=[perceptron], 18 | is_classif=True, 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_quantile.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import quantile_regression 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | generate_attributes 7 | 8 | 9 | class QuantileRegressorTest(StandardRegressorTest): 10 | """ 11 | Class for _quantile regression testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=QuantileRegressorTest, 17 | fn_list=[quantile_regression], 18 | is_classif=False, 19 | trial_timeout=30.0, # Increase timeout for longer runtime due to some solvers 20 | ) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_ransac.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import ransac_regression 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestRANSACRegressor(StandardRegressorTest): 10 | """ 11 | Class for _ransac regression testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestRANSACRegressor, 17 | fn_list=[ransac_regression], 18 | is_classif=False, 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_ridge.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | ridge, \ 5 | ridge_cv, \ 6 | ridge_classifier, \ 7 | ridge_classifier_cv 8 | from tests.utils import \ 9 | StandardClassifierTest, \ 10 | StandardRegressorTest, \ 11 | generate_attributes 12 | 13 | 14 | class TestRidgeClassification(StandardClassifierTest): 15 | """ 16 | Class for _ridge classification testing 17 | """ 18 | 19 | 20 | class TestRidgeRegression(StandardRegressorTest): 21 | """ 22 | Class for _ridge regression testing 23 | """ 24 | 25 | 26 | generate_attributes( 27 | TestClass=TestRidgeClassification, 28 | fn_list=[ridge_classifier, ridge_classifier_cv], 29 | is_classif=True 30 | ) 31 | 32 | 33 | generate_attributes( 34 | TestClass=TestRidgeRegression, 35 | fn_list=[ridge, ridge_cv], 36 | is_classif=False 37 | ) 38 | 39 | 40 | if __name__ == '__main__': 41 | unittest.main() 42 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_stochastic_gradient.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | HyperoptEstimator, \ 5 | sgd_classifier, \ 6 | sgd_regressor, \ 7 | sgd_one_class_svm 8 | from tests.utils import \ 9 | StandardRegressorTest, \ 10 | StandardClassifierTest, \ 11 | generate_attributes, \ 12 | TrialsExceptionHandler 13 | 14 | from hyperopt import rand 15 | from sklearn.metrics import accuracy_score 16 | 17 | 18 | class TestSGDClassifier(StandardClassifierTest): 19 | """ 20 | Class for _stochastic_gradient classification testing 21 | """ 22 | 23 | 24 | class TestSGDRegression(StandardRegressorTest): 25 | """ 26 | Class for _stochastic_gradient regression testing 27 | """ 28 | 29 | 30 | class TestSGDOneClassSVM(StandardClassifierTest): 31 | """ 32 | Class for SGDOneClassSVM testing 33 | """ 34 | @TrialsExceptionHandler 35 | def test_sgd_one_class_svm(self): 36 | """ 37 | Instantiate sgd one class svm classifier hyperopt estimator model 38 | fit and score model 39 | """ 40 | model = HyperoptEstimator( 41 | regressor=sgd_one_class_svm(name="sgd_one_class_svm"), 42 | preprocessing=[], 43 | algo=rand.suggest, 44 | trial_timeout=10.0, 45 | max_evals=5, 46 | ) 47 | model.fit(self.X_train, self.Y_train) 48 | accuracy_score(y_true=self.Y_test, y_pred=model.predict(self.X_test)) 49 | 50 | test_sgd_one_class_svm.__name__ = f"test_{sgd_one_class_svm.__name__}" 51 | 52 | 53 | generate_attributes( 54 | TestClass=TestSGDClassifier, 55 | fn_list=[sgd_classifier], 56 | is_classif=True, 57 | ) 58 | 59 | 60 | generate_attributes( 61 | TestClass=TestSGDRegression, 62 | fn_list=[sgd_regressor], 63 | is_classif=False, 64 | ) 65 | 66 | 67 | if __name__ == '__main__': 68 | unittest.main() 69 | -------------------------------------------------------------------------------- /tests/test_components/test_linear_model/test_theil_sen.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import theil_sen_regressor 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestTheilSenRegressor(StandardRegressorTest): 10 | """ 11 | Class for _theil_sen regression testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestTheilSenRegressor, 17 | fn_list=[theil_sen_regressor], 18 | is_classif=False, 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_mixture/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_mixture/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_mixture/test_bayesian_mixture.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import bayesian_gaussian_mixture 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | StandardClassifierTest, \ 7 | generate_attributes 8 | 9 | 10 | class TestBayesianGaussianMixtureClassifier(StandardClassifierTest): 11 | """ 12 | Class for _bayesian_mixture classification testing 13 | """ 14 | 15 | 16 | class TestBayesianGaussianMixtureRegression(StandardRegressorTest): 17 | """ 18 | Class for _bayesian_mixture regression testing 19 | """ 20 | 21 | 22 | generate_attributes( 23 | TestClass=TestBayesianGaussianMixtureClassifier, 24 | fn_list=[bayesian_gaussian_mixture], 25 | is_classif=True, 26 | ) 27 | 28 | 29 | generate_attributes( 30 | TestClass=TestBayesianGaussianMixtureRegression, 31 | fn_list=[bayesian_gaussian_mixture], 32 | is_classif=False, 33 | ) 34 | 35 | 36 | if __name__ == '__main__': 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /tests/test_components/test_mixture/test_gaussian_mixture.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import gaussian_mixture 4 | from tests.utils import \ 5 | StandardRegressorTest, \ 6 | StandardClassifierTest, \ 7 | generate_attributes 8 | 9 | 10 | class TestGaussianMixtureClassifier(StandardClassifierTest): 11 | """ 12 | Class for _gaussian_mixture classification testing 13 | """ 14 | 15 | 16 | class TestGaussianMixtureRegression(StandardRegressorTest): 17 | """ 18 | Class for _gaussian_mixture regression testing 19 | """ 20 | 21 | 22 | generate_attributes( 23 | TestClass=TestGaussianMixtureClassifier, 24 | fn_list=[gaussian_mixture], 25 | is_classif=True, 26 | ) 27 | 28 | 29 | generate_attributes( 30 | TestClass=TestGaussianMixtureRegression, 31 | fn_list=[gaussian_mixture], 32 | is_classif=False, 33 | ) 34 | 35 | 36 | if __name__ == '__main__': 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /tests/test_components/test_multiclass.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | one_vs_rest_classifier, \ 5 | one_vs_one_classifier, \ 6 | output_code_classifier 7 | from tests.utils import \ 8 | StandardClassifierTest, \ 9 | generate_attributes 10 | 11 | 12 | class OneVsRestClassifierTest(StandardClassifierTest): 13 | """ 14 | Class for _multiclass classification testing 15 | """ 16 | 17 | 18 | generate_attributes( 19 | TestClass=OneVsRestClassifierTest, 20 | fn_list=[one_vs_rest_classifier, one_vs_one_classifier, output_code_classifier], 21 | is_classif=True, 22 | ) 23 | 24 | 25 | if __name__ == '__main__': 26 | unittest.main() 27 | -------------------------------------------------------------------------------- /tests/test_components/test_naive_bayes.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from tests.utils import \ 4 | StandardClassifierTest, \ 5 | generate_attributes 6 | from hpsklearn import \ 7 | bernoulli_nb, \ 8 | categorical_nb, \ 9 | complement_nb, \ 10 | gaussian_nb, \ 11 | multinomial_nb 12 | 13 | 14 | class TestNaiveBayes(StandardClassifierTest): 15 | """ 16 | Class for naive_bayes classification testing 17 | """ 18 | 19 | 20 | generate_attributes( 21 | TestClass=TestNaiveBayes, 22 | fn_list=[bernoulli_nb, gaussian_nb], 23 | is_classif=True 24 | ) 25 | 26 | 27 | generate_attributes( 28 | TestClass=TestNaiveBayes, 29 | fn_list=[categorical_nb, complement_nb, multinomial_nb], 30 | is_classif=True, 31 | non_negative_input=True 32 | ) 33 | 34 | 35 | if __name__ == '__main__': 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /tests/test_components/test_neighbors/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_neighbors/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_neighbors/test_classification.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | k_neighbors_classifier, \ 5 | radius_neighbors_classifier 6 | from tests.utils import \ 7 | StandardClassifierTest, \ 8 | generate_attributes 9 | 10 | 11 | class TestNeighborsClassifier(StandardClassifierTest): 12 | """ 13 | Class for _classification classification testing 14 | """ 15 | 16 | 17 | generate_attributes( 18 | TestClass=TestNeighborsClassifier, 19 | fn_list=[k_neighbors_classifier, radius_neighbors_classifier], 20 | is_classif=True, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_neighbors/test_nearest_centroid.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import nearest_centroid 4 | from tests.utils import \ 5 | StandardClassifierTest, \ 6 | generate_attributes 7 | 8 | 9 | class TestNearestCentroidClassifier(StandardClassifierTest): 10 | """ 11 | Class for _nearest_centroid classification testing 12 | """ 13 | 14 | 15 | generate_attributes( 16 | TestClass=TestNearestCentroidClassifier, 17 | fn_list=[nearest_centroid], 18 | is_classif=True, 19 | ) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /tests/test_components/test_neighbors/test_regression.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | k_neighbors_regressor, \ 5 | radius_neighbors_regressor 6 | from tests.utils import \ 7 | StandardRegressorTest, \ 8 | generate_attributes 9 | 10 | 11 | class TestNeighborsRegression(StandardRegressorTest): 12 | """ 13 | Class for _regression regression testing 14 | """ 15 | 16 | 17 | generate_attributes( 18 | TestClass=TestNeighborsRegression, 19 | fn_list=[k_neighbors_regressor, radius_neighbors_regressor], 20 | is_classif=False, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_neural_network/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_neural_network/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_neural_network/test_multilayer_perceptron.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | mlp_classifier, \ 5 | mlp_regressor 6 | from tests.utils import \ 7 | StandardRegressorTest, \ 8 | StandardClassifierTest, \ 9 | generate_attributes 10 | 11 | 12 | class TestMLPClassifier(StandardClassifierTest): 13 | """ 14 | Class for _multilayer_perceptron classification testing 15 | """ 16 | 17 | 18 | class TestMLPRegression(StandardRegressorTest): 19 | """ 20 | Class for _multilayer_perceptron regression testing 21 | """ 22 | 23 | 24 | generate_attributes( 25 | TestClass=TestMLPClassifier, 26 | fn_list=[mlp_classifier], 27 | is_classif=True, 28 | non_negative_input=True, 29 | non_negative_output=True 30 | ) 31 | 32 | 33 | generate_attributes( 34 | TestClass=TestMLPRegression, 35 | fn_list=[mlp_regressor], 36 | is_classif=False, 37 | non_negative_input=True, 38 | non_negative_output=True 39 | ) 40 | 41 | 42 | if __name__ == '__main__': 43 | unittest.main() 44 | -------------------------------------------------------------------------------- /tests/test_components/test_preprocessing/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_preprocessing/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_preprocessing/test_data.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hyperopt import rand 4 | 5 | from tests.utils import \ 6 | StandardPreprocessingTest, \ 7 | generate_preprocessor_attributes, \ 8 | TrialsExceptionHandler 9 | from hpsklearn import HyperoptEstimator, \ 10 | binarizer, \ 11 | min_max_scaler, \ 12 | max_abs_scaler, \ 13 | normalizer, \ 14 | robust_scaler, \ 15 | standard_scaler, \ 16 | quantile_transformer, \ 17 | power_transformer, \ 18 | gaussian_nb 19 | 20 | import numpy as np 21 | 22 | 23 | class TestDataPreprocessing(StandardPreprocessingTest): 24 | """ 25 | Class for _data preprocessing testing 26 | """ 27 | @TrialsExceptionHandler 28 | def test_power_transformer(self): 29 | """ 30 | Instantiate gaussian_nb hyperopt estimator model 31 | define preprocessor power_transformer 32 | fit and score model on positive data 33 | """ 34 | model = HyperoptEstimator( 35 | classifier=gaussian_nb("classifier"), 36 | preprocessing=[power_transformer("preprocessing")], 37 | algo=rand.suggest, 38 | trial_timeout=10.0, 39 | max_evals=5, 40 | ) 41 | model.fit(np.abs(self.X_train), self.Y_train) 42 | model.score(np.abs(self.X_test), self.Y_test) 43 | 44 | test_power_transformer.__name__ = f"test_{power_transformer.__name__}" 45 | 46 | 47 | # List of preprocessors to test 48 | preprocessors = [ 49 | binarizer, 50 | min_max_scaler, 51 | max_abs_scaler, 52 | normalizer, 53 | robust_scaler, 54 | standard_scaler, 55 | quantile_transformer 56 | ] 57 | 58 | 59 | generate_preprocessor_attributes( 60 | TestClass=TestDataPreprocessing, 61 | preprocessor_list=preprocessors, 62 | classifier=gaussian_nb, 63 | ) 64 | 65 | 66 | if __name__ == '__main__': 67 | unittest.main() 68 | -------------------------------------------------------------------------------- /tests/test_components/test_preprocessing/test_discretization.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from tests.utils import \ 4 | StandardPreprocessingTest, \ 5 | generate_preprocessor_attributes 6 | from hpsklearn import \ 7 | k_bins_discretizer, \ 8 | gaussian_nb 9 | 10 | 11 | class TestDiscretizationPreprocessing(StandardPreprocessingTest): 12 | """ 13 | Class for _discretization preprocessing testing 14 | """ 15 | 16 | 17 | generate_preprocessor_attributes( 18 | TestClass=TestDiscretizationPreprocessing, 19 | preprocessor_list=[k_bins_discretizer], 20 | classifier=gaussian_nb, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_preprocessing/test_encoders.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import numpy as np 3 | 4 | from hyperopt import rand 5 | 6 | from tests.utils import \ 7 | StandardPreprocessingTest, \ 8 | TrialsExceptionHandler 9 | from hpsklearn import HyperoptEstimator, \ 10 | one_hot_encoder, \ 11 | ordinal_encoder, \ 12 | multinomial_nb 13 | 14 | 15 | class TestEncodersPreprocessing(StandardPreprocessingTest): 16 | """ 17 | Class for _encoders preprocessing testing 18 | """ 19 | 20 | 21 | preprocessors = [ 22 | one_hot_encoder, 23 | ordinal_encoder, 24 | ] 25 | 26 | 27 | def create_preprocessing_function(pre_fn): 28 | """ 29 | Instantiate multinomial_nb hyperopt estimator model 30 | 'pre_fn' regards the preprocessor 31 | fit and score model 32 | """ 33 | 34 | @TrialsExceptionHandler 35 | def test_preprocessor(self): 36 | model = HyperoptEstimator( 37 | classifier=multinomial_nb("classifier"), 38 | preprocessing=[pre_fn("preprocessing")], 39 | algo=rand.suggest, 40 | trial_timeout=5.0, 41 | max_evals=5, 42 | ) 43 | model.fit(np.abs(np.round(self.X_test).astype(np.int64)), self.Y_test) 44 | model.score(np.abs(np.round(self.X_test).astype(np.int64)), self.Y_test) 45 | 46 | test_preprocessor.__name__ = f"test_{pre_fn.__name__}" 47 | return test_preprocessor 48 | 49 | 50 | # Create unique _data preprocessing algorithms 51 | # with test_ prefix so that unittest can see them 52 | for pre in preprocessors: 53 | setattr( 54 | TestEncodersPreprocessing, 55 | f"test_{pre.__name__}", 56 | create_preprocessing_function(pre) 57 | ) 58 | 59 | 60 | if __name__ == '__main__': 61 | unittest.main() 62 | -------------------------------------------------------------------------------- /tests/test_components/test_preprocessing/test_polynomial.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from tests.utils import \ 4 | StandardPreprocessingTest, \ 5 | generate_preprocessor_attributes 6 | from hpsklearn import \ 7 | polynomial_features, \ 8 | spline_transformer, \ 9 | gaussian_nb 10 | 11 | 12 | class TestPolynomialPreprocessing(StandardPreprocessingTest): 13 | """ 14 | Class for _polynomial preprocessing testing 15 | """ 16 | 17 | 18 | # List of preprocessors to test 19 | preprocessors = [ 20 | polynomial_features, 21 | spline_transformer 22 | ] 23 | 24 | 25 | generate_preprocessor_attributes( 26 | TestClass=TestPolynomialPreprocessing, 27 | preprocessor_list=preprocessors, 28 | classifier=gaussian_nb, 29 | ) 30 | 31 | 32 | if __name__ == '__main__': 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /tests/test_components/test_semi_supervised/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_semi_supervised/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_semi_supervised/test_label_propagation.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | label_propagation, \ 5 | label_spreading 6 | from tests.utils import \ 7 | StandardClassifierTest, \ 8 | generate_attributes 9 | 10 | 11 | class TestLabelPropagationClassifier(StandardClassifierTest): 12 | """ 13 | Class for _label_propagation classification testing 14 | """ 15 | 16 | 17 | generate_attributes( 18 | TestClass=TestLabelPropagationClassifier, 19 | fn_list=[label_propagation, label_spreading], 20 | is_classif=True, 21 | ) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /tests/test_components/test_svm/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_svm/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_svm/test_svm_classes.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hyperopt import rand 4 | from hpsklearn import \ 5 | HyperoptEstimator, \ 6 | linear_svc, \ 7 | linear_svr, \ 8 | nu_svc, \ 9 | nu_svr, \ 10 | one_class_svm, \ 11 | svc, \ 12 | svr 13 | from tests.utils import \ 14 | StandardRegressorTest, \ 15 | StandardClassifierTest, \ 16 | IrisTest, \ 17 | generate_attributes, \ 18 | TrialsExceptionHandler 19 | from sklearn.metrics import accuracy_score 20 | 21 | 22 | class TestSVMClassifier(StandardClassifierTest): 23 | """ 24 | Class for _classes classification testing 25 | """ 26 | 27 | 28 | class TestSVMRegression(StandardRegressorTest): 29 | """ 30 | Class for _classes regression testing 31 | """ 32 | 33 | 34 | class TestOneClassSVM(IrisTest): 35 | """ 36 | Class for one_class_svm testing 37 | """ 38 | @TrialsExceptionHandler 39 | def test_one_class_svm(self): 40 | """ 41 | Instantiate one_class_svm hyperopt estimator model 42 | fit and score model 43 | """ 44 | model = HyperoptEstimator( 45 | regressor=one_class_svm(name="one_class_svm_regressor"), 46 | preprocessing=[], 47 | algo=rand.suggest, 48 | trial_timeout=10.0, 49 | max_evals=5, 50 | ) 51 | model.fit(self.X_train, self.Y_train) 52 | accuracy_score(y_true=self.Y_test, y_pred=model.predict(self.X_test)) 53 | 54 | test_one_class_svm.__name__ = f"test_{one_class_svm.__name__}" 55 | 56 | 57 | generate_attributes( 58 | TestClass=TestSVMClassifier, 59 | fn_list=[linear_svc, nu_svc, svc], 60 | is_classif=True, 61 | ) 62 | 63 | 64 | generate_attributes( 65 | TestClass=TestSVMRegression, 66 | fn_list=[linear_svr, nu_svr, svr], 67 | is_classif=False, 68 | ) 69 | 70 | 71 | if __name__ == '__main__': 72 | unittest.main() 73 | -------------------------------------------------------------------------------- /tests/test_components/test_tree/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_components/test_tree/__init__.py -------------------------------------------------------------------------------- /tests/test_components/test_tree/test_tree_classes.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import numpy as np 3 | 4 | from hyperopt import rand 5 | 6 | from hpsklearn import \ 7 | HyperoptEstimator, \ 8 | decision_tree_classifier, \ 9 | decision_tree_regressor, \ 10 | extra_tree_classifier, \ 11 | extra_tree_regressor 12 | from tests.utils import \ 13 | StandardClassifierTest, \ 14 | StandardRegressorTest, \ 15 | generate_attributes, \ 16 | TrialsExceptionHandler 17 | 18 | 19 | class TestTreeClassification(StandardClassifierTest): 20 | """ 21 | Class for tree._classes classification testing 22 | """ 23 | 24 | 25 | class TestTreeRegression(StandardRegressorTest): 26 | """ 27 | Class for tree._classes regression testing 28 | """ 29 | @TrialsExceptionHandler 30 | def test_poisson_function(self): 31 | """ 32 | Instantiate decision tree regressor hyperopt estimator model 33 | define 'criterion' = 'poisson' 34 | fit and score model 35 | """ 36 | model = HyperoptEstimator( 37 | regressor=decision_tree_regressor(name="poisson_regressor", 38 | criterion="poisson"), 39 | preprocessing=[], 40 | algo=rand.suggest, 41 | trial_timeout=10.0, 42 | max_evals=5, 43 | ) 44 | model.fit(np.abs(self.X_train), np.abs(self.Y_train)) 45 | model.score(np.abs(self.X_test), np.abs(self.Y_test)) 46 | 47 | test_poisson_function.__name__ = f"test_{decision_tree_regressor.__name__}" 48 | 49 | 50 | generate_attributes( 51 | TestClass=TestTreeClassification, 52 | fn_list=[decision_tree_classifier, extra_tree_classifier], 53 | is_classif=True 54 | ) 55 | 56 | 57 | generate_attributes( 58 | TestClass=TestTreeRegression, 59 | fn_list=[decision_tree_regressor, extra_tree_regressor], 60 | is_classif=False 61 | ) 62 | 63 | 64 | if __name__ == '__main__': 65 | unittest.main() 66 | -------------------------------------------------------------------------------- /tests/test_components/test_vkmeans.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from tests.utils import \ 4 | StandardPreprocessingTest, \ 5 | generate_preprocessor_attributes 6 | from hpsklearn import colkmeans, \ 7 | gaussian_nb 8 | 9 | 10 | class TestColkmeansPreprocessing(StandardPreprocessingTest): 11 | """ 12 | Class for _data preprocessing testing 13 | """ 14 | 15 | 16 | generate_preprocessor_attributes( 17 | TestClass=TestColkmeansPreprocessing, 18 | preprocessor_list=[colkmeans], 19 | classifier=gaussian_nb, 20 | ) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /tests/test_components/test_xgboost.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from hpsklearn import \ 4 | xgboost_classification, \ 5 | xgboost_regression 6 | from tests.utils import \ 7 | StandardRegressorTest, \ 8 | StandardClassifierTest, \ 9 | generate_attributes 10 | 11 | 12 | class TestXGBoostClassifier(StandardClassifierTest): 13 | """ 14 | Class for _xgboost classification testing 15 | """ 16 | 17 | 18 | class TestXGBoostRegression(StandardRegressorTest): 19 | """ 20 | Class for _xgboost regression testing 21 | """ 22 | 23 | 24 | generate_attributes( 25 | TestClass=TestXGBoostClassifier, 26 | fn_list=[xgboost_classification], 27 | is_classif=True, 28 | ) 29 | 30 | 31 | generate_attributes( 32 | TestClass=TestXGBoostRegression, 33 | fn_list=[xgboost_regression], 34 | is_classif=False, 35 | ) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /tests/test_estimator/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyperopt/hyperopt-sklearn/54e079981d1f8d0db77aced7457afc54acfa7db8/tests/test_estimator/__init__.py -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | minversion = 3.7.0 3 | envlist = py311, py312, py313, flake8 4 | isolated_build = true 5 | 6 | [gh-actions] 7 | python = 8 | 3.11: py311 9 | 3.12: py312 10 | 3.13: py313, flake8 11 | 12 | [testenv] 13 | setenv = 14 | PYTHONPATH = {toxinidir} 15 | deps = 16 | -r{toxinidir}/requirements_dev.txt 17 | commands = 18 | coverage run -m unittest discover 19 | coverage report -m 20 | 21 | [testenv:flake8] 22 | basepython = python3.13 23 | deps = flake8 24 | commands = flake8 --max-line-length=120 --ignore=F401,W504 hpsklearn tests 25 | --------------------------------------------------------------------------------