how to fit random vector into list of vectors python
Question:
say I have the following list of vectors
vectors=[(10,0),(-10,0),(0,10),(0,-10)]
and I have the following random vector:
vector=(200,-300)
how can I get the vector from the list that is the most similar to the vector that I am inputting in python?
Answers:
You can use scipy.spatial to find nearest neighbor.
Eg:
import numpy as np
from scipy import spatial
vectors = np.array([(10, 0), (-10, 0), (0, 10), (0, -10)])
tree = spatial.KDTree(vectors)
vector = np.array([200, -300])
distance, i = tree.query(vector)
nearest = vectors[i]
First you need to define similarity between the vectors and your random vector (let’s call it reference). Then you can use python’s min()
function to obtain the closest vector.
In this example I will use the squared Euclidean distance between both tuple members:
vectors = [(10,0), (-10,0), (0,10), (0,-10)]
reference = (200, -300)
def distance_to_ref(x):
return (x[0] - ref[0])**2 + (x[1] - ref[1])**2
closest = min(map(distance_to_ref, vectors))
Instead of map
function, people like to use the list comprehension:
closest = min([distance_to_ref(v) for v in vectors))
Likewise, instead of my native implementation of squared Euclidean distance you can plug in a more generic implementation from a library or write something completely different.
say I have the following list of vectors
vectors=[(10,0),(-10,0),(0,10),(0,-10)]
and I have the following random vector:
vector=(200,-300)
how can I get the vector from the list that is the most similar to the vector that I am inputting in python?
You can use scipy.spatial to find nearest neighbor.
Eg:
import numpy as np
from scipy import spatial
vectors = np.array([(10, 0), (-10, 0), (0, 10), (0, -10)])
tree = spatial.KDTree(vectors)
vector = np.array([200, -300])
distance, i = tree.query(vector)
nearest = vectors[i]
First you need to define similarity between the vectors and your random vector (let’s call it reference). Then you can use python’s min()
function to obtain the closest vector.
In this example I will use the squared Euclidean distance between both tuple members:
vectors = [(10,0), (-10,0), (0,10), (0,-10)]
reference = (200, -300)
def distance_to_ref(x):
return (x[0] - ref[0])**2 + (x[1] - ref[1])**2
closest = min(map(distance_to_ref, vectors))
Instead of map
function, people like to use the list comprehension:
closest = min([distance_to_ref(v) for v in vectors))
Likewise, instead of my native implementation of squared Euclidean distance you can plug in a more generic implementation from a library or write something completely different.