Repository navigation
Validate input shapes in KMeans and pairwise distances - #374
Merged
josevalim merged 2 commits intoNov 18, 2026
Merged
Conversation
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
approved these changes
Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #373.
KMeans.transform/2with an input of shape{n, 1}returns a{n, num_clusters}tensor instead of raising:This comes from the centering added in #354. The mean of
xandyhas one entry per feature ofy, sox - meanbroadcasts the single column ofxagainst it. In 0.4.2 the same call raised.AffinityPropagation.predict/2,RadiusNNClassifier.predict/2andRadiusNNRegressor.predict/2return a result the same way.Changes
pairwise_squared_euclidean/2,pairwise_euclidean/2,pairwise_minkowski/3andpairwise_cosine/2now raise unless both inputs are rank 2 with the same number of features.pairwise_minkowski/3withpother than 2 already broadcast mismatched inputs before #354, andpairwise_cosine/2raised fromNx.dotwith a less clear message.KMeans.predict/2andtransform/2now check the input against the fitted clusters, with the messagesKMeans.fit/2andPCA.transform/2already use:This replaces
assert_same_shape!(x[0], clusters[0])inpredict/2, which compared a row with a centroid and raisedexpected 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 meannsamples of one feature.fit/2already rejects it, and scikit-learn does the same.Of the other estimators, I checked the ones that go through these distances, plus
BruteKNN,KNNClassifier,PCAandKernelPCA, which already validate their input.Tests
The new tests cover a rank 1 input and an
{n, 1}input forKMeans.predict/2andtransform/2, and mismatched shapes for each pairwise distance. They fail onmainand pass with this change. The checks also raise underjit_applywith EXLA.