mojolearn.KNNImputer#
- class mojolearn.KNNImputer(*, missing_values=nan, n_neighbors=5, weights='uniform', metric='nan_euclidean', copy=True, add_indicator=False, keep_empty_features=False)[source]#
Bases:
_XNeighborsImputation of missing values by k nearest neighbors.
Fills each missing value from the
n_neighborsnearest training rows that have that feature, using NaN-aware euclidean distances, following scikit-learn’sKNNImputer. Distance ties go to the lower row index. When no donor is at a finite distance, the training column mean is used. Features that are entirely missing atfitare dropped from the output (or set to zero withkeep_empty_features=True). Computation is float32.- Parameters:
missing_values (float, default nan) – Value marking a missing entry: NaN or another number. Strings, None and booleans raise
NotImplementedError.n_neighbors (int, default 5) – Number of donor neighbors; at least 1.
weights ({‘uniform’, ‘distance’}, default ‘uniform’) – Donor weighting: equal, or by inverse distance. Callables raise
NotImplementedError.metric ({‘nan_euclidean’}, default ‘nan_euclidean’) – Distance; any other value raises
NotImplementedError.copy (bool, default True) – Accepted for scikit-learn compatibility; the output is always a new array.
add_indicator (bool, default False) – Append a 0/1 indicator column for each feature that had missing values at
fit.keep_empty_features (bool, default False) – Keep features that are entirely missing at
fit, imputed as zero.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator (
'fast'is Apple only). None uses the process default.
- Variables:
n_features_in_ (int) – Number of features seen in
fit.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> X[::7, 3] = np.nan >>> X_filled = mojolearn.KNNImputer(n_neighbors=5).fit_transform(X)
- __init__(*, missing_values=nan, n_neighbors=5, weights='uniform', metric='nan_euclidean', copy=True, add_indicator=False, keep_empty_features=False)[source]#
Methods
fit(X[, y])Store the training data used as donors.
fit_transform(X[, y])Fit to X, then impute X:
fit(X).transform(X).get_feature_names_out([input_features])Output feature names.
get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
set_params(**params)Set constructor parameters.
transform(X)Impute the missing values of X.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y=None)[source]#
Store the training data used as donors.
- Parameters:
X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data, with missing values marked by
missing_values.y (None) – Ignored.
- Returns:
self (KNNImputer) – The fitted imputer.
- transform(X)[source]#
Impute the missing values of X.
- Parameters:
X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Data to impute; the number of features must match
fit(ValueErrorotherwise).- Returns:
X_imputed (Array of shape (n_samples, n_features_out), float32) – Imputed data, without the features that were entirely missing at
fit(unlesskeep_empty_features), followed by the indicator columns whenadd_indicatoris True.
- fit_transform(X, y=None)[source]#
Fit to X, then impute X:
fit(X).transform(X).- Parameters:
X (array-like of shape (n_samples, n_features)) – Data with missing values.
y (None) – Ignored.
- Returns:
X_imputed (Array of shape (n_samples, n_features_out), float32)
- get_feature_names_out(input_features=None)[source]#
Output feature names.
The kept input names, followed by
missingindicator_<name>for each indicator column whenadd_indicatoris True.- Parameters:
input_features (list of str or None, default None) – Input feature names; None means
x0,x1, … Its length must equaln_features_in_(ValueErrorotherwise).- Returns:
names (list of str)