You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Off-by-one indexing error in paired_manhattan_distances docstring #34315
found a small index error in the docstring for sklearn.metrics.pairwise.paired_manhattan_distances.
The docstring in sklearn/metrics/pairwise.py says it calculates distances up to: "(X[0], Y[0]), (X[1], Y[1]), ..., (X[n_samples], Y[n_samples])."
Because Python starts counting at 0, the last item in an array of size n_samples is actually at n_samples - 1. If we try to use X[n_samples] in real code, Python throws an IndexError.
Steps/Code to Reproduce
Here is a quick code example using the docstring's own matrix shape to show the error:
importsklearnimportnumpyasnpprint(f"Scikit-Learn Version: {sklearn.__version__}")
n_samples=3X=np.array([[1, 1, 0], [0, 1, 0], [0, 0, 1]])
try:
X[n_samples]
exceptIndexErrorase:
print(f"Error: {e}")
# Output: Error: index 3 is out of bounds for axis 0 with size 3
Scikit-Learn Version: 1.8.0
Suggest a potential alternative/fix
To fix this, we just need to update the docstring in sklearn/metrics/pairwise.py to end with (X[n_samples - 1], Y[n_samples - 1]).
Let me know if I should open a quick PR to patch this!
Hi @Aravind-1294, thank you for your interest, but please note the warning message at the top of the issue and the fact that @shipitdev already expressed interest in opening a PR for this.
thanks @AnneBeyer for keeping an eye on the thread, And no worries at all @Aravind-1294 :) I appreciate the enthusiasm. i am just hanging tight until a core maintainer drops the Needs Triage label so i can raise a PR. i hope im right about the protocol here, please let me know if that's not how the it works. Thank you :)
Hi @shipitdev, thank you for bringing this up and thank you for your patience. Looking at the metric's docstrings, there are different versions used across the paired distances (all that return ndarray of shape (n_samples,)). While only paired_manhattan_distances has an indexing error, would you mind making them consistent while you are at it? I think the return value description in paired_cosine_distances is actually the clearest. For the docstring, I would simply go with Distances are calculated between (X[0], Y[0]), (X[1], Y[1]), etc. to keep it short.
Hi @shipitdev, thank you for bringing this up and thank you for your patience. Looking at the metric's docstrings, there are different versions used across the paired distances (all that return ndarray of shape (n_samples,)). While only paired_manhattan_distances has an indexing error, would you mind making them consistent while you are at it? I think the return value description in paired_cosine_distances is actually the clearest. For the docstring, I would simply go with Distances are calculated between (X[0], Y[0]), (X[1], Y[1]), etc. to keep it short.
Hey @AnneBeyer, thanks for looking into this and removing the triage label!
I'd be happy to make those consistent. I'll standardize the docstring to your request and modify the return blocks to match paired_cosine_distances across all the 4 paired_distance functions i found in this file and push the changes to my open PR shortly! Thank you for the suggestion :)
Activity
Aravind-1294 commentedon Jun 17, 2026
Hi, I'm interested in contributing this fix. If no one is already working on it, I'd be happy to open a PR. Thanks!
AnneBeyer commentedon Jun 17, 2026
Hi @Aravind-1294, thank you for your interest, but please note the warning message at the top of the issue and the fact that @shipitdev already expressed interest in opening a PR for this.
Aravind-1294 commentedon Jun 17, 2026
My bad, I overlooked the warning and @shipitdev's comment. Thanks for the clarification!
shipitdev commentedon Jun 17, 2026
thanks @AnneBeyer for keeping an eye on the thread, And no worries at all @Aravind-1294 :) I appreciate the enthusiasm. i am just hanging tight until a core maintainer drops the
Needs Triagelabel so i can raise a PR. i hope im right about the protocol here, please let me know if that's not how the it works. Thank you :)AnneBeyer commentedon Jul 1, 2026
Hi @shipitdev, thank you for bringing this up and thank you for your patience. Looking at the metric's docstrings, there are different versions used across the paired distances (all that return
ndarray of shape (n_samples,)). While onlypaired_manhattan_distanceshas an indexing error, would you mind making them consistent while you are at it? I think the return value description inpaired_cosine_distancesis actually the clearest. For the docstring, I would simply go withDistances are calculated between (X[0], Y[0]), (X[1], Y[1]), etc.to keep it short.shipitdev commentedon Jul 1, 2026
Hey @AnneBeyer, thanks for looking into this and removing the triage label!
I'd be happy to make those consistent. I'll standardize the docstring to your request and modify the return blocks to match
paired_cosine_distancesacross all the 4 paired_distance functions i found in this file and push the changes to my open PR shortly! Thank you for the suggestion :)