Class: KNNImputer
Imputation for completing missing values using k-Nearest Neighbors.
Each sample’s missing values are imputed using the mean value from n_neighbors
nearest neighbors found in the training set. Two samples are close if the features that neither is missing are close.
Read more in the User Guide.
Constructors
new KNNImputer()
new KNNImputer(
opts
?):KNNImputer
Parameters
Parameter | Type | Description |
---|---|---|
opts ? | object | - |
opts.add_indicator ? | boolean | If true , a MissingIndicator transform will stack onto the output of the imputer’s transform. This allows a predictive estimator to account for missingness despite imputation. If a feature has no missing values at fit/train time, the feature won’t appear on the missing indicator even if there are missing values at transform/test time. |
opts.copy ? | boolean | If true , a copy of X will be created. If false , imputation will be done in-place whenever possible. |
opts.keep_empty_features ? | boolean | If true , features that consist exclusively of missing values when fit is called are returned in results when transform is called. The imputed value is always 0 . |
opts.metric ? | "nan_euclidean" | Distance metric for searching neighbors. Possible values: |
opts.missing_values ? | string | number | The placeholder for the missing values. All occurrences of missing_values will be imputed. For pandas’ dataframes with nullable integer dtypes with missing values, missing_values should be set to np.nan, since pd.NA will be converted to np.nan. |
opts.n_neighbors ? | number | Number of neighboring samples to use for imputation. |
opts.weights ? | "uniform" | "distance" | Weight function used in prediction. Possible values: |
Returns KNNImputer
Defined in generated/impute/KNNImputer.ts:25
Properties
Property | Type | Default value | Defined in |
---|---|---|---|
_isDisposed | boolean | false | generated/impute/KNNImputer.ts:23 |
_isInitialized | boolean | false | generated/impute/KNNImputer.ts:22 |
_py | PythonBridge | undefined | generated/impute/KNNImputer.ts:21 |
id | string | undefined | generated/impute/KNNImputer.ts:18 |
opts | any | undefined | generated/impute/KNNImputer.ts:19 |
Accessors
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/impute/KNNImputer.ts:411
indicator_
Get Signature
get indicator_():
Promise
<any
>
Indicator used to add binary indicators for missing values. undefined
if add_indicator is false
.
Returns Promise
<any
>
Defined in generated/impute/KNNImputer.ts:363
n_features_in_
Get Signature
get n_features_in_():
Promise
<number
>
Number of features seen during fit.
Returns Promise
<number
>
Defined in generated/impute/KNNImputer.ts:386
py
Get Signature
get py():
PythonBridge
Returns PythonBridge
Set Signature
set py(
pythonBridge
):void
Parameters
Parameter | Type |
---|---|
pythonBridge | PythonBridge |
Returns void
Defined in generated/impute/KNNImputer.ts:77
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/impute/KNNImputer.ts:129
fit()
fit(
opts
):Promise
<any
>
Fit the imputer on X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | any | Input data, where n_samples is the number of samples and n_features is the number of features. |
opts.y ? | any | Not used, present here for API consistency by convention. |
Returns Promise
<any
>
Defined in generated/impute/KNNImputer.ts:146
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.fit_params ? | any | Additional fit parameters. |
opts.X ? | ArrayLike [] | Input samples. |
opts.y ? | ArrayLike | Target values (undefined for unsupervised transformations). |
Returns Promise
<any
[]>
Defined in generated/impute/KNNImputer.ts:185
get_feature_names_out()
get_feature_names_out(
opts
):Promise
<any
>
Get output feature names for transformation.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.input_features ? | any | Input features. |
Returns Promise
<any
>
Defined in generated/impute/KNNImputer.ts:227
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/impute/KNNImputer.ts:263
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/impute/KNNImputer.ts:90
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/impute/KNNImputer.ts:299
transform()
transform(
opts
):Promise
<ArrayLike
[]>
Impute all missing values in X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike [] | The input data to complete. |
Returns Promise
<ArrayLike
[]>
Defined in generated/impute/KNNImputer.ts:331