示例#1
0
    def test___init__(self, mock_one_hot_encoder):
        """Test that during instantiation we create a OneHotEncoder and we fit it with the given
        choices."""
        # setup
        choices = ['cat', 'dog', 'parrot']
        encoder_instance = MagicMock()
        mock_one_hot_encoder.return_value = encoder_instance

        # run
        instance = CategoricalHyperParam(choices=choices)

        # assert
        self.assertEqual(instance.choices, choices)
        self.assertEqual(instance.default, 'cat')

        # TODO: Fix / reimplmement assert_called_with_np
        # expected_encoder_calls = [
        #     call(categories=[np.array(['cat', 'dog', 'parrot'], dtype=object)], sparse=True)
        # ]

        # assert_called_with_np_array(
        #     mock_one_hot_encoder.call_args_list,
        #     expected_encoder_calls
        # )
        expected_encoder_fit_call = [call(np.array(choices).reshape(-1, 1))]
        assert_called_with_np_array(encoder_instance.fit.call_args_list, expected_encoder_fit_call)
示例#2
0
    def test___init__(self, mock_one_hot_encoder):
        """Test that during instantiation we create a OneHotEncoder and we fit it with the given
        choices."""
        # setup
        choices = ['cat', 'dog', 'parrot']
        encoder_instance = MagicMock()
        mock_one_hot_encoder.return_value = encoder_instance

        # run
        instance = CategoricalHyperParam(choices=choices)

        # assert
        expected_encoder_fit_call = [call(np.array(choices).reshape(-1, 1))]
        mock_one_hot_encoder.assert_called_once_with(sparse=False)
        self.assertEqual(instance.choices, choices)
        assert_called_with_np_array(encoder_instance.fit.call_args_list,
                                    expected_encoder_fit_call)
示例#3
0
    def test_transform_list_of_invalid_dicts(self):
        """Test transform method with a list of dictionaries where one of them does not have
        the categorical value."""

        # setup
        self.bhp.transform.return_value = [[1], [0]]

        # Here we create a CHP so we can raise an value error as there will be a NaN inside the
        # pandas.DataFrame.
        self.chp = CategoricalHyperParam(['cat', 'dog'])
        self.ihp.transform.return_value = [[1], [1]]

        values_list_dict = [
            {'bhp': True, 'ihp': 2},
            {'bhp': False, 'chp': 'cat', 'ihp': 3}
        ]

        # run / assert
        with self.assertRaises(ValueError):
            self.instance.transform(values_list_dict)
示例#4
0
def extract_tunable_hyperparams(pipeline):

    # state tunable hyperparameters for this pipeline
    tunable_hyperparams = {}

    # obtain all the hyperparameters
    hyperparams = pipeline.get_all_hyperparams()

    for step_num, (step_hyperparams,
                   step) in enumerate(zip(hyperparams, pipeline.steps)):

        for name, hyperparam in step_hyperparams.items():
            # all logic goes here
            if TUNING_PARAMETER not in hyperparam.semantic_types:
                continue

            btb_hyperparam = None
            default = step.hyperparams.get(name,
                                           {}).get('data',
                                                   hyperparam.get_default())

            if isinstance(hyperparam, Bounded):
                btb_hyperparam = _generate_bounded_hyperparam(
                    hyperparam, default)

            elif isinstance(hyperparam, Enumeration):
                btb_hyperparam = CategoricalHyperParam(
                    choices=hyperparam.values, default=default)

            elif isinstance(hyperparam, UniformBool):
                btb_hyperparam = BooleanHyperParam(default=default)

            elif isinstance(hyperparam, (UniformInt, Uniform)):
                btb_hyperparam = _generate_bounded_hyperparam(
                    hyperparam, default)

            if btb_hyperparam is not None:
                tunable_hyperparams[(str(step_num),
                                     hyperparam.name)] = btb_hyperparam

    return tunable_hyperparams
示例#5
0
 def setUpClass(cls):
     cls.instance = CategoricalHyperParam(choices=['Cat', 'Dog', 'Horse', 'Tiger'])
示例#6
0
    def from_dict(cls, dict_hyperparams):
        """Create an instance from a dictionary containing information over hyperparameters.

        Class method that creates an instance from a dictionary that describes the type of a
        hyperparameter, the range or values that this can have and the default value of the
        hyperparameter.

        Args:
            dict_hyperparams (dict):
                A python dictionary containing as `key` the given name for the hyperparameter and
                as value a dictionary containing the following keys:

                    - Type (str):
                        ``bool`` for ``BoolHyperParam``, ``int`` for ``IntHyperParam``, ``float``
                        for ``FloatHyperParam``, ``str`` for ``CategoricalHyperParam``.

                    - Range or Values (list):
                        Range / values that this hyperparameter can take, in case of
                        ``CategoricalHyperParam`` those will be used as the ``choices``, for
                        ``NumericalHyperParams`` the ``min`` value will be used as the minimum
                        value and the ``max`` value will be used as the ``maximum`` value.

                    - Default (str, bool, int, float or None):
                        The default value for the hyperparameter.

        Returns:
            Tunable:
                A ``Tunable`` instance with the given hyperparameters.
        """

        if not isinstance(dict_hyperparams, dict):
            raise TypeError('Hyperparams must be a dictionary.')

        hyperparams = {}

        for name, hyperparam in dict_hyperparams.items():
            hp_type = hyperparam['type']
            hp_default = hyperparam.get('default')

            if hp_type == 'int':
                hp_range = hyperparam.get('range') or hyperparam.get('values')
                hp_min = min(hp_range) if hp_range else None
                hp_max = max(hp_range) if hp_range else None
                hp_instance = IntHyperParam(min=hp_min,
                                            max=hp_max,
                                            default=hp_default)

            elif hp_type == 'float':
                hp_range = hyperparam.get('range') or hyperparam.get('values')
                hp_min = min(hp_range)
                hp_max = max(hp_range)
                hp_instance = FloatHyperParam(min=hp_min,
                                              max=hp_max,
                                              default=hp_default)

            elif hp_type == 'bool':
                hp_instance = BooleanHyperParam(default=hp_default)

            elif hp_type == 'str':
                hp_choices = hyperparam.get('range') or hyperparam.get(
                    'values')
                hp_instance = CategoricalHyperParam(choices=hp_choices,
                                                    default=hp_default)

            hyperparams[name] = hp_instance

        return cls(hyperparams)