Documentation
Classes
KNNImputer

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 (opens in a new tab)

Constructors

constructor()

Signature

new KNNImputer(opts?: object): KNNImputer;

Parameters

NameTypeDescription
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. Default Value false
opts.copy?booleanIf true, a copy of X will be created. If false, imputation will be done in-place whenever possible. Default Value true
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. Default Value false
opts.metric?"nan_euclidean"Distance metric for searching neighbors. Possible values: Default Value 'nan_euclidean'
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. Default Value 5
opts.weights?"uniform" | "distance"Weight function used in prediction. Possible values: Default Value 'uniform'

Returns

KNNImputer

Defined in: generated/impute/KNNImputer.ts:25 (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/impute/KNNImputer.ts:136 (opens in a new tab)

fit()

Fit the imputer on X.

Signature

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

Parameters

NameTypeDescription
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:153 (opens in a new tab)

fit_transform()

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit\_params and returns a transformed version of X.

Signature

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

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]Input samples.
opts.fit_params?anyAdditional fit parameters.
opts.y?ArrayLikeTarget values (undefined for unsupervised transformations).

Returns

Promise<any[]>

Defined in: generated/impute/KNNImputer.ts:193 (opens in a new tab)

get_feature_names_out()

Get output feature names for transformation.

Signature

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

Parameters

NameTypeDescription
optsobject-
opts.input_features?anyInput features.

Returns

Promise<any>

Defined in: generated/impute/KNNImputer.ts:240 (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/impute/KNNImputer.ts:278 (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/impute/KNNImputer.ts:90 (opens in a new tab)

set_output()

Set output container.

See Introducing the set_output API for an example on how to use the API.

Signature

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

Parameters

NameTypeDescription
optsobject-
opts.transform?"default" | "pandas"Configure output of transform and fit\_transform.

Returns

Promise<any>

Defined in: generated/impute/KNNImputer.ts:315 (opens in a new tab)

transform()

Impute all missing values in X.

Signature

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

Parameters

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

Returns

Promise<ArrayLike[]>

Defined in: generated/impute/KNNImputer.ts:348 (opens in a new tab)

Properties

_isDisposed

boolean = false

Defined in: generated/impute/KNNImputer.ts:23 (opens in a new tab)

_isInitialized

boolean = false

Defined in: generated/impute/KNNImputer.ts:22 (opens in a new tab)

_py

PythonBridge

Defined in: generated/impute/KNNImputer.ts:21 (opens in a new tab)

id

string

Defined in: generated/impute/KNNImputer.ts:18 (opens in a new tab)

opts

any

Defined in: generated/impute/KNNImputer.ts:19 (opens in a new tab)

Accessors

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/impute/KNNImputer.ts:429 (opens in a new tab)

indicator_

Indicator used to add binary indicators for missing values. undefined if add_indicator is false.

Signature

indicator_(): Promise<any>;

Returns

Promise<any>

Defined in: generated/impute/KNNImputer.ts:381 (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/impute/KNNImputer.ts:404 (opens in a new tab)

py

Signature

py(): PythonBridge;

Returns

PythonBridge

Defined in: generated/impute/KNNImputer.ts:77 (opens in a new tab)

Signature

py(pythonBridge: PythonBridge): void;

Parameters

NameType
pythonBridgePythonBridge

Returns

void

Defined in: generated/impute/KNNImputer.ts:81 (opens in a new tab)