dislib.neighbors.NearestNeighbors

class dislib.neighbors.base.NearestNeighbors(n_neighbors=5)[source]

Bases: sklearn.base.BaseEstimator

Unsupervised learner for implementing neighbor searches.

Parameters:n_neighbors (int, optional (default=5)) – Number of neighbors to use by default for kneighbors queries.

Examples

>>> import dislib as ds
>>> from dislib.neighbors import NearestNeighbors
>>>
>>>
>>> if __name__ == '__main__':
>>>     data = ds.random_array((100, 5), block_size=(25, 5))
>>>     knn = NearestNeighbors(n_neighbors=10)
>>>     knn.fit(data)
>>>     distances, indices = knn.kneighbors(data)
fit(x)[source]

Fit the model using training data.

Parameters:x (ds-array, shape=(n_samples, n_features)) – Training data.
Returns:self
Return type:NearestNeighbors
kneighbors(x, n_neighbors=None, return_distance=True)[source]

Finds the K nearest neighbors of the input samples. Returns indices and distances to the neighbors of each sample.

Parameters:
  • x (ds-array, shape=(n_samples, n_features)) – The query samples.
  • n_neighbors (int, optional (default=None)) – Number of neighbors to get. If None, the value passed in the constructor is employed.
  • return_distance (boolean, optional (default=True)) – Whether to return distances.
Returns:

  • dist (ds-array, shape=(n_samples, n_neighbors)) – Array representing the lengths to points, only present if return_distance=True.
  • ind (ds-array, shape=(n_samples, n_neighbors)) – Indices of the nearest samples in the fitted data.