How to use the orion.algo.space.Categorical function in orion

To help you get started, we’ve selected a few orion examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
def test_sample(self, seed):
        """Check whether sampling works correctly."""
        space = Space()
        probs = (0.1, 0.2, 0.3, 0.4)
        categories = ('asdfa', 2, 3, 4)
        dim1 = Categorical('yolo', OrderedDict(zip(categories, probs)), shape=(2, 2))
        space.register(dim1)
        dim2 = Integer('yolo2', 'uniform', -3, 6)
        space.register(dim2)
        dim3 = Real('yolo3', 'norm', 0.9)
        space.register(dim3)

        point = space.sample(seed=seed)
        test_point = [(dim1.sample()[0],
                       dim2.sample()[0],
                       dim3.sample()[0]), ]
        assert len(point) == len(test_point) == 1
        assert len(point[0]) == len(test_point[0]) == 3
        assert np.all(point[0][0] == test_point[0][0])
        assert point[0][1] == test_point[0][1]
        assert point[0][2] == test_point[0][2]
github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
def test_cardinality(self):
        """Check whether space capacity is correct"""
        space = Space()
        probs = (0.1, 0.2, 0.3, 0.4)
        categories = ('asdfa', 2, 3, 4)
        dim = Categorical('yolo', OrderedDict(zip(categories, probs)), shape=2)
        space.register(dim)
        dim = Integer('yolo2', 'uniform', -3, 6)
        space.register(dim)
        dim = Fidelity('epoch', 1, 9, 3)
        space.register(dim)

        assert (4 * 2) * 6 * 1 == space.cardinality

        dim = Integer('yolo3', 'uniform', -3, 2, shape=(3, 1))
        space.register(dim)
        assert (4 * 2) * 6 * 1 * (2 * 3 * 1) == space.cardinality

        dim = Real('yolo4', 'norm', 0.9)
        space.register(dim)
        assert np.inf == space.cardinality
github Epistimio / orion / tests / unittests / core / io / test_space_builder.py View on Github external
def test_build_choices(self, dimbuilder):
        """Create correctly a `Categorical` dimension."""
        dim = dimbuilder.build('yolo', "choices('adfa', 1, 0.3, 'asaga', shape=4)")
        assert isinstance(dim, Categorical)
        assert dim.name == 'yolo'
        assert dim._prior_name == 'Distribution'
        assert isinstance(dim.prior, dists.rv_discrete)

        dim = dimbuilder.build('yolo', "choices(['adfa', 1])")
        assert isinstance(dim, Categorical)
        assert dim.name == 'yolo'
        assert dim._prior_name == 'Distribution'
        assert isinstance(dim.prior, dists.rv_discrete)

        dim = dimbuilder.build('yolo2', "choices({'adfa': 0.1, 3: 0.4, 5: 0.5})")
        assert isinstance(dim, Categorical)
        assert dim.name == 'yolo2'
        assert dim._prior_name == 'Distribution'
        assert isinstance(dim.prior, dists.rv_discrete)
github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
def test_probabilities_are_ok(self, seed):
        """Test that the probabilities given are legit using law of big numbers."""
        bins = defaultdict(int)
        probs = (0.1, 0.2, 0.3, 0.4)
        categories = ('asdfa', '2', '3', '4')
        categories = OrderedDict(zip(categories, probs))
        dim = Categorical('yolo', categories)
        for _ in range(500):
            sample = dim.sample(seed=seed)[0]
            bins[sample] += 1
        for keys in bins.keys():
            bins[keys] /= float(500)
        for key, value in categories.items():
            assert abs(bins[key] - value) < 0.01
github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
def test_that_objects_types_are_ok(self):
        """Check that output samples are of the correct type.

        Don't let numpy mess with their automatic type inference.
        """
        categories = {'asdfa': 0.1, 2: 0.2, 3: 0.3, 'lalala': 0.4}
        dim = Categorical('yolo', categories)

        assert '2' not in dim
        assert 2 in dim
        assert 'asdfa' in dim

        dim = Categorical('yolo', categories, shape=(2,))

        assert ['2', 'asdfa'] not in dim
        assert [2, 'asdfa'] in dim
github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
def test_interval(self):
        """Check that calling `Categorical.interval` raises `RuntimeError`."""
        categories = {'asdfa': 0.1, 2: 0.2, 3: 0.3, 4: 0.4}
        dim = Categorical('yolo', categories, shape=2)

        assert dim.interval() == ('asdfa', 2, 3, 4)
github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
def test_order(self):
        """Test that the same space built twice will have the same ordering."""
        space1 = Space()
        space1.register(Integer('yolo1', 'uniform', -3, 6, shape=(2,)))
        space1.register(Integer('yolo2', 'uniform', -3, 6, shape=(2,)))
        space1.register(Real('yolo3', 'norm', 0.9))
        space1.register(Categorical('yolo4', ('asdfa', 2)))

        space2 = Space()
        space2.register(Integer('yolo1', 'uniform', -3, 6, shape=(2,)))
        space2.register(Real('yolo3', 'norm', 0.9))
        space2.register(Categorical('yolo4', ('asdfa', 2)))
        space2.register(Integer('yolo2', 'uniform', -3, 6, shape=(2,)))

        assert list(space1) == list(space1.keys())
        assert list(space2) == list(space2.keys())
        assert list(space1.values()) == list(space2.values())
        assert list(space1.items()) == list(space2.items())
        assert list(space1.keys()) == list(space2.keys())
        assert list(space1.values()) == list(space2.values())
        assert list(space1.items()) == list(space2.items())
github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
assert dim.prior_name == 'reciprocal'

        dim = Dimension('yolo', 'norm', 0.9)
        assert dim.prior_name == 'norm'

        dim = Real('yolo', 'uniform', 1, 2)
        assert dim.prior_name == 'uniform'

        dim = Integer('yolo1', 'uniform', -3, 6)
        assert dim.prior_name == 'int_uniform'

        dim = Integer('yolo1', 'norm', -3, 6)
        assert dim.prior_name == 'int_norm'

        categories = {'asdfa': 0.1, 2: 0.2, 3: 0.3, 'lalala': 0.4}
        dim = Categorical('yolo', categories)
        assert dim.prior_name == 'choices'
github Epistimio / orion / tests / unittests / algo / test_space.py View on Github external
def test_getitem(self):
        """Test getting dimensions from space."""
        space = Space()
        probs = (0.1, 0.2, 0.3, 0.4)
        categories = ('asdfa', 2, 3, 4)
        dim = Categorical('yolo', OrderedDict(zip(categories, probs)), shape=2)
        space.register(dim)
        dim = Integer('yolo2', 'uniform', -3, 6)
        space.register(dim)
        dim = Real('yolo3', 'norm', 0.9)
        space.register(dim)

        assert space['yolo'].type == 'categorical'
        assert space[0].type == 'categorical'

        with pytest.raises(KeyError):
            space['asdf']

        with pytest.raises(IndexError):
            space[3]
github Epistimio / orion / src / orion / core / io / space_builder.py View on Github external
def choices(self, *args, **kwargs):
        """Create a `Categorical` dimension."""
        name = self.name
        try:
            if isinstance(args[0], (dict, list)):
                return Categorical(name, *args, **kwargs)
        except IndexError as exc:
            raise TypeError("Parameter '{}': "
                            "Expected argument with categories.".format(name)) from exc

        return Categorical(name, args, **kwargs)