def knn(data_points, query_points, k):
    """Find k nearest neighbors in a data set. The implementation currently only supports data type `np.float32`.

    Parameters
    ----------
    data_points: numpy.ndarray (of type `np.float32`)
        Dataset with shape :math:`n \\times d`, where :math:`n` is the number of data points in the data set and :math:`d` is the dimension of each data point
    query_points: numpy.ndarray (of type `np.float32`)
        Data points for which the next neighbours should be calculated
    k: int
        Number of neighbors to find

    Returns
    -------
    distances: numpy.ndarray
        Distances to the neighbors
    indices: numpy.ndarray
        Indices of the k nearest neighbors in original data array

    Example
    -------
    >>> from pymatting import *
    >>> import numpy as np
    >>> data_set = np.random.randn(100, 2)
    >>> knn(data_set.astype(np.float32), np.array([[0.5,0.5]], dtype=np.float32), k=2)
    (array([[0.16233477, 0.25393516]], dtype=float32), array([[25, 17]]))
    """
    tree = KDTree(data_points)
    return tree.query(query_points, k)
Find k nearest neighbors in a data set. The implementation currently only supports data type `np.float32`.

    Parameters
    ----------
    data_points: numpy.ndarray (of type `np.float32`)
        Dataset with shape :math:`n \times d`, where :math:`n` is the number of data points in the data set and :math:`d` is the dimension of each data point
    query_points: numpy.ndarray (of type `np.float32`)
        Data points for which the next neighbours should be calculated
    k: int
        Number of neighbors to find

    Returns
    -------
    distances: numpy.ndarray
        Distances to the neighbors
    indices: numpy.ndarray
        Indices of the k nearest neighbors in original data array

    Example
    -------
    >>> from pymatting import *
    >>> import numpy as np
    >>> data_set = np.random.randn(100, 2)
    >>> knn(data_set.astype(np.float32), np.array([[0.5,0.5]], dtype=np.float32), k=2)
    (array([[0.16233477, 0.25393516]], dtype=float32), array([[25, 17]]))
    
pymatting/util/kdtree.py