简体   繁体   中英

How to find a vector from a list of vectors that is nearest to all other vectors using Python?

I have a list of vectors as a numpy array.

[[ 1., 0., 0.],
 [ 0., 1., 2.] ...]

They all have the same dimension. How do I find out that in the vector space which vector is the closest to all the other vectors in the array? Is there scipy or sklearn function that calculates this?

Update :

By "closest", I meant the cosine and the Euclidean distance.

Update 2 :

Let's say I have 4 vectors (a,b,c,d), and the Cosine distance between the vectors are:

a,b = 0.2

a,c = 0.9

a,d = 0.7

b,c = 0.5

b,d = 0.75

c,d = 0.8

So for each, vector a,b,c,d I get :

{
    'a': [1,0.2,0.9,0.7],

    'b': [0.2,1,0.5,0.75],

    'c' : [0.9,0.5,1,0.75],

    'd' : [0.7,0.75,0.8,1]
}

Is there a way of saying let's say vector d is the one that is the most similar to a,b,c ?

You could brute force it something like this. Note that this is O(n^2), and will get slow for large n.

import numpy as np

def cost_function(v1, v2):
    """Returns the square of the distance between vectors v1 and v2."""
    diff = np.subtract(v1, v2)
    # You may want to take the square root here
    return np.dot(diff, diff)

n_vectors = 5
vectors = np.random.rand(n_vectors,3)

min_i = -1
min_cost = 0
for i in range (0, n_vectors):
    sum_cost = 0.0
    for j in range(0, n_vectors):
        sum_cost = sum_cost + cost_function(vectors[i,:],vectors[j,:])
    if min_i < 0 or min_cost > sum_cost:
        min_i = i
        min_cost = sum_cost
    print('{} at {}: {:.3f}'.format(i, vectors[i,:], sum_cost))
print('Lowest cost point is {} at {}: {:.3f}'.format(min_i, vectors[min_i,:], min_cost))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM