Reputation: 89
I have two arrays centroids
and nodes
I need to find the shortest distance of each point in centroids
to any point in nodes
The output for centroids
is following
array([[12.52512263, 55.78940022],
[12.52027731, 55.7893347 ],
[12.51987146, 55.78855611]])
The output for nodes
is following
array([[12.5217378, 55.7799275],
[12.5122589, 55.7811443],
[12.5241664, 55.7843297],
[12.5189395, 55.7802709]])
I use the following code to get the shortest distance
shortdist_from_centroid_to_node = np.min(cdist(centroids,nodes))
However, this is the output I get (I should get 3 lines of output)
Out[131]: 3.0575613850140956e-05
Can anyone specify what the problem is here? Thanks.
Upvotes: 0
Views: 375
Reputation: 1014
When you doing np.min it return the minimal value of the 2d-array. You want the minimum value for each centroids.
shortest_distance_to_centroid = [min(x) for x in cdist(centroids,nodes)]
To have the associate index one way would be to get the index of the corresponding value. Another is to write a custom min() function that also return the index (so you parse the list only once)
[(list(x).index(min(x)), min(x)) for x in cdist(centroids,nodes)] # the cast list() is needed because numpy array don't have index methods
solution with a custom function:
def my_min(x):
current_min = x[0]
current_index = [1]
for i, v in enumerate(x[1:]):
if v < current_min:
current_min = v
current_index = i + 1
return (current_index, current_min)
[my_min(x) for x in cdist(centroids,nodes)]
Upvotes: 1
Reputation: 251
I guess what you need is just add an arg called axis
, just like this:
shortdist_from_centroid_to_node = np.min(cdist(centroids,nodes), axis=1)
As for the meaning of the axis arg, you could refer to numpy.min. All in all you need minimum on each row rather than on the whole matrix.
Upvotes: 1
Reputation: 2795
If I am not wrong your code says you are trying to access the min
value hence you are getting a single value. remove np.min()
try:
shortdist_from_centroid_to_node = cdist(centroids,nodes)
Upvotes: 0