Documentation
Classes
KNeighborsClassifier

KNeighborsClassifier

Classifier implementing the k-nearest neighbors vote.

Read more in the User Guide.

Python Reference (opens in a new tab)

Constructors

constructor()

Signature

new KNeighborsClassifier(opts?: object): KNeighborsClassifier;

Parameters

NameTypeDescription
opts?object-
opts.algorithm?"auto" | "ball_tree" | "kd_tree" | "brute"Algorithm used to compute the nearest neighbors: Default Value 'auto'
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. Default Value 30
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 (opens in a new tab) 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. Default Value 'minkowski'
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 (opens in a new tab) context. \-1 means using all processors. See Glossary for more details. Doesn’t affect fit method.
opts.n_neighbors?numberNumber of neighbors to use by default for kneighbors queries. Default Value 5
opts.p?numberPower parameter for the Minkowski metric. 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. Default Value 2
opts.weights?"uniform" | "distance"Weight function used in prediction. Possible values: Default Value 'uniform'

Returns

KNeighborsClassifier

Defined in: generated/neighbors/KNeighborsClassifier.ts:23 (opens in a new tab)

Methods

dispose()

Disposes of the underlying Python resources.

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

Signature

dispose(): Promise<void>;

Returns

Promise<void>

Defined in: generated/neighbors/KNeighborsClassifier.ts:147 (opens in a new tab)

fit()

Fit the k-nearest neighbors classifier from the training dataset.

Signature

fit(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeTraining data.
opts.y?anyTarget values.

Returns

Promise<any>

Defined in: generated/neighbors/KNeighborsClassifier.ts:164 (opens in a new tab)

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Signature

get_metadata_routing(opts: object): Promise<any>;

Parameters

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

Returns

Promise<any>

Defined in: generated/neighbors/KNeighborsClassifier.ts:208 (opens in a new tab)

init()

Initializes the underlying Python resources.

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

Signature

init(py: PythonBridge): Promise<void>;

Parameters

NameType
pyPythonBridge

Returns

Promise<void>

Defined in: generated/neighbors/KNeighborsClassifier.ts:97 (opens in a new tab)

kneighbors()

Find the K-neighbors of a point.

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

Signature

kneighbors(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
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.
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. Default Value true

Returns

Promise<ArrayLike[]>

Defined in: generated/neighbors/KNeighborsClassifier.ts:248 (opens in a new tab)

kneighbors_graph()

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

Signature

kneighbors_graph(opts: object): Promise<any[]>;

Parameters

NameTypeDescription
optsobject-
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).
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. Default Value 'connectivity'
opts.n_neighbors?numberNumber of neighbors for each sample. The default is the value passed to the constructor.

Returns

Promise<any[]>

Defined in: generated/neighbors/KNeighborsClassifier.ts:299 (opens in a new tab)

predict()

Predict the class labels for the provided data.

Signature

predict(opts: object): Promise<ArrayLike>;

Parameters

NameTypeDescription
optsobject-
opts.X?anyTest samples.

Returns

Promise<ArrayLike>

Defined in: generated/neighbors/KNeighborsClassifier.ts:351 (opens in a new tab)

predict_proba()

Return probability estimates for the test data X.

Signature

predict_proba(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.X?anyTest samples.

Returns

Promise<any>

Defined in: generated/neighbors/KNeighborsClassifier.ts:386 (opens in a new tab)

score()

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Signature

score(opts: object): Promise<number>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]Test samples.
opts.sample_weight?ArrayLikeSample weights.
opts.y?ArrayLikeTrue labels for X.

Returns

Promise<number>

Defined in: generated/neighbors/KNeighborsClassifier.ts:425 (opens in a new tab)

set_score_request()

Request metadata passed to the score method.

Note that this method is only relevant if enable\_metadata\_routing=True (see sklearn.set\_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Signature

set_score_request(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.sample_weight?string | booleanMetadata routing for sample\_weight parameter in score.

Returns

Promise<any>

Defined in: generated/neighbors/KNeighborsClassifier.ts:478 (opens in a new tab)

Properties

_isDisposed

boolean = false

Defined in: generated/neighbors/KNeighborsClassifier.ts:21 (opens in a new tab)

_isInitialized

boolean = false

Defined in: generated/neighbors/KNeighborsClassifier.ts:20 (opens in a new tab)

_py

PythonBridge

Defined in: generated/neighbors/KNeighborsClassifier.ts:19 (opens in a new tab)

id

string

Defined in: generated/neighbors/KNeighborsClassifier.ts:16 (opens in a new tab)

opts

any

Defined in: generated/neighbors/KNeighborsClassifier.ts:17 (opens in a new tab)

Accessors

classes_

Class labels known to the classifier

Signature

classes_(): Promise<any[]>;

Returns

Promise<any[]>

Defined in: generated/neighbors/KNeighborsClassifier.ts:516 (opens in a new tab)

effective_metric_

The distance metric used. It will be same as the metric parameter or a synonym of it, e.g. ‘euclidean’ if the metric parameter set to ‘minkowski’ and p parameter set to 2.

Signature

effective_metric_(): Promise<string>;

Returns

Promise<string>

Defined in: generated/neighbors/KNeighborsClassifier.ts:543 (opens in a new tab)

effective_metric_params_

Additional keyword arguments for the metric function. For most metrics will be same with metric\_params parameter, but may also contain the p parameter value if the effective\_metric\_ attribute is set to ‘minkowski’.

Signature

effective_metric_params_(): Promise<any>;

Returns

Promise<any>

Defined in: generated/neighbors/KNeighborsClassifier.ts:570 (opens in a new tab)

feature_names_in_

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

Signature

feature_names_in_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/neighbors/KNeighborsClassifier.ts:624 (opens in a new tab)

n_features_in_

Number of features seen during fit.

Signature

n_features_in_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/neighbors/KNeighborsClassifier.ts:597 (opens in a new tab)

n_samples_fit_

Number of samples in the fitted data.

Signature

n_samples_fit_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/neighbors/KNeighborsClassifier.ts:651 (opens in a new tab)

outputs_2d_

False when y’s shape is (n_samples, ) or (n_samples, 1) during fit otherwise true.

Signature

outputs_2d_(): Promise<boolean>;

Returns

Promise<boolean>

Defined in: generated/neighbors/KNeighborsClassifier.ts:678 (opens in a new tab)

py

Signature

py(): PythonBridge;

Returns

PythonBridge

Defined in: generated/neighbors/KNeighborsClassifier.ts:84 (opens in a new tab)

Signature

py(pythonBridge: PythonBridge): void;

Parameters

NameType
pythonBridgePythonBridge

Returns

void

Defined in: generated/neighbors/KNeighborsClassifier.ts:88 (opens in a new tab)