我有矢量v1,v2,v3,v4,v5,100 dim,我需要找到一个中心矢量,每个矢量的距离相等。
更新:从数学看qazxsw poi,有没有办法在Numpy / Python中实现解决方案?
一切都取决于向量在Python中的呈现方式。
让v1,v2,...,v5表示为值列表。每个列表都有len = 100。
在这种情况下,我会做以下事情:
this answer
如果向量已经组成为5x100阵列,例如np.vstack([v1, v2, v3, v4, v5]).mean(axis=1)
,arr
,你可以得到如下解决方案:
arr.shape=(5, 100)
编辑:[问题已更改/澄清]
要获得等距向量(x),请查看我刚写的以下代码片段:
arr.mean(axis=1)
因此,import numpy as np
from scipy.optimize import least_squares
np.random.seed(10)
vector_as_rows = np.random.rand(5, 100)
def Q(x, vs=vector_as_rows):
d = x[-1]
result = list()
for v in vs:
result.append(np.linalg.norm(v-x[:-1])- d)
result.append(0)
return result
res = least_squares(Q, np.random.rand(101)).x
for v in vector_as_rows:
print("Dist between x and v[k]: ", np.linalg.norm(v - res[:-1]))
(len = 100)与所有res[:-1]
等距; v[i]
是距离值。
res[-1]
我怀疑这个问题有分析解决方案;我刚刚从您提供的链接中实现了一种可能的方法来解决未确定的线性系统。
Dist between x and v[k]: 2.530871535402036
Dist between x and v[k]: 2.530871505069009
Dist between x and v[k]: 2.530871545163243
Dist between x and v[k]: 2.5308715299141045
Dist between x and v[k]: 2.5308715309178402
结果是:
A = (vector_as_rows[0] - vector_as_rows[1:]) * 2
res = np.dot(np.linalg.pinv(A), (vector_as_rows[0]**2 - vector_as_rows[1:]**2).sum(axis=1))
for v in vector_as_rows:
print("Dist between x and v[k]: ", np.linalg.norm(v - res))
我使用Dist between x and v[k]: 5.569005123058085
Dist between x and v[k]: 5.569005123058085
Dist between x and v[k]: 5.569005123058084
Dist between x and v[k]: 5.569005123058084
Dist between x and v[k]: 5.569005123058085
,即Moore-Penrose伪反演。使用它我得到了一个未确定线性系统的最小长度解决方案。因此,获得的向量np.linalg.pinv
具有该问题的所有可能解的最小范数。