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_neighbors
int
, optional Number of neighbors to use by default for kneighbors queries, by default 5.
- radius
float
, optional Range of parameter space to use by default for radius_neighbors queries, by default 1.0.
- algorithm
str
, optional Algorithm used to compute the nearest-neighbors, by default ‘auto’. See
sklearn.neighbors.NearestNeighbors
for details.- n_jobs
int
, 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 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 sparsecsc_matrix
.- yarray_like of shape (n_samples,) or (n_samples, n_outputs)
The target values, by default None.
- Returns:
- self
object
Fitted estimator.
- self
- 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.
- 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_neighbors
int
, default=None Number of neighbors required for each sample. The default is the value passed to the constructor.
- return_distance
bool
, default=True Whether or not to return the distances.
- Returns:
- 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.
- radius
float
, 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_distance
bool
, default=True Whether or not to return the distances.
- sort_results
bool
, 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
, settingsort_results=True
will result in an error.New in version 0.22.
- Returns:
- neigh_dist
ndarray
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 themetric
constructor parameter.- neigh_ind
ndarray
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.
- neigh_dist
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:
- **params
dict
Estimator parameters.
- **params
- Returns:
- selfestimator instance
Estimator instance.