Skip to content
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

Fix fit_transform on KMeans #4055

Merged
merged 3 commits into from
Jul 16, 2021
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: 2 additions & 2 deletions python/cuml/cluster/kmeans.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ class KMeans(Base,
Compute clustering and transform X to cluster-distance space.

"""
return self.fit(X).transform(X, convert_dtype=convert_dtype,
sample_weight=sample_weight)
self.fit(X, sample_weight=sample_weight)
return self.transform(X, convert_dtype=convert_dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a call/test in one of the pytests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a test to cover fit_transform


def get_param_names(self):
return super().get_param_names() + \
Expand Down
41 changes: 40 additions & 1 deletion python/cuml/test/test_kmeans.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2020, NVIDIA CORPORATION.
# Copyright (c) 2019-2021, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -323,3 +323,42 @@ def test_score(nrows, ncols, nclusters, random_state):

cp.testing.assert_allclose(
actual_score, expected_score, atol=0.1, rtol=1e-5)


@pytest.mark.parametrize('nrows', [100])
@pytest.mark.parametrize('ncols', [25])
@pytest.mark.parametrize('nclusters', [5])
@pytest.mark.parametrize('max_weight', [10])
def test_fit_transform_weighted_kmeans(nrows, ncols, nclusters,
max_weight, random_state):

# Using fairly high variance between points in clusters
cluster_std = 1.0
np.random.seed(random_state)

# set weight per sample to be from 1 to max_weight
wt = np.random.randint(1, high=max_weight, size=nrows)

X, y = make_blobs(nrows,
ncols,
nclusters,
cluster_std=cluster_std,
shuffle=False,
random_state=0)

cuml_kmeans = cuml.KMeans(init="k-means++",
n_clusters=nclusters,
n_init=10,
random_state=random_state,
output_type='numpy')

cuml_transf = cuml_kmeans.fit_transform(X, sample_weight=wt)
cu_score = cuml_kmeans.score(X)

sk_kmeans = cluster.KMeans(random_state=random_state,
n_clusters=nclusters)
sk_transf = sk_kmeans.fit_transform(cp.asnumpy(X), sample_weight=wt)
sk_score = sk_kmeans.score(cp.asnumpy(X))

assert abs(cu_score - sk_score) <= cluster_std * 1.5
assert sk_transf.shape == cuml_transf.shape