NumPy:向量化到一组点的距离之和 [英] NumPy: vectorize sum of distances to a set of points
本文介绍了NumPy:向量化到一组点的距离之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实施 k -medoids Python/NumPy中的聚类算法.作为该算法的一部分,我必须计算从对象到它们的"medoids"(集群代表)的距离之和.
I'm trying to implementing a k-medoids clustering algorithm in Python/NumPy. As part of this algo, I have to compute the sum of distances from objects to their "medoids" (cluster representatives).
我有:五点距离矩阵
n_samples = 5
D = np.array([[ 0. , 3.04959014, 4.74341649, 3.72424489, 6.70298441],
[ 3.04959014, 0. , 5.38516481, 4.52216762, 6.16846821],
[ 4.74341649, 5.38516481, 0. , 1.02469508, 8.23711114],
[ 3.72424489, 4.52216762, 1.02469508, 0. , 7.69025357],
[ 6.70298441, 6.16846821, 8.23711114, 7.69025357, 0. ]])
一组初始类固醇
medoids = np.array([0, 3])
和集群成员身份
cl = np.array([0, 0, 1, 1, 0])
我可以使用来计算所需的总和
I can compute the required sum using
>>> np.sum(D[i, medoids[cl[i]]] for i in xrange(n_samples))
10.777269622938899
但是使用Python循环.我是否缺少某种矢量化的惯用法来计算该总和?
but that uses a Python loop. Am I missing some kind of vectorized idiom for computing this sum?
推荐答案
怎么样:
In [17]: D[np.arange(n_samples),medoids[cl]].sum()
Out[17]: 10.777269629999999
这篇关于NumPy:向量化到一组点的距离之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文