Source code for evalml.pipelines.components.transformers.imputers.simple_imputer
"""Component that imputes missing data according to a specified imputation strategy."""
import pandas as pd
import woodwork
from sklearn.impute import SimpleImputer as SkImputer
from evalml.pipelines.components.transformers import Transformer
from evalml.utils import infer_feature_types
[docs]class SimpleImputer(Transformer):
"""Imputes missing data according to a specified imputation strategy. Natural language columns are ignored.
Args:
impute_strategy (string): Impute strategy to use. Valid values include "mean", "median", "most_frequent", "constant" for
numerical data, and "most_frequent", "constant" for object data types.
fill_value (string): When impute_strategy == "constant", fill_value is used to replace missing data.
Defaults to 0 when imputing numerical data and "missing_value" for strings or object data types.
random_seed (int): Seed for the random number generator. Defaults to 0.
"""
name = "Simple Imputer"
hyperparameter_ranges = {"impute_strategy": ["mean", "median", "most_frequent"]}
"""{
"impute_strategy": ["mean", "median", "most_frequent"]
}"""
def __init__(
self, impute_strategy="most_frequent", fill_value=None, random_seed=0, **kwargs
):
parameters = {"impute_strategy": impute_strategy, "fill_value": fill_value}
parameters.update(kwargs)
imputer = SkImputer(strategy=impute_strategy, fill_value=fill_value, **kwargs)
self._all_null_cols = None
super().__init__(
parameters=parameters, component_obj=imputer, random_seed=random_seed
)
def _drop_natural_language_columns(self, X):
natural_language_columns = list(
X.ww.select(["NaturalLanguage"], return_schema=True).columns.keys()
)
if natural_language_columns:
X = X.ww.copy()
X = X.ww.drop(columns=natural_language_columns)
return X, natural_language_columns
def _set_boolean_columns_to_categorical(self, X):
boolean_columns = list(
X.ww.select(
["Boolean", "BooleanNullable"], return_schema=True
).columns.keys()
)
if boolean_columns:
X = X.ww.copy()
X.ww.set_types({col: "Categorical" for col in boolean_columns})
return X
[docs] def fit(self, X, y=None):
"""Fits imputer to data. 'None' values are converted to np.nan before imputation and are treated as the same.
Args:
X (pd.DataFrame or np.ndarray): the input training data of shape [n_samples, n_features]
y (pd.Series, optional): the target training data of length [n_samples]
Returns:
self
"""
X = infer_feature_types(X)
nan_ratio = X.ww.describe().loc["nan_count"] / X.shape[0]
self._all_null_cols = nan_ratio[nan_ratio == 1].index.tolist()
X, _ = self._drop_natural_language_columns(X)
X = self._set_boolean_columns_to_categorical(X)
# If the Dataframe only had natural language columns, do nothing.
if X.shape[1] == 0:
return self
self._component_obj.fit(X, y)
return self