fastmatch.knn_scikit
1import numpy as np 2from sklearn.neighbors import NearestNeighbors 3 4 5class ScikitNearestNeighbors: 6 """ 7 A wrapper for scikit-learn's NearestNeighbors to conform to the API 8 used by the other backends in this package. 9 """ 10 11 def __init__(self, **kwargs): 12 """ 13 Initializes the scikit-learn NearestNeighbors model. 14 Any keyword arguments are passed directly to the scikit-learn constructor. 15 """ 16 self.model = NearestNeighbors(**kwargs) 17 18 def fit(self, X: np.ndarray): 19 """ 20 Fits the NearestNeighbors model to the provided data. 21 22 Args: 23 X (np.ndarray): Array of shape (N, M) to fit the model on. 24 25 Returns: 26 self: The fitted object. 27 """ 28 self.model.fit(X) 29 return self 30 31 def kneighbors(self, X: np.ndarray, n_neighbors: int = 1): 32 """ 33 Finds the k-nearest neighbors for the samples in X. 34 35 Args: 36 X (np.ndarray): Array of shape (N, M) to find neighbors for. 37 n_neighbors (int): The number of neighbors to find. 38 39 Returns: 40 (distances, indices): Tuple of arrays containing the distances 41 and indices of the nearest neighbors. 42 """ 43 # n_neighbors in the method call overrides the one in the constructor 44 distances, indices = self.model.kneighbors(X, n_neighbors=n_neighbors) 45 return distances, indices
class
ScikitNearestNeighbors:
6class ScikitNearestNeighbors: 7 """ 8 A wrapper for scikit-learn's NearestNeighbors to conform to the API 9 used by the other backends in this package. 10 """ 11 12 def __init__(self, **kwargs): 13 """ 14 Initializes the scikit-learn NearestNeighbors model. 15 Any keyword arguments are passed directly to the scikit-learn constructor. 16 """ 17 self.model = NearestNeighbors(**kwargs) 18 19 def fit(self, X: np.ndarray): 20 """ 21 Fits the NearestNeighbors model to the provided data. 22 23 Args: 24 X (np.ndarray): Array of shape (N, M) to fit the model on. 25 26 Returns: 27 self: The fitted object. 28 """ 29 self.model.fit(X) 30 return self 31 32 def kneighbors(self, X: np.ndarray, n_neighbors: int = 1): 33 """ 34 Finds the k-nearest neighbors for the samples in X. 35 36 Args: 37 X (np.ndarray): Array of shape (N, M) to find neighbors for. 38 n_neighbors (int): The number of neighbors to find. 39 40 Returns: 41 (distances, indices): Tuple of arrays containing the distances 42 and indices of the nearest neighbors. 43 """ 44 # n_neighbors in the method call overrides the one in the constructor 45 distances, indices = self.model.kneighbors(X, n_neighbors=n_neighbors) 46 return distances, indices
A wrapper for scikit-learn's NearestNeighbors to conform to the API used by the other backends in this package.
ScikitNearestNeighbors(**kwargs)
12 def __init__(self, **kwargs): 13 """ 14 Initializes the scikit-learn NearestNeighbors model. 15 Any keyword arguments are passed directly to the scikit-learn constructor. 16 """ 17 self.model = NearestNeighbors(**kwargs)
Initializes the scikit-learn NearestNeighbors model. Any keyword arguments are passed directly to the scikit-learn constructor.
def
fit(self, X: numpy.ndarray):
19 def fit(self, X: np.ndarray): 20 """ 21 Fits the NearestNeighbors model to the provided data. 22 23 Args: 24 X (np.ndarray): Array of shape (N, M) to fit the model on. 25 26 Returns: 27 self: The fitted object. 28 """ 29 self.model.fit(X) 30 return self
Fits the NearestNeighbors model to the provided data.
Args: X (np.ndarray): Array of shape (N, M) to fit the model on.
Returns: self: The fitted object.
def
kneighbors(self, X: numpy.ndarray, n_neighbors: int = 1):
32 def kneighbors(self, X: np.ndarray, n_neighbors: int = 1): 33 """ 34 Finds the k-nearest neighbors for the samples in X. 35 36 Args: 37 X (np.ndarray): Array of shape (N, M) to find neighbors for. 38 n_neighbors (int): The number of neighbors to find. 39 40 Returns: 41 (distances, indices): Tuple of arrays containing the distances 42 and indices of the nearest neighbors. 43 """ 44 # n_neighbors in the method call overrides the one in the constructor 45 distances, indices = self.model.kneighbors(X, n_neighbors=n_neighbors) 46 return distances, indices
Finds the k-nearest neighbors for the samples in X.
Args: X (np.ndarray): Array of shape (N, M) to find neighbors for. n_neighbors (int): The number of neighbors to find.
Returns: (distances, indices): Tuple of arrays containing the distances and indices of the nearest neighbors.