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.

Python Reference

Constructors

new KNNImputer()

new KNNImputer(opts?): KNNImputer

Parameters

ParameterTypeDescription
opts?object-
opts.add_indicator?booleanIf 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?booleanIf true, a copy of X will be created. If false, imputation will be done in-place whenever possible.
opts.keep_empty_features?booleanIf 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 | numberThe 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?numberNumber 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

PropertyTypeDefault valueDefined in
_isDisposedbooleanfalsegenerated/impute/KNNImputer.ts:23
_isInitializedbooleanfalsegenerated/impute/KNNImputer.ts:22
_pyPythonBridgeundefinedgenerated/impute/KNNImputer.ts:21
idstringundefinedgenerated/impute/KNNImputer.ts:18
optsanyundefinedgenerated/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

ParameterType
pythonBridgePythonBridge

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

ParameterTypeDescription
optsobject-
opts.X?anyInput data, where n_samples is the number of samples and n_features is the number of features.
opts.y?anyNot 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

ParameterTypeDescription
optsobject-
opts.fit_params?anyAdditional fit parameters.
opts.X?ArrayLike[]Input samples.
opts.y?ArrayLikeTarget 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

ParameterTypeDescription
optsobject-
opts.input_features?anyInput 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

ParameterTypeDescription
optsobject-
opts.routing?anyA 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

ParameterType
pyPythonBridge

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

ParameterTypeDescription
optsobject-
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

ParameterTypeDescription
optsobject-
opts.X?ArrayLike[]The input data to complete.

Returns Promise<ArrayLike[]>

Defined in generated/impute/KNNImputer.ts:331