本文整理汇总了Python中skopt.space.Real方法的典型用法代码示例。如果您正苦于以下问题:Python space.Real方法的具体用法?Python space.Real怎么用?Python space.Real使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skopt.space
的用法示例。
在下文中一共展示了space.Real方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dimensions_names
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [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
示例2: test_real
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_real():
a = Real(1, 25)
for i in range(50):
r = a.rvs(random_state=i)
check_limits(r, 1, 25)
assert r in a
random_values = a.rvs(random_state=0, n_samples=10)
assert len(random_values) == 10
assert_array_equal(a.transform(random_values), random_values)
assert_array_equal(a.inverse_transform(random_values), random_values)
log_uniform = Real(10**-5, 10**5, prior="log-uniform")
assert log_uniform != Real(10**-5, 10**5)
for i in range(50):
random_val = log_uniform.rvs(random_state=i)
check_limits(random_val, 10**-5, 10**5)
random_values = log_uniform.rvs(random_state=0, n_samples=10)
assert len(random_values) == 10
transformed_vals = log_uniform.transform(random_values)
assert_array_equal(transformed_vals, np.log10(random_values))
assert_array_equal(
log_uniform.inverse_transform(transformed_vals), random_values)
示例3: test_dimension_name
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [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])]
示例4: _knob_to_dimension
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [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__)
示例5: optimize_threshold
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def optimize_threshold(self, xtrain, ytrain, xval, yval):
ytrain_pred = self.predict_labels(xtrain, raw_prob=True)
yval_pred = self.predict_labels(xval, raw_prob=True)
self.opt_threshold = 0.5
ytrain_pred_labels = self.get_labels_from_prob(ytrain_pred, threshold=self.opt_threshold)
yval_pred_labels = self.get_labels_from_prob(yval_pred, threshold=self.opt_threshold)
train_f1_score = f1_score(ytrain_pred_labels, ytrain)
val_f1_score = f1_score(yval_pred_labels, yval)
print(f"train f1 score: {train_f1_score}, val f1 score: {val_f1_score}")
f1_train_partial = partial(self.get_f1score_for_optimization, y_true=ytrain.copy(), y_pred=ytrain_pred.copy(), ismin=True)
n_searches = 50
dim_0 = Real(low=0.2, high=0.8, name='dim_0')
dimensions = [dim_0]
search_result = gp_minimize(func=f1_train_partial,
dimensions=dimensions,
acq_func='gp_hedge', # Expected Improvement.
n_calls=n_searches,
# n_jobs=n_cpu,
verbose=False)
self.opt_threshold = search_result.x
if isinstance(self.opt_threshold,list):
self.opt_threshold = self.opt_threshold[0]
self.optimum_threshold_filename = f"model_threshold_{'_'.join(str(v) for k, v in model_params.items())}.npy"
np.save(os.path.join(f"{model_params['model_save_dir']}",self.optimum_threshold_filename), self.opt_threshold)
train_f1_score = self.get_f1score_for_optimization(self.opt_threshold, y_true=ytrain, y_pred=ytrain_pred)
val_f1_score = self.get_f1score_for_optimization(self.opt_threshold, y_true=yval, y_pred=yval_pred )
print(f"optimized train f1 score: {train_f1_score}, optimized val f1 score: {val_f1_score}")
示例6: _fit_svc
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def _fit_svc(n_jobs=1, n_points=1, cv=None):
"""
Utility function to fit a larger classification task with SVC
"""
X, y = make_classification(n_samples=1000, n_features=20, n_redundant=0,
n_informative=18, random_state=1,
n_clusters_per_class=1)
opt = BayesSearchCV(
SVC(),
{
'C': Real(1e-3, 1e+3, prior='log-uniform'),
'gamma': Real(1e-3, 1e+1, prior='log-uniform'),
'degree': Integer(1, 3),
},
n_jobs=n_jobs, n_iter=11, n_points=n_points, cv=cv,
random_state=42,
)
opt.fit(X, y)
assert opt.score(X, y) > 0.9
opt2 = BayesSearchCV(
SVC(),
{
'C': Real(1e-3, 1e+3, prior='log-uniform'),
'gamma': Real(1e-3, 1e+1, prior='log-uniform'),
'degree': Integer(1, 3),
},
n_jobs=n_jobs, n_iter=11, n_points=n_points, cv=cv,
random_state=42,
)
opt2.fit(X, y)
assert opt.score(X, y) == opt2.score(X, y)
示例7: test_searchcv_callback
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_searchcv_callback():
# Test whether callback is used in BayesSearchCV and
# whether is can be used to interrupt the search loop
X, y = load_iris(True)
opt = BayesSearchCV(
DecisionTreeClassifier(),
{
'max_depth': [3], # additional test for single dimension
'min_samples_split': Real(0.1, 0.9),
},
n_iter=5
)
total_iterations = [0]
def callback(opt_result):
# this simply counts iterations
total_iterations[0] += 1
# break the optimization loop at some point
if total_iterations[0] > 2:
return True # True == stop optimization
return False
opt.fit(X, y, callback=callback)
assert total_iterations[0] == 3
# test whether final model was fit
opt.score(X, y)
示例8: test_searchcv_total_iterations
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_searchcv_total_iterations():
# Test the total iterations counting property of BayesSearchCV
opt = BayesSearchCV(
DecisionTreeClassifier(),
[
({'max_depth': (1, 32)}, 10), # 10 iterations here
{'min_samples_split': Real(0.1, 0.9)} # 5 (default) iters here
],
n_iter=5
)
assert opt.total_iterations == 10 + 5
示例9: test_constant_liar_runs
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_constant_liar_runs(strategy, surrogate, acq_func):
"""
Tests whether the optimizer runs properly during the random
initialization phase and beyond
Parameters
----------
* `strategy` [string]:
Name of the strategy to use during optimization.
* `surrogate` [scikit-optimize surrogate class]:
A class of the scikit-optimize surrogate used in Optimizer.
"""
optimizer = Optimizer(
base_estimator=surrogate(),
dimensions=[Real(-5.0, 10.0), Real(0.0, 15.0)],
acq_func=acq_func,
acq_optimizer='sampling',
random_state=0
)
# test arguments check
assert_raises(ValueError, optimizer.ask, {"strategy": "cl_maen"})
assert_raises(ValueError, optimizer.ask, {"n_points": "0"})
assert_raises(ValueError, optimizer.ask, {"n_points": 0})
for i in range(n_steps):
x = optimizer.ask(n_points=n_points, strategy=strategy)
# check if actually n_points was generated
assert_equal(len(x), n_points)
if "ps" in acq_func:
optimizer.tell(x, [[branin(v), 1.1] for v in x])
else:
optimizer.tell(x, [branin(v) for v in x])
示例10: test_all_points_different
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_all_points_different(strategy, surrogate):
"""
Tests whether the parallel optimizer always generates
different points to evaluate.
Parameters
----------
* `strategy` [string]:
Name of the strategy to use during optimization.
* `surrogate` [scikit-optimize surrogate class]:
A class of the scikit-optimize surrogate used in Optimizer.
"""
optimizer = Optimizer(
base_estimator=surrogate(),
dimensions=[Real(-5.0, 10.0), Real(0.0, 15.0)],
acq_optimizer='sampling',
random_state=1
)
tolerance = 1e-3 # distance above which points are assumed same
for i in range(n_steps):
x = optimizer.ask(n_points, strategy)
optimizer.tell(x, [branin(v) for v in x])
distances = pdist(x)
assert all(distances > tolerance)
示例11: test_reproducible_runs
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_reproducible_runs(strategy, surrogate):
# two runs of the optimizer should yield exactly the same results
optimizer = Optimizer(
base_estimator=surrogate(random_state=1),
dimensions=[Real(-5.0, 10.0), Real(0.0, 15.0)],
acq_optimizer='sampling',
random_state=1
)
points = []
for i in range(n_steps):
x = optimizer.ask(n_points, strategy)
points.append(x)
optimizer.tell(x, [branin(v) for v in x])
# the x's should be exaclty as they are in `points`
optimizer = Optimizer(
base_estimator=surrogate(random_state=1),
dimensions=[Real(-5.0, 10.0), Real(0.0, 15.0)],
acq_optimizer='sampling',
random_state=1
)
for i in range(n_steps):
x = optimizer.ask(n_points, strategy)
assert points[i] == x
optimizer.tell(x, [branin(v) for v in x])
示例12: test_dimensions
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_dimensions():
check_dimension(Real, (1., 4.), 2.251066014107722)
check_dimension(Real, (1, 4), 2.251066014107722)
check_dimension(Integer, (1, 4), 2)
check_dimension(Integer, (1., 4.), 2)
check_dimension(Integer, (1, 4), 2)
check_categorical(("a", "b", "c", "d"), "b")
check_categorical((1., 2., 3., 4.), 2.)
check_categorical((1, 2, 3, 4), 2)
示例13: test_real_log_sampling_in_bounds
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_real_log_sampling_in_bounds():
dim = Real(low=1, high=32, prior='log-uniform', transform='normalize')
# round trip a value that is within the bounds of the space
#
# x = dim.inverse_transform(dim.transform(31.999999999999999))
for n in (32., 31.999999999999999):
round_tripped = dim.inverse_transform(dim.transform([n]))
assert np.allclose([n], round_tripped)
assert n in dim
assert round_tripped in dim
示例14: test_real_bounds
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_real_bounds():
# should give same answer as using check_limits() but this is easier
# to read
a = Real(1., 2.1)
assert 0.99 not in a
assert 1. in a
assert 2.09 in a
assert 2.1 in a
assert np.nextafter(2.1, 3.) not in a
示例15: test_valid_transformation
# 需要导入模块: from skopt import space [as 别名]
# 或者: from skopt.space import Real [as 别名]
def test_valid_transformation():
check_valid_transformation(Integer)
check_valid_transformation(Real)