Class: KNeighborsTransformer
Transform X into a (weighted) graph of k nearest neighbors.
The transformed data is a sparse graph as returned by kneighbors_graph.
Read more in the User Guide.
Constructors
new KNeighborsTransformer()
new KNeighborsTransformer(
opts
?):KNeighborsTransformer
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 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. Distance matrices are not supported. |
opts.metric_params ? | any | Additional keyword arguments for the metric function. |
opts.mode ? | "connectivity" | "distance" | Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric. |
opts.n_jobs ? | number | The number of parallel jobs to run for neighbors search. If \-1 , then the number of jobs is set to the number of CPU cores. |
opts.n_neighbors ? | number | Number of neighbors for each sample in the transformed sparse graph. For compatibility reasons, as each sample is considered as its own neighbor, one extra neighbor will be computed when mode == ‘distance’. In this case, the sparse graph contains (n_neighbors + 1) neighbors. |
opts.p ? | number | Parameter 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. This parameter is expected to be positive. |
Returns KNeighborsTransformer
Defined in generated/neighbors/KNeighborsTransformer.ts:25
Properties
Property | Type | Default value | Defined in |
---|---|---|---|
_isDisposed | boolean | false | generated/neighbors/KNeighborsTransformer.ts:23 |
_isInitialized | boolean | false | generated/neighbors/KNeighborsTransformer.ts:22 |
_py | PythonBridge | undefined | generated/neighbors/KNeighborsTransformer.ts:21 |
id | string | undefined | generated/neighbors/KNeighborsTransformer.ts:18 |
opts | any | undefined | generated/neighbors/KNeighborsTransformer.ts:19 |
Accessors
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/KNeighborsTransformer.ts:489
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/KNeighborsTransformer.ts:516
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/KNeighborsTransformer.ts:570
n_features_in_
Get Signature
get n_features_in_():
Promise
<number
>
Number of features seen during fit.
Returns Promise
<number
>
Defined in generated/neighbors/KNeighborsTransformer.ts:543
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/KNeighborsTransformer.ts:597
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/KNeighborsTransformer.ts:86
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/KNeighborsTransformer.ts:142
fit()
fit(
opts
):Promise
<any
>
Fit the k-nearest neighbors transformer from the training dataset.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike | Training data. |
opts.y ? | any | Not used, present for API consistency by convention. |
Returns Promise
<any
>
Defined in generated/neighbors/KNeighborsTransformer.ts:159
fit_transform()
fit_transform(
opts
):Promise
<any
[]>
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike [] | Training set. |
opts.y ? | any | Not used, present for API consistency by convention. |
Returns Promise
<any
[]>
Defined in generated/neighbors/KNeighborsTransformer.ts:200
get_feature_names_out()
get_feature_names_out(
opts
):Promise
<any
>
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are: \["class_name0", "class_name1", "class_name2"\]
.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.input_features ? | any | Only used to validate feature names with the names seen in fit . |
Returns Promise
<any
>
Defined in generated/neighbors/KNeighborsTransformer.ts:243
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/KNeighborsTransformer.ts:281
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/KNeighborsTransformer.ts:99
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/KNeighborsTransformer.ts:319
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/KNeighborsTransformer.ts:367
set_output()
set_output(
opts
):Promise
<any
>
Set output container.
See Introducing the set_output API for an example on how to use the API.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.transform ? | "default" | "pandas" | "polars" | Configure output of transform and fit_transform . |
Returns Promise
<any
>
Defined in generated/neighbors/KNeighborsTransformer.ts:417
transform()
transform(
opts
):Promise
<any
[]>
Compute the (weighted) graph of Neighbors for points in X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike [] | Sample data. |
Returns Promise
<any
[]>