Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bayes_opt/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ def random_sample(
The samples.
"""
random_state = ensure_rng(random_state)
return random_state.randint(self.bounds[0], self.bounds[1] + 1, n_samples).astype(float)
return random_state.randint(self.bounds[0], self.bounds[1] + 1, n_samples, dtype=np.int64).astype(
float
)

def to_float(self, value: int | float) -> float:
"""Convert a parameter value to a float.
Expand Down
14 changes: 14 additions & 0 deletions tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ def target_func(**kwargs):
assert p1.kernel_transform(np.array([1.3, 3.6, 7.2])) == pytest.approx(np.array([1, 4, 7]))


@pytest.mark.parametrize("bounds", [(3_000_000_000, 3_000_000_010), (-3_000_000_010, -3_000_000_000), (0, 5)])
def test_int_random_sample_large_bounds(bounds):
parameter = IntParameter("x", bounds)
samples = parameter.random_sample(100, random_state=np.random.RandomState(42))
repeated = parameter.random_sample(100, random_state=np.random.RandomState(42))
assert samples.dtype == np.dtype(float)
np.testing.assert_array_equal(samples, repeated)
assert np.all(samples >= bounds[0])
assert np.all(samples <= bounds[1])
assert np.all(samples == np.floor(samples))
assert bounds[0] in samples
assert bounds[1] in samples


def test_categorical_kernel_transform_preserves_batch_rows():
parameter = CategoricalParameter("x", ["a", "b", "c"])
values = np.array([[0.2, 0.8, 0.1], [0.9, 0.1, 0.2], [0.1, 0.2, 0.7], [0.2, 0.8, 0.1]])
Expand Down
Loading