Skip to content

Validate input shapes in KMeans and pairwise distances - #374

Merged
josevalim merged 2 commits into
elixir-nx:mainfrom
RicardoSantos-99:fix-kmeans-input-shape
Nov 18, 2026
Merged

josevalim merged 2 commits into
elixir-nx:mainfrom
RicardoSantos-99:fix-kmeans-input-shape

Conversation

@RicardoSantos-99

Copy link
Copy Markdown
Contributor

Closes #373.

KMeans.transform/2 with an input of shape {n, 1} returns a {n, num_clusters} tensor instead of raising:

model = Scholar.Cluster.KMeans.fit(Nx.tensor([[1, 2, 3], [4, 5, 6]]), num_clusters: 2)
Scholar.Cluster.KMeans.transform(model, Nx.tensor([[1], [2]]))

This comes from the centering added in #354. The mean of x and y has one entry per feature of y, so x - mean broadcasts the single column of x against it. In 0.4.2 the same call raised. AffinityPropagation.predict/2, RadiusNNClassifier.predict/2 and RadiusNNRegressor.predict/2 return a result the same way.

Changes

pairwise_squared_euclidean/2, pairwise_euclidean/2, pairwise_minkowski/3 and pairwise_cosine/2 now raise unless both inputs are rank 2 with the same number of features. pairwise_minkowski/3 with p other than 2 already broadcast mismatched inputs before #354, and pairwise_cosine/2 raised from Nx.dot with a less clear message.

KMeans.predict/2 and transform/2 now check the input against the fitted clusters, with the messages KMeans.fit/2 and PCA.transform/2 already use:

expected input tensor to have shape {n_samples, n_features}, got tensor with shape: {3}
expected input tensor to have the same number of features as tensor used to fit the model, got 1 and 3

This replaces assert_same_shape!(x[0], clusters[0]) in predict/2, which compared a row with a centroid and raised expected tensor to have shape {}, got tensor with shape {3} for a rank 1 input.

A rank 1 input raises rather than being reshaped to {1, n}, since it could also mean n samples of one feature. fit/2 already rejects it, and scikit-learn does the same.

Of the other estimators, I checked the ones that go through these distances, plus BruteKNN, KNNClassifier, PCA and KernelPCA, which already validate their input.

Tests

The new tests cover a rank 1 input and an {n, 1} input for KMeans.predict/2 and transform/2, and mismatched shapes for each pairwise distance. They fail on main and pass with this change. The checks also raise under jit_apply with EXLA.

Since the centering added in elixir-nx#354, an x of shape {n, 1} broadcasts against
the mean of y and returns a result instead of raising. pairwise_minkowski
with p other than 2 already broadcast mismatched inputs before that.
@josevalim
josevalim merged commit a7557b3 into elixir-nx:main Nov 18, 2026
2 checks passed
@RicardoSantos-99
RicardoSantos-99 deleted the fix-kmeans-input-shape branch November 18, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug, Scholar.Cluster.KMeans accepts wrong intput shape for transform

2 participants