Find closest/similar value(vector) inside a matrix

前端 未结 1 1229
说谎
说谎 2020-11-30 13:01

let\'s say I have the following numpy matrix (simplified):

matrix = np.array([[1, 1],
               [2, 2],
               [5, 5],
               [6, 6]]
           


        
相关标签:
1条回答
  • 2020-11-30 13:43

    Approach #1

    We can use Cython-powered kd-tree for quick nearest-neighbor lookup, which is very efficient both memory-wise and with performance -

    In [276]: from scipy.spatial import cKDTree
    
    In [277]: matrix[cKDTree(matrix).query(search_vec, k=1)[1]]
    Out[277]: array([2, 2])
    

    Approach #2

    With SciPy's cdist -

    In [286]: from scipy.spatial.distance import cdist
    
    In [287]: matrix[cdist(matrix, np.atleast_2d(search_vec)).argmin()]
    Out[287]: array([2, 2])
    

    Approach #3

    With Scikit-learn's Nearest Neighbors -

    from sklearn.neighbors import NearestNeighbors
    
    nbrs = NearestNeighbors(n_neighbors=1).fit(matrix)
    closest_vec = matrix[nbrs.kneighbors(np.atleast_2d(search_vec))[1][0,0]]
    

    Approach #4

    With Scikit-learn's kdtree -

    from sklearn.neighbors import KDTree
    kdt = KDTree(matrix, metric='euclidean')
    cv = matrix[kdt.query(np.atleast_2d(search_vec), k=1, return_distance=False)[0,0]]
    

    Approach #5

    From eucl_dist package (disclaimer: I am its author) and following the wiki contents, we could leverage matrix-multiplication -

    M = matrix.dot(search_vec)
    d = np.einsum('ij,ij->i',matrix,matrix) + np.inner(search_vec,search_vec) -2*M
    closest_vec = matrix[d.argmin()]
    
    0 讨论(0)
提交回复
热议问题