from sklearn.impute import KNNImputer as KNNImputerOperation
from DashAI.back.converters.category.basic_preprocessing import (
BasicPreprocessingConverter,
)
from DashAI.back.converters.sklearn_wrapper import SklearnWrapper
from DashAI.back.core.schema_fields import (
bool_field,
enum_field,
int_field,
schema_field,
)
from DashAI.back.core.schema_fields.base_schema import BaseSchema
class KNNImputerSchema(BaseSchema):
n_neighbors: schema_field(
int_field(ge=1),
5,
"The number of nearest neighbors to use for imputation.",
) # type: ignore
weights: schema_field(
enum_field(["uniform", "distance"]), # {‘uniform’, ‘distance’} or callable
"uniform",
"The weight function to use for imputation.",
) # type: ignore
metric: schema_field(
enum_field(["nan_euclidean"]), # {‘nan_euclidean‘} or callable
"nan_euclidean",
"The metric to use for imputation.",
) # type: ignore
use_copy: schema_field(
bool_field(),
True,
"If True, a copy of X will be created.",
alias="copy",
) # type: ignore
add_indicator: schema_field(
bool_field(),
False,
"If True, a MissingIndicator transform will stack onto output.",
) # type: ignore
keep_empty_features: schema_field(
bool_field(),
False,
"If True, empty features will be kept.",
) # type: ignore
[docs]
class KNNImputer(BasicPreprocessingConverter, SklearnWrapper, KNNImputerOperation):
"""Scikit-learn's KNNImputer wrapper for DashAI."""
SCHEMA = KNNImputerSchema
DESCRIPTION = "Imputation for completing missing values using k-Nearest Neighbors."
DISPLAY_NAME = "KNN Imputer"
IMAGE_PREVIEW = "knn_imputer.png"
[docs]
def __init__(self, **kwargs):
super().__init__(**kwargs)