def test_find_binning_thresholds_regular_data(): data = np.linspace(0, 10, 1001).reshape(-1, 1) bin_thresholds = find_binning_thresholds(data, max_bins=10) assert_allclose(bin_thresholds[0], [1, 2, 3, 4, 5, 6, 7, 8, 9]) bin_thresholds = find_binning_thresholds(data, max_bins=5) assert_allclose(bin_thresholds[0], [2, 4, 6, 8])
def test_find_binning_thresholds_low_n_bins(): bin_thresholds = find_binning_thresholds(DATA, max_bins=128, random_state=0) assert len(bin_thresholds) == 2 for i in range(len(bin_thresholds)): assert bin_thresholds[i].shape == (127, ) # 128 - 1 assert bin_thresholds[i].dtype == DATA.dtype
def test_find_binning_thresholds_random_data(): bin_thresholds = find_binning_thresholds(DATA, random_state=0) assert len(bin_thresholds) == 2 for i in range(len(bin_thresholds)): assert bin_thresholds[i].shape == (255, ) # 256 - 1 assert bin_thresholds[i].dtype == DATA.dtype assert_allclose(bin_thresholds[0][[64, 128, 192]], np.array([-0.7, 0.0, 0.7]), atol=1e-1) assert_allclose(bin_thresholds[1][[64, 128, 192]], np.array([9.99, 10.00, 10.01]), atol=1e-2)
def test_map_to_bins(n_bins): bin_thresholds = find_binning_thresholds(DATA, max_bins=n_bins, random_state=0) binned = _map_to_bins(DATA, bin_thresholds) assert binned.shape == DATA.shape assert binned.dtype == np.uint8 assert binned.flags.f_contiguous min_indices = DATA.argmin(axis=0) max_indices = DATA.argmax(axis=0) for feature_idx, min_idx in enumerate(min_indices): assert binned[min_idx, feature_idx] == 0 for feature_idx, max_idx in enumerate(max_indices): assert binned[max_idx, feature_idx] == n_bins - 1
def test_find_binning_thresholds_invalid_n_bins(): with pytest.raises(ValueError): find_binning_thresholds(DATA, max_bins=1024)