2014-01-06 21:45:40 +08:00
|
|
|
# Authors: Gael Varoquaux <gael.varoquaux@normalesup.org>
|
|
|
|
|
# Justin Vincent
|
|
|
|
|
# Lars Buitinck
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2010-08-26 04:00:03 +08:00
|
|
|
|
2019-10-02 19:56:24 +08:00
|
|
|
import math
|
2010-08-26 04:00:03 +08:00
|
|
|
|
2018-04-22 01:06:10 +08:00
|
|
|
import numpy as np
|
|
|
|
|
import pytest
|
2019-10-02 19:56:24 +08:00
|
|
|
import scipy.stats
|
2018-04-22 01:06:10 +08:00
|
|
|
|
2019-10-29 00:28:56 +08:00
|
|
|
from sklearn.utils._testing import assert_array_equal
|
2016-10-11 03:33:44 +08:00
|
|
|
|
2018-11-12 21:28:05 +08:00
|
|
|
from sklearn.utils.fixes import _object_dtype_isnan
|
2019-10-02 19:56:24 +08:00
|
|
|
from sklearn.utils.fixes import loguniform
|
2010-08-26 04:00:03 +08:00
|
|
|
|
|
|
|
|
|
2018-11-12 21:28:05 +08:00
|
|
|
@pytest.mark.parametrize("dtype, val", ([object, 1], [object, "a"], [float, 1]))
|
|
|
|
|
def test_object_dtype_isnan(dtype, val):
|
|
|
|
|
X = np.array([[val, np.nan], [np.nan, val]], dtype=dtype)
|
|
|
|
|
|
|
|
|
|
expected_mask = np.array([[False, True], [True, False]])
|
|
|
|
|
|
|
|
|
|
mask = _object_dtype_isnan(X)
|
|
|
|
|
|
|
|
|
|
assert_array_equal(mask, expected_mask)
|
2019-10-02 19:56:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("low,high,base", [(-1, 0, 10), (0, 2, np.exp(1)), (-1, 1, 2)])
|
|
|
|
|
def test_loguniform(low, high, base):
|
|
|
|
|
rv = loguniform(base**low, base**high)
|
|
|
|
|
assert isinstance(rv, scipy.stats._distn_infrastructure.rv_frozen)
|
|
|
|
|
rvs = rv.rvs(size=2000, random_state=0)
|
|
|
|
|
|
|
|
|
|
# Test the basics; right bounds, right size
|
|
|
|
|
assert (base**low <= rvs).all() and (rvs <= base**high).all()
|
|
|
|
|
assert len(rvs) == 2000
|
|
|
|
|
|
|
|
|
|
# Test that it's actually (fairly) uniform
|
|
|
|
|
log_rvs = np.array([math.log(x, base) for x in rvs])
|
|
|
|
|
counts, _ = np.histogram(log_rvs)
|
|
|
|
|
assert counts.mean() == 200
|
|
|
|
|
assert np.abs(counts - counts.mean()).max() <= 40
|
|
|
|
|
|
|
|
|
|
# Test that random_state works
|
|
|
|
|
assert loguniform(base**low, base**high).rvs(random_state=0) == loguniform(
|
|
|
|
|
base**low, base**high
|
|
|
|
|
).rvs(random_state=0)
|