当前位置: 首页>>代码示例>>Python>>正文


Python space.Categorical方法代码示例

本文整理汇总了Python中skopt.space.Categorical方法的典型用法代码示例。如果您正苦于以下问题:Python space.Categorical方法的具体用法?Python space.Categorical怎么用?Python space.Categorical使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在skopt.space的用法示例。


在下文中一共展示了space.Categorical方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_dimensions_names

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_dimensions_names():
    from skopt.space import Real, Categorical, Integer
    # create search space and optimizer
    space = [Real(0, 1, name='real'),
             Categorical(['a', 'b', 'c'], name='cat'),
             Integer(0, 1, name='int')]
    opt = Optimizer(space, n_initial_points=1)
    # result of the optimizer missing dimension names
    result = opt.tell([(0.5, 'a', 0.5)], [3])
    names = []
    for d in result.space.dimensions:
        names.append(d.name)
    assert len(names) == 3
    assert "real" in names
    assert "cat" in names
    assert "int" in names
    assert None not in names 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:19,代码来源:test_optimizer.py

示例2: test_categorical_only

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_categorical_only():
    from skopt.space import Categorical
    cat1 = Categorical([2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
    cat2 = Categorical([2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

    opt = Optimizer([cat1, cat2])
    for n in range(15):
        x = opt.ask()
        res = opt.tell(x, 12 * n)
    assert len(res.x_iters) == 15
    next_x = opt.ask(n_points=4)
    assert len(next_x) == 4

    cat3 = Categorical(["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"])
    cat4 = Categorical(["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"])

    opt = Optimizer([cat3, cat4])
    for n in range(15):
        x = opt.ask()
        res = opt.tell(x, 12 * n)
    assert len(res.x_iters) == 15
    next_x = opt.ask(n_points=4)
    assert len(next_x) == 4 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:25,代码来源:test_optimizer.py

示例3: test_categorical_only2

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_categorical_only2():
    from numpy import linalg
    from skopt.space import Categorical
    from skopt.learning import GaussianProcessRegressor
    space = [Categorical([1, 2, 3]), Categorical([4, 5, 6])]
    opt = Optimizer(space,
                    base_estimator=GaussianProcessRegressor(alpha=1e-7),
                    acq_optimizer='lbfgs',
                    n_initial_points=10,
                    n_jobs=2)

    next_x = opt.ask(n_points=4)
    assert len(next_x) == 4
    opt.tell(next_x, [linalg.norm(x) for x in next_x])
    next_x = opt.ask(n_points=4)
    assert len(next_x) == 4
    opt.tell(next_x, [linalg.norm(x) for x in next_x])
    next_x = opt.ask(n_points=4)
    assert len(next_x) == 4 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:21,代码来源:test_optimizer.py

示例4: test_set_get_transformer

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_set_get_transformer():
    # can you pass a Space instance to the Space constructor?
    space = Space([(0.0, 1.0), (-5, 5),
                   ("a", "b", "c"), (1.0, 5.0, "log-uniform"), ("e", "f")])

    transformer = space.get_transformer()
    assert_array_equal(["identity", "identity", "onehot",
                        "identity", "onehot"], transformer)
    space.set_transformer("normalize")
    transformer = space.get_transformer()
    assert_array_equal(["normalize"] * 5, transformer)
    space.set_transformer(transformer)
    assert_array_equal(transformer, space.get_transformer())

    space.set_transformer_by_type("label", Categorical)
    assert space.dimensions[2].transform(["a"]) == [0] 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:18,代码来源:test_space.py

示例5: test_dimension_name

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_dimension_name():
    notnames = [1, 1., True]
    for n in notnames:
        with pytest.raises(ValueError) as exc:
            real = Real(1, 2, name=n)
            assert("Dimension's name must be either string or"
                   "None." == exc.value.args[0])
    s = Space([Real(1, 2, name="a"),
               Integer(1, 100, name="b"),
               Categorical(["red, blue"], name="c")])
    assert s["a"] == (0, s.dimensions[0])
    assert s["a", "c"] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[["a", "c"]] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[("a", "c")] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[0] == (0, s.dimensions[0])
    assert s[0, "c"] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[0, 2] == [(0, s.dimensions[0]), (2, s.dimensions[2])] 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:19,代码来源:test_space.py

示例6: _knob_to_dimension

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def _knob_to_dimension(knob):
    if isinstance(knob, CategoricalKnob):
        return Categorical([x.value for x in knob.values])
    elif isinstance(knob, IntegerKnob):
        return Integer(knob.value_min, knob.value_max)
    elif isinstance(knob, FloatKnob):
        if knob.is_exp:
            # Avoid error in skopt when low/high are 0
            value_min = knob.value_min if knob.value_min != 0 else 1e-12
            value_max = knob.value_max if knob.value_max != 0 else 1e-12
            return Real(value_min, value_max, 'log-uniform')
        else:
            return Real(knob.value_min, knob.value_max, 'uniform')
    else:
        raise UnsupportedKnobError(knob.__class__) 
开发者ID:nginyc,项目名称:rafiki,代码行数:17,代码来源:skopt.py

示例7: test_searchcv_rank

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_searchcv_rank():
    """
    Test whether results of BayesSearchCV can be reproduced with a fixed
    random state.
    """

    X, y = load_iris(True)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, train_size=0.75, random_state=0
    )

    random_state = 42

    opt = BayesSearchCV(
        SVC(random_state=random_state),
        {
            'C': Real(1e-6, 1e+6, prior='log-uniform'),
            'gamma': Real(1e-6, 1e+1, prior='log-uniform'),
            'degree': Integer(1, 8),
            'kernel': Categorical(['linear', 'poly', 'rbf']),
        },
        n_iter=11, random_state=random_state, return_train_score=True
    )

    opt.fit(X_train, y_train)
    results = opt.cv_results_

    test_rank = np.asarray(rankdata(-np.array(results["mean_test_score"]),
                                    method='min'), dtype=np.int32)
    train_rank = np.asarray(rankdata(-np.array(results["mean_train_score"]),
                                     method='min'), dtype=np.int32)

    assert_array_equal(np.array(results['rank_test_score']), test_rank)
    assert_array_equal(np.array(results['rank_train_score']), train_rank) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:36,代码来源:test_searchcv.py

示例8: check_categorical

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def check_categorical(vals, random_val):
    x = Categorical(vals)
    assert_equal(x, Categorical(vals))
    assert x != Categorical(vals[:-1] + ("zzz",))
    assert_equal(x.rvs(random_state=1), random_val) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:7,代码来源:test_space.py

示例9: test_categorical_transform

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_categorical_transform():
    categories = ["apple", "orange", "banana", None, True, False, 3]
    cat = Categorical(categories)

    apple = [1., 0., 0., 0., 0., 0., 0.]
    orange = [0., 1.0, 0.0, 0.0, 0., 0., 0.]
    banana = [0., 0., 1., 0., 0., 0., 0.]
    none = [0., 0., 0., 1., 0., 0., 0.]
    true = [0., 0., 0., 0., 1., 0., 0.]
    false = [0., 0., 0., 0., 0., 1., 0.]
    three = [0., 0., 0., 0., 0., 0., 1.]

    assert_equal(cat.transformed_size, 7)
    assert_equal(cat.transformed_size, cat.transform(["apple"]).size)
    assert_array_equal(
        cat.transform(categories),
        [apple, orange, banana, none, true, false, three]
        )
    assert_array_equal(cat.transform(["apple", "orange"]), [apple, orange])
    assert_array_equal(cat.transform(["apple", "banana"]), [apple, banana])
    assert_array_equal(cat.inverse_transform([apple, orange]),
                       ["apple", "orange"])
    assert_array_equal(cat.inverse_transform([apple, banana]),
                       ["apple", "banana"])
    ent_inverse = cat.inverse_transform(
        [apple, orange, banana, none, true, false, three])
    assert_array_equal(ent_inverse, categories) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:29,代码来源:test_space.py

示例10: test_categorical_transform_binary

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_categorical_transform_binary():
    categories = ["apple", "orange"]
    cat = Categorical(categories)

    apple = [0.]
    orange = [1.]

    assert_equal(cat.transformed_size, 1)
    assert_equal(cat.transformed_size, cat.transform(["apple"]).size)
    assert_array_equal(cat.transform(categories), [apple, orange])
    assert_array_equal(cat.transform(["apple", "orange"]), [apple, orange])
    assert_array_equal(cat.inverse_transform([apple, orange]),
                       ["apple", "orange"])
    ent_inverse = cat.inverse_transform([apple, orange])
    assert_array_equal(ent_inverse, categories) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:17,代码来源:test_space.py

示例11: test_normalize_categorical

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_normalize_categorical():
    categories = ["cat", "dog", "rat"]
    a = Categorical(categories, transform="normalize")
    for i in range(len(categories)):
        assert a.rvs(random_state=i)[0] in categories
    assert a.inverse_transform([0.]) == [categories[0]]
    assert a.inverse_transform([0.5]) == [categories[1]]
    assert a.inverse_transform([1.0]) == [categories[2]]
    assert_array_equal(categories, a.inverse_transform([0., 0.5, 1]))

    categories = [1, 2, 3]
    a = Categorical(categories, transform="normalize")
    assert_array_equal(categories, np.sort(np.unique(a.rvs(100,
                                                           random_state=1))))
    assert_array_equal(categories, a.inverse_transform([0., 0.5, 1.]))

    categories = [1., 2., 3.]
    a = Categorical(categories, transform="normalize")
    assert_array_equal(categories, np.sort(np.unique(a.rvs(100,
                                                           random_state=1))))
    assert_array_equal(categories, a.inverse_transform([0., 0.5, 1.]))

    categories = [1, 2, 3]
    a = Categorical(categories, transform="string")
    a.set_transformer("normalize")
    assert_array_equal(categories, np.sort(np.unique(a.rvs(100,
                                                           random_state=1))))
    assert_array_equal(categories, a.inverse_transform([0., 0.5, 1.])) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:30,代码来源:test_space.py

示例12: test_categorical_identity

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_categorical_identity():
    categories = ["cat", "dog", "rat"]
    cat = Categorical(categories, transform="identity")
    samples = cat.rvs(100)
    assert all([t in categories for t in cat.rvs(100)])
    transformed = cat.transform(samples)
    assert_array_equal(transformed, samples)
    assert_array_equal(samples, cat.inverse_transform(transformed)) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:10,代码来源:test_space.py

示例13: test_categorical_string

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_categorical_string():
    categories = [1, 2, 3]
    categories_transformed = ["1", "2", "3"]
    cat = Categorical(categories, transform="string")
    samples = cat.rvs(100)
    assert all([t in categories for t in cat.rvs(100)])
    transformed = cat.transform(samples)
    assert all([t in categories_transformed for t in transformed])
    assert_array_equal(samples, cat.inverse_transform(transformed)) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:11,代码来源:test_space.py

示例14: test_space_from_yaml

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_space_from_yaml():
    with NamedTemporaryFile(delete=False) as tmp:
        tmp.write(b"""
        Space:
            - Real:
                low: 0.0
                high: 1.0
            - Integer:
                low: -5
                high: 5
            - Categorical:
                categories:
                - a
                - b
                - c
            - Real:
                low: 1.0
                high: 5.0
                prior: log-uniform
            - Categorical:
                categories:
                - e
                - f
        """)
        tmp.flush()

        space = Space([(0.0, 1.0),
                       (-5, 5),
                       ("a", "b", "c"),
                       (1.0, 5.0, "log-uniform"),
                       ("e", "f")])

        space2 = Space.from_yaml(tmp.name)
        assert_equal(space, space2)
        tmp.close()
        os.unlink(tmp.name) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:38,代码来源:test_space.py

示例15: test_purely_categorical_space

# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Categorical [as 别名]
def test_purely_categorical_space():
    # Test reproduces the bug in #908, make sure it doesn't come back
    dims = [Categorical(['a', 'b', 'c']), Categorical(['A', 'B', 'C'])]
    optimizer = Optimizer(dims, n_initial_points=1, random_state=3)

    x = optimizer.ask()
    # before the fix this call raised an exception
    optimizer.tell(x, 1.) 
开发者ID:scikit-optimize,项目名称:scikit-optimize,代码行数:10,代码来源:test_space.py


注:本文中的skopt.space.Categorical方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。