Skip to content

[MRG] Fix #13134: Ensure sorted bin edges for KBinsDiscretizer strategy kmeans #13135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2019
Merged
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: 4 additions & 0 deletions doc/whats_new/v0.20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Changelog
:mod:`sklearn.preprocessing`
............................

- |Fix| Fixed a bug in :class:`preprocessing.KBinsDiscretizer` where
``strategy='kmeans'`` fails with an error during transformation due to unsorted
bin edges. :issue:`13134` by :user:`Sandro Casagrande <SandroCasagrande>`.

- |Fix| Fixed a bug in :class:`preprocessing.OneHotEncoder` where the
deprecation of ``categorical_features`` was handled incorrectly in
combination with ``handle_unknown='ignore'``.
Expand Down
2 changes: 2 additions & 0 deletions sklearn/preprocessing/_discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def fit(self, X, y=None):
# 1D k-means procedure
km = KMeans(n_clusters=n_bins[jj], init=init, n_init=1)
centers = km.fit(column[:, None]).cluster_centers_[:, 0]
# Must sort, centers may be unsorted even with sorted init
centers.sort()
bin_edges[jj] = (centers[1:] + centers[:-1]) * 0.5
bin_edges[jj] = np.r_[col_min, bin_edges[jj], col_max]

Expand Down
16 changes: 11 additions & 5 deletions sklearn/preprocessing/tests/test_discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ def test_invalid_strategy_option():


@pytest.mark.parametrize(
'strategy, expected_2bins, expected_3bins',
[('uniform', [0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 2, 2]),
('kmeans', [0, 0, 0, 0, 1, 1], [0, 0, 1, 1, 2, 2]),
('quantile', [0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 2, 2])])
def test_nonuniform_strategies(strategy, expected_2bins, expected_3bins):
'strategy, expected_2bins, expected_3bins, expected_5bins',
[('uniform', [0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 2, 2], [0, 0, 1, 1, 4, 4]),
('kmeans', [0, 0, 0, 0, 1, 1], [0, 0, 1, 1, 2, 2], [0, 0, 1, 2, 3, 4]),
('quantile', [0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 2, 2], [0, 1, 2, 3, 4, 4])])
def test_nonuniform_strategies(
strategy, expected_2bins, expected_3bins, expected_5bins):
X = np.array([0, 0.5, 2, 3, 9, 10]).reshape(-1, 1)

# with 2 bins
Expand All @@ -202,6 +203,11 @@ def test_nonuniform_strategies(strategy, expected_2bins, expected_3bins):
Xt = est.fit_transform(X)
assert_array_equal(expected_3bins, Xt.ravel())

# with 5 bins
est = KBinsDiscretizer(n_bins=5, strategy=strategy, encode='ordinal')
Xt = est.fit_transform(X)
assert_array_equal(expected_5bins, Xt.ravel())


@pytest.mark.parametrize('strategy', ['uniform', 'kmeans', 'quantile'])
@pytest.mark.parametrize('encode', ['ordinal', 'onehot', 'onehot-dense'])
Expand Down