Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to fit random vector into list of vectors python

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading