votes up 5

Precomputed metric requires shape (n_queries, n_indexed). Got (%d, %d) for %d indexed.

Package:
Exception Class:
ValueError

Raise code

            estimator=estimator)
        Y = check_array(Y, accept_sparse=accept_sparse, dtype=dtype,
                        copy=copy, force_all_finite=force_all_finite,
                        estimator=estimator)

    if precomputed:
        if X.shape[1] != Y.shape[0]:
            raise ValueError("Precomputed metric requires shape "
                             "(n_queries, n_indexed). Got (%d, %d) "
                             "for %d indexed." %
                             (X.shape[0], X.shape[1], Y.shape[0]))
    elif X.shape[1] != Y.shape[1]:
        raise ValueError("Incompatible dimension for X and Y matrices: "
                         "X.shape[1] == %d while Y.shape[1] == %d" % (
                             X.shape[1], Y.shape[1]))

    return 
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 3 votes down

This happens when there is shape mismatch between X and Y while computing pairwise_distances.

This particular error is raised when the metrics parameter is set to "precomputed"

How to reproduce the error:

import numpy as np
from sklearn.metrics import pairwise_distances
from sklearn.metrics.pairwise import pairwise_kernels
X = np.array([[23], [35], [58],[03]])
Y = np.array([[11,0], [21,1],[21,1]])
result = pairwise_distances(X, Y,metric="precomputed")
print(result)

Output (the error):

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-dc2f26e5b1fb> in <module>()
      4 X = np.array([[2, 3], [3, 5], [5, 8],[0, 3]])
      5 Y = np.array([[1, 1,0], [2, 1,1],[2, 1,1]])
----> 6 result = pairwise_distances(X, Y,metric="precomputed")
      7 print(result)

/usr/local/lib/python3.7/dist-packages/sklearn/metrics/pairwise.py in pairwise_distances(X, Y, metric, n_jobs, force_all_finite, **kwds)
   1715     if metric == "precomputed":
   1716         X, _ = check_pairwise_arrays(X, Y, precomputed=True,
-> 1717                                      force_all_finite=force_all_finite)
   1718 
   1719         whom = ("`pairwise_distances`. Precomputed distance "

/usr/local/lib/python3.7/dist-packages/sklearn/metrics/pairwise.py in check_pairwise_arrays(X, Y, precomputed, dtype, accept_sparse, force_all_finite, copy)
    149                              "(n_queries, n_indexed). Got (%d, %d) "
    150                              "for %d indexed." %
--> 151                              (X.shape[0], X.shape[1], Y.shape[0]))
    152     elif X.shape[1] != Y.shape[1]:
    153         raise ValueError("Incompatible dimension for X and Y matrices: "

ValueError: Precomputed metric requires shape (n_queries, n_indexed). Got (4, 2) for 3 indexed.


How to Fix:

The X.shape[1] and Y.shape[0] should be the same doing precomputed pairwise distance.

import numpy as np
from sklearn.metrics import pairwise_distances
from sklearn.metrics.pairwise import pairwise_kernels
X = np.array([[23], [35], [58],[03]])
Y = np.array([[11], [21]])
result = pairwise_distances(X, Y,metric="precomputed")
print(result)

Output (expected):

[[2. 3.]
 [3. 5.]
 [5. 8.]
 [0. 3.]]

Jun 24, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix