多维点的几何中位数 [英] Geometric median of multidimensional points
本文介绍了多维点的几何中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3D点数组:
a = np.array([[2., 3., 8.], [10., 4., 3.], [58., 3., 4.], [34., 2., 43.]])
如何计算这些点的几何中位数?
推荐答案
我在他们的论文.一切都以numpy进行矢量化处理,因此应该非常快.我没有实现权重-只是未加权的分数.
I implemented Yehuda Vardi and Cun-Hui Zhang's algorithm for the geometric median, described in their paper "The multivariate L1-median and associated data depth". Everything is vectorized in numpy, so should be very fast. I didn't implement weights - only unweighted points.
import numpy as np
from scipy.spatial.distance import cdist, euclidean
def geometric_median(X, eps=1e-5):
y = np.mean(X, 0)
while True:
D = cdist(X, [y])
nonzeros = (D != 0)[:, 0]
Dinv = 1 / D[nonzeros]
Dinvs = np.sum(Dinv)
W = Dinv / Dinvs
T = np.sum(W * X[nonzeros], 0)
num_zeros = len(X) - np.sum(nonzeros)
if num_zeros == 0:
y1 = T
elif num_zeros == len(X):
return y
else:
R = (T - y) * Dinvs
r = np.linalg.norm(R)
rinv = 0 if r == 0 else num_zeros/r
y1 = max(0, 1-rinv)*T + min(1, rinv)*y
if euclidean(y, y1) < eps:
return y1
y = y1
除了默认的SO许可条款外,如果您愿意,我还根据zlib许可发布了上面的代码.
In addition to the default SO license terms, I release the code above under the zlib license, if you so prefer.
这篇关于多维点的几何中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文