简体   繁体   English

如何优化 2 个点 (x,y,z) 和两个 arrays 之间的距离

[英]How can I optimize the distance between 2 points (x,y,z) and two arrays

I need to calculate the distance between each pixcel and each centroid.我需要计算每个像素和每个质心之间的距离。

Arguments: Arguments:

  • X (numpy array): PxD 1st set of data points (usually data points) X(numpy 数组):PxD 第一组数据点(通常是数据点)
  • C (numpy array): KxD 2nd set of data points (usually cluster centroids points) C(numpy 数组):KxD 第二组数据点(通常是聚类质心点)

Returns:回报:

  • dist: PxK numpy array position ij is the distance between the i-th point of the first set an the j-th point of the second set dist: PxK numpy 数组 position ij 是第一组的第 i 个点和第二组的第 j 个点之间的距离
def distance(X, C):

    dist = numpy.empty((X.shape[0], C.shape[0]))

    for i,x in enumerate(X):
        for y,c in enumerate(C):
            dist[i][y] = euclidean_dist(x,c)

    return dist

def euclidean_dist(x, y):
    x1, y1, z1 = x
    x2, y2, z2 = y
    return math.sqrt((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2)

If you can add scipy dependency, then this is already implemented in scipy.spatial.distance.cdist .如果您可以添加 scipy 依赖项,那么这已经在scipy.spatial.distance.cdist中实现。 Otherwise we can use numpy.broadcasting and numpy.linalg.norm :否则我们可以使用numpy.broadcastingnumpy.linalg.norm

Scipy Implemenation Scipy 实现

from scipy.spatial import distance
distance.cdist(X, C, 'euclidean')

Numpy Implementation Numpy 实现

import numpy as np
np.linalg.norm(X[:,None,:] - C, axis=2)

Performance表现

P = 100_000
K = 10_00
D = 3

X = np.random.randint(0,10, (P,D))
C = np.random.randint(0,10, (K,D))

%timeit distance.cdist(X, C, 'euclidean')
1.06 s ± 57 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit np.linalg.norm(X[:,None,:] - C, axis=2)
15 s ± 2.18 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

We can see that for large sizes of X and C scipy implementation is way faster.我们可以看到,对于大尺寸的XC scipy 的实现要快得多。

暂无
暂无

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

相关问题 如何在两点之间线性插值 x、y 和 z? - How to linear interpolate the x, y, and z between two points? 单位球体上两个“P = (x, y, z)”点之间的大圆距离 - Great Circle Distance between two "P = (x, y, z)" points on a unit sphere 在两个 x,y,z 点之间线性插值高程 - Linearly interpolate elevation between two x,y,z points 给定两个数组在x,y轴上的不同位置,如何获得它们之间的空间距离 - How to get the spatial distance between two arrays given different locations of the arrays in the x,y axes GeoDjango:我怎样才能得到两点之间的距离? - GeoDjango: How can I get the distance between two points? 如何合并两个数组列表(x,y,z,m),排除仅基于(x,y,z)的重复项 - How to merge two lists of arrays, (x,y,z,m), excluding duplicates based on only (x,y,z) 给定两个 NumPy arrays,x 和 y 的长度相同,我怎么能 select 所有对 (x,y) 使得 x 在 x_min 和 x_max 之间有界? - Given two NumPy arrays, x and y of the same length, how can I select all pairs (x,y) such that x is bounded between x_min and x_max? 如何从 matlab 中具有 x、y 和 z 坐标的一组点生成体素网格 - How I can generate a voxel mesh from a set of points with x, y, and z coordinates in matlab Python:如何使用 3 arrays x[]、y[]、z[] 在 3D 中应用 polyfit 功能 - Python: How can I apply the polyfit feature in 3D with 3 arrays x[], y[], z[] 优化 2 个 **long** 二维点阵列之间的距离计算 - Optimize distance calculations between 2 **long**, 2-D arrays of points
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM