Warning
DEPRECATED
class sklearn.neighbors.LSHForest(n_estimators=10, radius=1.0, n_candidates=50, n_neighbors=5, min_hash_match=4, radius_cutoff_ratio=0.9, random_state=None)
[source]
Performs approximate nearest neighbor search using LSH forest.
LSH Forest: Locality Sensitive Hashing forest [1] is an alternative method for vanilla approximate nearest neighbor search methods. LSH forest data structure has been implemented using sorted arrays and binary search and 32 bit fixed-length hashes. Random projection is used as the hash family which approximates cosine distance.
The cosine distance is defined as 1 - cosine_similarity
: the lowest value is 0 (identical point) but it is bounded above by 2 for the farthest points. Its value does not depend on the norm of the vector points but only on their relative angles.
Parameters: |
|
---|---|
Attributes: |
|
[1] | M. Bawa, T. Condie and P. Ganesan, “LSH Forest: Self-Tuning Indexes for Similarity Search”, WWW ‘05 Proceedings of the 14th international conference on World Wide Web, 651-660, 2005. |
>>> from sklearn.neighbors import LSHForest
>>> X_train = [[5, 5, 2], [21, 5, 5], [1, 1, 1], [8, 9, 1], [6, 10, 2]] >>> X_test = [[9, 1, 6], [3, 1, 10], [7, 10, 3]] >>> lshf = LSHForest(random_state=42) >>> lshf.fit(X_train) LSHForest(min_hash_match=4, n_candidates=50, n_estimators=10, n_neighbors=5, radius=1.0, radius_cutoff_ratio=0.9, random_state=42) >>> distances, indices = lshf.kneighbors(X_test, n_neighbors=2) ... >>> distances array([[0.069..., 0.149...], [0.229..., 0.481...], [0.004..., 0.014...]]) >>> indices array([[1, 2], [2, 0], [4, 0]])
fit (X[, y]) | Fit the LSH forest on the data. |
get_params ([deep]) | Get parameters for this estimator. |
kneighbors (X[, n_neighbors, return_distance]) | Returns n_neighbors of approximate nearest neighbors. |
kneighbors_graph ([X, n_neighbors, mode]) | Computes the (weighted) graph of k-Neighbors for points in X |
partial_fit (X[, y]) | Inserts new data into the already fitted LSH Forest. |
radius_neighbors (X[, radius, return_distance]) | Finds the neighbors within a given radius of a point or points. |
radius_neighbors_graph ([X, radius, mode]) | Computes the (weighted) graph of Neighbors for points in X |
set_params (**params) | Set the parameters of this estimator. |
__init__(n_estimators=10, radius=1.0, n_candidates=50, n_neighbors=5, min_hash_match=4, radius_cutoff_ratio=0.9, random_state=None)
[source]
fit(X, y=None)
[source]
Fit the LSH forest on the data.
This creates binary hashes of input data points by getting the dot product of input points and hash_function then transforming the projection into a binary string array based on the sign (positive/negative) of the projection. A sorted array of binary hashes is created.
Parameters: |
|
---|---|
Returns: |
|
get_params(deep=True)
[source]
Get parameters for this estimator.
Parameters: |
|
---|---|
Returns: |
|
kneighbors(X, n_neighbors=None, return_distance=True)
[source]
Returns n_neighbors of approximate nearest neighbors.
Parameters: |
|
---|---|
Returns: |
|
kneighbors_graph(X=None, n_neighbors=None, mode=’connectivity’)
[source]
Computes the (weighted) graph of k-Neighbors for points in X
Parameters: |
|
---|---|
Returns: |
|
>>> X = [[0], [3], [1]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(n_neighbors=2) >>> neigh.fit(X) NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> A = neigh.kneighbors_graph(X) >>> A.toarray() array([[1., 0., 1.], [0., 1., 1.], [1., 0., 1.]])
partial_fit(X, y=None)
[source]
Inserts new data into the already fitted LSH Forest. Cost is proportional to new total size, so additions should be batched.
Parameters: |
|
---|
radius_neighbors(X, radius=None, return_distance=True)
[source]
Finds the neighbors within a given radius of a point or points.
Return the indices and distances of some points from the dataset lying in a ball with size radius
around the points of the query array. Points lying on the boundary are included in the results.
The result points are not necessarily sorted by distance to their query point.
LSH Forest being an approximate method, some true neighbors from the indexed dataset might be missing from the results.
Parameters: |
|
---|---|
Returns: |
|
radius_neighbors_graph(X=None, radius=None, mode=’connectivity’)
[source]
Computes the (weighted) graph of Neighbors for points in X
Neighborhoods are restricted the points at a distance lower than radius.
Parameters: |
|
---|---|
Returns: |
|
See also
>>> X = [[0], [3], [1]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(radius=1.5) >>> neigh.fit(X) NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> A = neigh.radius_neighbors_graph(X) >>> A.toarray() array([[1., 0., 1.], [0., 1., 0.], [1., 0., 1.]])
set_params(**params)
[source]
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter>
so that it’s possible to update each component of a nested object.
Returns: |
|
---|
© 2007–2018 The scikit-learn developers
Licensed under the 3-clause BSD License.
http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LSHForest.html