DocumentationClassesNearestNeighbors

Class: NearestNeighbors

Unsupervised learner for implementing neighbor searches.

Read more in the User Guide.

Python Reference

Constructors

new NearestNeighbors()

new NearestNeighbors(opts?): NearestNeighbors

Parameters

ParameterTypeDescription
opts?object-
opts.algorithm?"auto" | "ball_tree" | "kd_tree" | "brute"Algorithm used to compute the nearest neighbors:
opts.leaf_size?numberLeaf size passed to BallTree or KDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.
opts.metric?stringMetric to use for distance computation. Default is “minkowski”, which results in the standard Euclidean distance when p = 2. See the documentation of scipy.spatial.distance and the metrics listed in distance_metrics for valid metric values. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors. If metric is a callable function, it takes two arrays representing 1D vectors as inputs and must return one value indicating the distance between those vectors. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
opts.metric_params?anyAdditional keyword arguments for the metric function.
opts.n_jobs?numberThe number of parallel jobs to run for neighbors search. undefined means 1 unless in a joblib.parallel_backend context. \-1 means using all processors. See Glossary for more details.
opts.n_neighbors?numberNumber of neighbors to use by default for kneighbors queries.
opts.p?anyParameter for the Minkowski metric from sklearn.metrics.pairwise.pairwise_distances. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.
opts.radius?numberRange of parameter space to use by default for radius_neighbors queries.

Returns NearestNeighbors

Defined in generated/neighbors/NearestNeighbors.ts:23

Properties

PropertyTypeDefault valueDefined in
_isDisposedbooleanfalsegenerated/neighbors/NearestNeighbors.ts:21
_isInitializedbooleanfalsegenerated/neighbors/NearestNeighbors.ts:20
_pyPythonBridgeundefinedgenerated/neighbors/NearestNeighbors.ts:19
idstringundefinedgenerated/neighbors/NearestNeighbors.ts:16
optsanyundefinedgenerated/neighbors/NearestNeighbors.ts:17

Accessors

effective_metric_

Get Signature

get effective_metric_(): Promise<string>

Metric used to compute distances to neighbors.

Returns Promise<string>

Defined in generated/neighbors/NearestNeighbors.ts:444


effective_metric_params_

Get Signature

get effective_metric_params_(): Promise<any>

Parameters for the metric used to compute distances to neighbors.

Returns Promise<any>

Defined in generated/neighbors/NearestNeighbors.ts:471


feature_names_in_

Get Signature

get feature_names_in_(): Promise<ArrayLike>

Names of features seen during fit. Defined only when X has feature names that are all strings.

Returns Promise<ArrayLike>

Defined in generated/neighbors/NearestNeighbors.ts:525


n_features_in_

Get Signature

get n_features_in_(): Promise<number>

Number of features seen during fit.

Returns Promise<number>

Defined in generated/neighbors/NearestNeighbors.ts:498


n_samples_fit_

Get Signature

get n_samples_fit_(): Promise<number>

Number of samples in the fitted data.

Returns Promise<number>

Defined in generated/neighbors/NearestNeighbors.ts:552


py

Get Signature

get py(): PythonBridge

Returns PythonBridge

Set Signature

set py(pythonBridge): void

Parameters

ParameterType
pythonBridgePythonBridge

Returns void

Defined in generated/neighbors/NearestNeighbors.ts:84

Methods

dispose()

dispose(): Promise<void>

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Returns Promise<void>

Defined in generated/neighbors/NearestNeighbors.ts:138


fit()

fit(opts): Promise<any>

Fit the nearest neighbors estimator from the training dataset.

Parameters

ParameterTypeDescription
optsobject-
opts.X?ArrayLikeTraining data.
opts.y?anyNot used, present for API consistency by convention.

Returns Promise<any>

Defined in generated/neighbors/NearestNeighbors.ts:155


get_metadata_routing()

get_metadata_routing(opts): Promise<any>

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Parameters

ParameterTypeDescription
optsobject-
opts.routing?anyA MetadataRequest encapsulating routing information.

Returns Promise<any>

Defined in generated/neighbors/NearestNeighbors.ts:196


init()

init(py): Promise<void>

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Parameters

ParameterType
pyPythonBridge

Returns Promise<void>

Defined in generated/neighbors/NearestNeighbors.ts:97


kneighbors()

kneighbors(opts): Promise<ArrayLike[]>

Find the K-neighbors of a point.

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

Parameters

ParameterTypeDescription
optsobject-
opts.n_neighbors?numberNumber of neighbors required for each sample. The default is the value passed to the constructor.
opts.return_distance?booleanWhether or not to return the distances.
opts.X?anyThe 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.

Returns Promise<ArrayLike[]>

Defined in generated/neighbors/NearestNeighbors.ts:234


kneighbors_graph()

kneighbors_graph(opts): Promise<any[]>

Compute the (weighted) graph of k-Neighbors for points in X.

Parameters

ParameterTypeDescription
optsobject-
opts.mode?"connectivity" | "distance"Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are distances between points, type of distance depends on the selected metric parameter in NearestNeighbors class.
opts.n_neighbors?numberNumber of neighbors for each sample. The default is the value passed to the constructor.
opts.X?anyThe 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. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).

Returns Promise<any[]>

Defined in generated/neighbors/NearestNeighbors.ts:280


radius_neighbors()

radius_neighbors(opts): Promise<any>

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

ParameterTypeDescription
optsobject-
opts.radius?numberLimiting distance of neighbors to return. The default is the value passed to the constructor.
opts.return_distance?booleanWhether or not to return the distances.
opts.sort_results?booleanIf 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.
opts.X?anyThe 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.

Returns Promise<any>

Defined in generated/neighbors/NearestNeighbors.ts:332


radius_neighbors_graph()

radius_neighbors_graph(opts): Promise<any[]>

Compute the (weighted) graph of Neighbors for points in X.

Neighborhoods are restricted the points at a distance lower than radius.

Parameters

ParameterTypeDescription
optsobject-
opts.mode?"connectivity" | "distance"Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are distances between points, type of distance depends on the selected metric parameter in NearestNeighbors class.
opts.radius?numberRadius of neighborhoods. The default is the value passed to the constructor.
opts.sort_results?booleanIf true, in each row of the result, the non-zero entries will be sorted by increasing distances. If false, the non-zero entries may not be sorted. Only used with mode=’distance’.
opts.X?ArrayLikeThe 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.

Returns Promise<any[]>

Defined in generated/neighbors/NearestNeighbors.ts:389