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
1 change: 1 addition & 0 deletions python/cuml/cuml/cluster/kmeans.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ class KMeans(
cdef handle_t* handle_ = <handle_t*><size_t>handle.getHandle()
cdef lib.KMeansParams params
_kmeans_init_params(self, params)
params.metric = DistanceType.L2SqrtExpanded

cdef bool values_f32 = X.dtype == cp.float32
cdef bool indices_i32 = _kmeans_indices_i32(n_rows, n_cols)
Expand Down
30 changes: 30 additions & 0 deletions python/cuml/tests/test_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,36 @@ def test_kmeans_device_buffer_samples_host_path(
assert adjusted_rand_score(dev_labels, host_labels) >= 0.97


def test_kmeans_transform_euclidean():
"""transform should return Euclidean distances, matching sklearn."""
X = np.array(
[
[0.0, 0.0],
[1.0, 1.0],
[2.0, 2.0],
[10.0, 10.0],
[11.0, 11.0],
[12.0, 12.0],
]
)
init = np.array([[1.0, 1.0], [11.0, 11.0]])
query = np.array([[0.0, 0.0], [10.0, 10.0]])

cuml_model = cuml.KMeans(
n_clusters=2, init=init, n_init=1, output_type="numpy"
)
cuml_model.fit(X)
cu_dist = cuml_model.transform(query)

sk_model = cluster.KMeans(n_clusters=2, init=init, n_init=1)
sk_model.fit(X)
sk_dist = sk_model.transform(query)

expected = np.sqrt(np.array([[2.0, 242.0], [162.0, 2.0]]))
np.testing.assert_allclose(cu_dist, expected)
np.testing.assert_allclose(cu_dist, sk_dist)


def test_get_feature_names_out():
X, _ = make_blobs(n_features=5)
cu_model = cuml.KMeans().fit(X)
Expand Down
Loading