繁体   English   中英

如何从不同的numpy数组中找到两点之间的距离?

[英]How do I find the distances between two points from different numpy arrays?

这是针对K均值算法的。 这是用于家庭作业,因此我不想使用内置的Kmeans功能。 我有2个numpy数组。 一种是质心。 另一个是数据点。 我试图找到每个质心到每个数据点的距离。 我不知道如何将数组传递给函数以使其打印。 我想最后得到的距离与质心一样多。 然后,我可以比较数组中的每个距离,选择最小的距离,然后将该点分配给群集之一。 然后找到每个聚类的均值,这些数字将成为我的新质心。

import numpy as np

centroids = np.array([[3,44],[5,15]])
dataPoints = np.array([[2,4],[17,4],[45,2],[45,7],[16,32],[32,14],[20,56],[68,33]])
def distance(a,b):
    for x in a: #for each point in centroids array
        for y in b:#for each point in the dataPoints array
            print np.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)#print the distance

distance (randPoints, dataPoints)#call the function with the data

我得到的输出:

[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]
[ 12.04159458  41.48493703]

我在做什么,这显然是错误的吗? 我应该以2个不同的数组结束,每个数组有8个距离。

import numpy as np

centroids = np.array([[3,44],[5,15]])
dataPoints = np.array([[2,4],[17,4],[45,2],[45,7],[16,32],[32,14],[20,56],[68,33]])

def size(vector):
    return np.sqrt(sum(x**2 for x in vector))

def distance(vector1, vector2):
    return size(vector1 - vector2)

def distances(array1, array2):
    return [[distance(vector1, vector2) for vector2 in array2] for vector1 in array1]

print(distances(centroids, dataPoints))

我厌倦了为1、2和3d数组计算距离的化身,所以我拼凑了一个函数,该函数模仿scipy的pdist和cdist,但使用了许多人在此站点上使用的einsum。 至少在我看来,这很容易理解,einsum在其他目的上用途广泛。 因此,请考虑以下内容。 如果需要提取最近的x值等,可以使用然后使用排序(sort,argsort等)。希望对您有用。

a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([[6, 5], [4, 3], [2, 1]])

def e_dist(a, b, metric='euclidean'):
    """Distance calculation for 1D, 2D and 3D points using einsum
    : a, b   - list, tuple, array in 1,2 or 3D form
    : metric - euclidean ('e','eu'...), sqeuclidean ('s','sq'...), 
    :-----------------------------------------------------------------------
    """
    a = np.asarray(a)
    b = np.atleast_2d(b)
    a_dim = a.ndim
    b_dim = b.ndim
    if a_dim == 1:
        a = a.reshape(1, 1, a.shape[0])
    if a_dim >= 2:
        a = a.reshape(np.prod(a.shape[:-1]), 1, a.shape[-1])
    if b_dim > 2:
        b = b.reshape(np.prod(b.shape[:-1]), b.shape[-1])
    diff = a - b
    dist_arr = np.einsum('ijk,ijk->ij', diff, diff)
    if metric[:1] == 'e':
        dist_arr = np.sqrt(dist_arr)
    dist_arr = np.squeeze(dist_arr)
    return dist_arr

e_dist(a, b)
array([[ 5.8,  3.2,  1.4],
       [ 3.2,  1.4,  3.2],
       [ 1.4,  3.2,  5.8]])

e_dist(a[0], b)
array([ 5.8,  3.2,  1.4])

e_dist(a[:2], b)
array([[ 5.8,  3.2,  1.4],
       [ 3.2,  1.4,  3.2]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM