I have vectors v1, v2, v3, v4, v5, with 100 dim, I need to find a center vector, that will have equal distance with each.

Uptdate: Looking at this answer from Mathmatics, is there a way to implement the solution in Numpy/Python?

1 Answers

3
bubble On Best Solutions

Everything depends on how vectors are presented in Python.

Lets v1, v2, ... , v5 are presented as lists of values. Each list has len = 100.

In this case I would do the following:

np.vstack([v1, v2, v3, v4, v5]).mean(axis=1)

If vectors are already composed as 5x100 array, e.g. arr, arr.shape=(5, 100), you can get the solution as follows:

arr.mean(axis=1)

EDIT: [the question was changed/clarified]

To get equidistant vector (x) look at the following code snippet I just wrote:

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]))

So, res[:-1] (len = 100) is equidistant to all v[i]; res[-1] is distance value.

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

I suspected that the problem has analytical solution; I just implemented one of the possible ways to solve undetermined linear system from the link you provided.

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))

And result is:

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

I used np.linalg.pinv, that is Moore-Penrose pseudo-inversion. Using it I got a minimal length solution for the undetermined linear system. So, obtained vector res has smallest norm of all possible solutions for this problem.