A pythonic way of calculating distance using numpy matrices?

I have a list of points in the matrix numpy,

A = [[x11,x12,x13],[x21,x22,x23] ]

and I have a start point o= [o1,o2,o3]from which I must calculate the distance for each point,

A - osubtracts ofrom each point. Currently I have to do a square of each attribute and add operation, I do this in a for loop. Is there a more intuitive way to do this?

PS: I am doing the above calculation as an application for clustering kmeans ports. I calculated the centroids, and now I have a computer distance for each point from the center of gravity.

input_mat = input_data_per_minute.values[:,2:5]

scaled_input_mat = scale2(input_mat)

k_means = cluster.KMeans(n_clusters=5)

print 'training start'
k_means.fit(scaled_input_mat)
print 'training over'

out = k_means.cluster_centers_

I need to calculate the distance between input_matand each centroid of the clusters.

+4
2

:

Numpy , , . . number_of_points * number_of_cluster_centers * 3:

, .

:

import numpy as np

points = np.array([[1,1,1],
                   [2,1,1],
                   [1,2,1],
                   [5,5,5]])

centers = np.array([[1.5, 1.5, 1],
                    [5,5,5]])

, numpy- :

distance_3d = points[:,None,:] - centers[None,:,:]

"", - "" , . , . :

(number_of_points, number_of_cluster_centers, 3)

:

# Square each distance
distance_3d_squared = distance_3d ** 2

# Take the sum of each coordinates distance (the result will be 2D)
distance_sum = np.sum(distance_3d_squared, axis=2)

# And take the square root
distance = np.sqrt(distance_sum)

:

#array([[ 0.70710678,  6.92820323],
#       [ 0.70710678,  6.40312424],
#       [ 0.70710678,  6.40312424],
#       [ 6.36396103,  0.        ]])

, distance[i, j] i j.

:

:

distance2 = np.sqrt(np.sum((points[:,None,:] - centers[None,:,:]) ** 2, axis=2))

Scipy ( ):

scipy cdist:

from scipy.spatial.distance import cdist
distance3 = cdist(points, centers)

, cdist .

+3

- : (, ;))

In [1]: import numpy as np

In [2]: a = np.array([[11,12,13],[21,22,23]])

In [3]: o = [1,2,3]

In [4]: a - o  # just showing
Out[4]: 
array([[10, 10, 10],
       [20, 20, 20]])

In [5]: a ** 2  # just showing
Out[5]: 
array([[121, 144, 169],
       [441, 484, 529]])

In [6]: b = (a ** 2) + (a - o)

In [7]: b
Out[7]: 
array([[131, 154, 179],
       [461, 504, 549]])

Numpy , ! , 90%% . for .

0

All Articles