Andy Hadjigeorgiou
Andy Hadjigeorgiou

Reputation: 613

python cdist error ValueError: XA must be a 2-dimensional array

here's a shortened version of my code:

`dist_array = ssd.cdist(test[y], training)`

test[y] printed is

[  0.00000000e+00   1.79900000e+01   1.03800000e+01   1.22800000e+02
   1.00100000e+03   1.18400000e-01   2.77600000e-01   3.00100000e-01
   1.47100000e-01   2.41900000e-01   7.87100000e-02   1.09500000e+00
   9.05300000e-01   8.58900000e+00   1.53400000e+02   6.39900000e-03
   4.90400000e-02   5.37300000e-02   1.58700000e-02   3.00300000e-02
   6.19300000e-03   2.53800000e+01   1.73300000e+01   1.84600000e+02
   2.01900000e+03   1.62200000e-01   6.65600000e-01   7.11900000e-01
   2.65400000e-01   4.60100000e-01   1.18900000e-01]

training printed (shortened) is:

[[  0.00000000e+00   1.92100000e+01   1.85700000e+01 ...,   2.09100000e-01
    3.53700000e-01   8.29400000e-02]
 [  0.00000000e+00   1.47100000e+01   2.15900000e+01 ...,   1.83400000e-01
    3.69800000e-01   1.09400000e-01]
 [  1.00000000e+00   1.30500000e+01   1.93100000e+01 ...,   1.11100000e-02
    2.43900000e-01   6.28900000e-02]
 ..., 
 [  0.00000000e+00   1.66000000e+01   2.80800000e+01 ...,   1.41800000e-01
    2.21800000e-01   7.82000000e-02]
 [  0.00000000e+00   2.06000000e+01   2.93300000e+01 ...,   2.65000000e-01
    4.08700000e-01   1.24000000e-01]
 [  1.00000000e+00   7.76000000e+00   2.45400000e+01 ...,   0.00000000e+00
    2.87100000e-01   7.03900000e-02]]

Both have 31 columns. Is there a way to find the distances between array XA and each row of array XB and have the distances outputted into another array?

Thank you very much!

Upvotes: 16

Views: 23064

Answers (4)

mirekphd
mirekphd

Reputation: 6831

It's enough to reshape the vector (by adding 2nd dimension), matrix can be left "as is":

cdist(np.reshape(my_vector, (1, -1)), my_matrix)

Upvotes: 0

user3067175
user3067175

Reputation: 170

You need to change them into 2-D arrays:

import numpy as np

np.array([1,2,3,4]).shape

--(4,)

np.array([1,2,3,4]).reshape(-1,1).shape

--(4,1)

np.array([1,2,3,4]).reshape(1,-1).shape

--(1,4)

Upvotes: 13

Belter
Belter

Reputation: 3837

from scipy.spatial import distance

p_a = np.array([4, 0])

p_b = np.array([0, 3])

p_a = p_a.reshape(1, -1)

p_b = p_b.reshape(1, -1)

distance.cdist(p_a, p_b, 'euclidean')
Out[58]: array([[ 5.]])

Upvotes: 2

Dr. Jan-Philip Gehrcke
Dr. Jan-Philip Gehrcke

Reputation: 35796

This is discussed here: http://comments.gmane.org/gmane.comp.python.scientific.user/29106

You need to broadcast your one-dimensional array to a two-dimensional one using e.g. the newaxis operator: distance.cdist(ref_1d[numpy.newaxis, :], query_2d).

Upvotes: 2

Related Questions