0

Please have a look at this code:

import numpy as np
from scipy.spatial import distance

#1
X = [[0,0], [0,1], [0,2], [0,3], [0,4], [0,5]]
c = [[0,0], [0,1], [0,3]]

#2
dists = distance.cdist(X, c)
print(dists)

#3
dmini = np.argmin(dists, axis=1)
print(dmini)

#4
mindists = dists[:, dmini]
print(mindists)

(#1) So I have my data X, some other points (centroids) c, then (#2) I compute the distance from each point in X to all the centroids c, and store the result in dists.

(#3) Then I select the index of the minimum distances with argmin.

(#4) Now I only want to select the value of the minimum values, using the indexes computed in step #3.

However, I get a strange output.

# dists
[[ 0.  1.  3.]
 [ 1.  0.  2.]
 [ 2.  1.  1.]
 [ 3.  2.  0.]
 [ 4.  3.  1.]
 [ 5.  4.  2.]]
#dmini
[0 1 1 2 2 2]

#mindists
[[ 0.  1.  1.  3.  3.  3.]
 [ 1.  0.  0.  2.  2.  2.]
 [ 2.  1.  1.  1.  1.  1.]
 [ 3.  2.  2.  0.  0.  0.]
 [ 4.  3.  3.  1.  1.  1.]
 [ 5.  4.  4.  2.  2.  2.]]

Reading here and there, it seems possible to select specific columns by giving a list of integers (indexes). In this case I should use the dmini values for indexing columns along rows.

I was expecting mindists to be (6,) in shape. What am I doing wrong?

Antonio Sesto
  • 2,868
  • 5
  • 33
  • 51
  • Why don't you have [tag:python-3.x] (or 2.7) and [tag:numpy] too? (IIRC) – user202729 Apr 26 '18 at 13:05
  • I do have those. – Antonio Sesto Apr 26 '18 at 13:17
  • possible duplicate of [Select One Element in Each Row of a Numpy Array by Column Indices](https://stackoverflow.com/questions/17074422/select-one-element-in-each-row-of-a-numpy-array-by-column-indices) – aleneum Apr 26 '18 at 13:18
  • I have read the question and the answer. However, I cannot understand what's wrong with code above since if I write `dists[:, [1]]`, the second column is correctly selected. Is using `np.choose`, ore more complex codes, the only way? – Antonio Sesto Apr 26 '18 at 13:26
  • @AntonioSesto I can't understand what you mean. I can only see the tag [tag:python] in the question. – user202729 Apr 26 '18 at 13:38
  • Now I understand. I will modify the tags. Thanks. – Antonio Sesto Apr 26 '18 at 15:00

0 Answers0