Skip to content
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
5 changes: 5 additions & 0 deletions python/cuml/cuml/cluster/kmeans.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ class KMeans(Base,
"while a minimum of 1 is required by KMeans."
)

if n_rows < self.n_clusters:
raise ValueError(
f"n_samples={n_rows} should be >= n_clusters={self.n_clusters}."
)

self._seed = check_random_seed(self.random_state)
self.n_features_in_ = n_cols
self.feature_names_in_ = X_m.index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,6 @@
tests:
- "sklearn.cluster.tests.test_k_means::test_relocating_with_duplicates[elkan-dense]"
- "sklearn.cluster.tests.test_k_means::test_relocating_with_duplicates[lloyd-dense]"
- "sklearn.cluster.tests.test_k_means::test_wrong_params[param0-n_samples.* should be >= n_clusters-KMeans]"
- "sklearn.cluster.tests.test_k_means::test_wrong_params[param1-The shape of the initial centers .* does not match the number of clusters-KMeans]"
- "sklearn.cluster.tests.test_k_means::test_wrong_params[param3-The shape of the initial centers .* does not match the number of features of the data-KMeans]"
- reason: cuml.accel deviates in predict output for KMeans
Expand Down
10 changes: 10 additions & 0 deletions python/cuml/tests/test_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,13 @@ def test_kmeans_empty_x():
y = np.ones(shape=10)
with pytest.raises(ValueError, match=r"Found array with 0 feature\(s\)"):
model.fit(X, y)


def test_kmeans_n_samples_less_than_n_clusters():
model = cuml.KMeans(n_clusters=8)

X = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
with pytest.raises(
ValueError, match="n_samples=2 should be >= n_clusters=8"
):
model.fit(X)