簡體   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