Class: KNeighborsClassifier
Classifier implementing the k-nearest neighbors vote.
Read more in the User Guide.
Constructors
new KNeighborsClassifier()
new KNeighborsClassifier(
opts
?):KNeighborsClassifier
Parameters
Parameter | Type | Description |
---|---|---|
opts ? | object | - |
opts.algorithm ? | "auto" | "ball_tree" | "kd_tree" | "brute" | Algorithm used to compute the nearest neighbors: |
opts.leaf_size ? | number | Leaf 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 ? | string | Metric 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 ? | any | Additional keyword arguments for the metric function. |
opts.n_jobs ? | number | The 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. Doesn’t affect fit method. |
opts.n_neighbors ? | number | Number of neighbors to use by default for kneighbors queries. |
opts.p ? | number | Power 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. This parameter is expected to be positive. |
opts.weights ? | "uniform" | "distance" | Weight function used in prediction. Possible values: |
Returns KNeighborsClassifier
Defined in generated/neighbors/KNeighborsClassifier.ts:23
Properties
Property | Type | Default value | Defined in |
---|---|---|---|
_isDisposed | boolean | false | generated/neighbors/KNeighborsClassifier.ts:21 |
_isInitialized | boolean | false | generated/neighbors/KNeighborsClassifier.ts:20 |
_py | PythonBridge | undefined | generated/neighbors/KNeighborsClassifier.ts:19 |
id | string | undefined | generated/neighbors/KNeighborsClassifier.ts:16 |
opts | any | undefined | generated/neighbors/KNeighborsClassifier.ts:17 |
Accessors
classes_
Get Signature
get classes_():
Promise
<any
[]>
Class labels known to the classifier
Returns Promise
<any
[]>
Defined in generated/neighbors/KNeighborsClassifier.ts:488
effective_metric_
Get Signature
get effective_metric_():
Promise
<string
>
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.
Returns Promise
<string
>
Defined in generated/neighbors/KNeighborsClassifier.ts:515
effective_metric_params_
Get Signature
get effective_metric_params_():
Promise
<any
>
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’.
Returns Promise
<any
>
Defined in generated/neighbors/KNeighborsClassifier.ts:542
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/KNeighborsClassifier.ts:596
n_features_in_
Get Signature
get n_features_in_():
Promise
<number
>
Number of features seen during fit.
Returns Promise
<number
>
Defined in generated/neighbors/KNeighborsClassifier.ts:569
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/KNeighborsClassifier.ts:623
outputs_2d_
Get Signature
get outputs_2d_():
Promise
<boolean
>
False when y
’s shape is (n_samples, ) or (n_samples, 1) during fit otherwise true
.
Returns Promise
<boolean
>
Defined in generated/neighbors/KNeighborsClassifier.ts:650
py
Get Signature
get py():
PythonBridge
Returns PythonBridge
Set Signature
set py(
pythonBridge
):void
Parameters
Parameter | Type |
---|---|
pythonBridge | PythonBridge |
Returns void
Defined in generated/neighbors/KNeighborsClassifier.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/KNeighborsClassifier.ts:140
fit()
fit(
opts
):Promise
<any
>
Fit the k-nearest neighbors classifier from the training dataset.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike | Training data. |
opts.y ? | any | Target values. |
Returns Promise
<any
>
Defined in generated/neighbors/KNeighborsClassifier.ts:157
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
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.routing ? | any | A MetadataRequest encapsulating routing information. |
Returns Promise
<any
>
Defined in generated/neighbors/KNeighborsClassifier.ts:198
init()
init(
py
):Promise
<void
>
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Parameters
Parameter | Type |
---|---|
py | PythonBridge |
Returns Promise
<void
>
Defined in generated/neighbors/KNeighborsClassifier.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
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.n_neighbors ? | number | Number of neighbors required for each sample. The default is the value passed to the constructor. |
opts.return_distance ? | boolean | Whether or not to return the distances. |
opts.X ? | any | 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. |
Returns Promise
<ArrayLike
[]>
Defined in generated/neighbors/KNeighborsClassifier.ts:236
kneighbors_graph()
kneighbors_graph(
opts
):Promise
<any
[]>
Compute the (weighted) graph of k-Neighbors for points in X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
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 ? | number | Number of neighbors for each sample. The default is the value passed to the constructor. |
opts.X ? | any | 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. 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/KNeighborsClassifier.ts:284
predict()
predict(
opts
):Promise
<ArrayLike
>
Predict the class labels for the provided data.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | any | Test samples. |
Returns Promise
<ArrayLike
>
Defined in generated/neighbors/KNeighborsClassifier.ts:332
predict_proba()
predict_proba(
opts
):Promise
<any
>
Return probability estimates for the test data X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | any | Test samples. |
Returns Promise
<any
>
Defined in generated/neighbors/KNeighborsClassifier.ts:366
score()
score(
opts
):Promise
<number
>
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.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight ? | ArrayLike | Sample weights. |
opts.X ? | ArrayLike [] | Test samples. |
opts.y ? | ArrayLike | True labels for X . |
Returns Promise
<number
>
Defined in generated/neighbors/KNeighborsClassifier.ts:404
set_score_request()
set_score_request(
opts
):Promise
<any
>
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:
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight ? | string | boolean | Metadata routing for sample_weight parameter in score . |
Returns Promise
<any
>