treeple.NearestNeighborsMetaEstimator#

class treeple.NearestNeighborsMetaEstimator(estimator=None, n_neighbors=5, radius=1.0, algorithm='auto', n_jobs=None)[source]#

Meta-estimator for nearest neighbors.

Uses a decision-tree, or forest model to compute distances between samples and then uses the sklearn’s nearest-neighbors API to compute neighbors.

Parameters:
estimatorBaseDecisionTree, BaseForest

The estimator to use for computing distances.

n_neighborsint, optional

Number of neighbors to use by default for kneighbors queries, by default 5.

radiusfloat, optional

Range of parameter space to use by default for radius_neighbors queries, by default 1.0.

algorithmstr, optional

Algorithm used to compute the nearest-neighbors, by default ‘auto’. See sklearn.neighbors.NearestNeighbors for details.

n_jobsint, optional

The number of parallel jobs to run for neighbors, by default None.

Methods

fit(X[, y])

Fit the nearest neighbors estimator from the training dataset.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

kneighbors([X, n_neighbors, return_distance])

Find the K-neighbors of a point.

radius_neighbors([X, radius, ...])

Find the neighbors within a given radius of a point or points.

set_params(**params)

Set the parameters of this estimator.

get_estimator

fit(X, y=None)[source]#

Fit the nearest neighbors estimator from the training dataset.

Parameters:
X{array_like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.

yarray_like of shape (n_samples,) or (n_samples, n_outputs)

The target values, by default None.

Returns:
selfobject

Fitted estimator.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

kneighbors(X=None, n_neighbors=None, return_distance=True)[source]#

Find the K-neighbors of a point.

Returns indices of and distances to the neighbors of each point.

Parameters:
X{array_like, sparse matrix}, shape (n_queries, n_features), or (n_queries, n_indexed) if metric == ‘precomputed’, default=None

Not used, present for API consistency by convention.

n_neighborsint, default=None

Number of neighbors required for each sample. The default is the value passed to the constructor.

return_distancebool, default=True

Whether or not to return the distances.

Returns:
neigh_distndarray of shape (n_queries, n_neighbors)

Array representing the lengths to points, only present if return_distance=True.

neigh_indndarray of shape (n_queries, n_neighbors)

Indices of the nearest points in the population matrix.

radius_neighbors(X=None, radius=None, return_distance=True, sort_results=False)[source]#

Find the neighbors within a given radius of a point or points.

Return the indices and distances of each point 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.

Parameters:
X{array_like, sparse matrix} of (n_samples, n_features), default=None

The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.

radiusfloat, or array_like of shape (n_samples,) default=None

Limiting distance of neighbors to return. The default is the value passed to the constructor. If an array-like of shape (n_samples), then will query for each sample point with a different radius.

return_distancebool, default=True

Whether or not to return the distances.

sort_resultsbool, default=False

If True, the distances and indices will be sorted by increasing distances before being returned. If False, the results may not be sorted. If return_distance=False, setting sort_results=True will result in an error.

New in version 0.22.

Returns:
neigh_distndarray of shape (n_samples,) of arrays

Array representing the distances to each point, only present if return_distance=True. The distance values are computed according to the metric constructor parameter.

neigh_indndarray of shape (n_samples,) of arrays

An array of arrays of indices of the approximate nearest points from the population matrix that lie within a ball of size radius around the query points.

Notes

Because the number of neighbors of each point is not necessarily equal, the results for multiple query points cannot be fit in a standard data array. For efficiency, radius_neighbors returns arrays of objects, where each object is a 1D array of indices or distances.

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.