gators.imputers.NumericsImputer

class gators.imputers.NumericsImputer(strategy: str, value: float = None, columns: List[str] = None, inplace: bool = True)[source]

Impute the numerical columns using the strategy passed by the user.

Parameters
strategystr

Imputation strategy.

Supported imputation strategies are:

  • ‘constant’

  • ‘mean’

  • ‘median’

valuestr, default None.

Imputation value used for strategy=constant.

inplaceList[float], default None.

If True, impute in-place. If False, create new imputed columns.

See also

gators.imputers.ObjectImputer

Impute categorical columns.

Examples

>>> from gators.imputers import NumericsImputer
>>> bins = {'A':[-np.inf, 0, np.inf], 'B':[-np.inf, 1, np.inf]}

The imputation can be done for the selected numerical columns

>>> obj = NumericsImputer(strategy='mean', columns=['A'])

or for all the numerical columns

>>> obj = NumericsImputer(strategy='mean')

The fit, transform, and fit_transform methods accept:

  • dask dataframes:

>>> import dask.dataframe as dd
>>> import pandas as pd
>>> import numpy as np
>>> X = dd.from_pandas(pd.DataFrame(
... {'A': [0.1, 0.2, np.nan], 'B': [1, 2, np.nan], 'C': ['z', 'a', 'a']}), npartitions=1)
  • koalas dataframes:

>>> import databricks.koalas as ks
>>> import numpy as np
>>> X = ks.DataFrame(
... {'A': [0.1, 0.2, np.nan], 'B': [1, 2, np.nan], 'C': ['z', 'a', 'a']})
  • and pandas dataframes:

>>> import pandas as pd
>>> import numpy as np
>>> X = pd.DataFrame(
... {'A': [0.1, 0.2, np.nan], 'B': [1, 2, np.nan], 'C': ['z', 'a', 'a']})

The result is a transformed dataframe belonging to the same dataframe library.

  • imputation done for the selected columns:

>>> obj = NumericsImputer(strategy='mean', columns=['A'])
>>> obj.fit_transform(X)
      A    B  C
0  0.10  1.0  z
1  0.20  2.0  a
2  0.15  NaN  a
  • imputation done for all the columns:

>>> X = pd.DataFrame(
... {'A': [0.1, 0.2, np.nan], 'B': [1, 2, np.nan], 'C': ['z', 'a', 'a']})
>>> obj = NumericsImputer(strategy='mean')
>>> obj.fit_transform(X)
      A    B  C
0  0.10  1.0  z
1  0.20  2.0  a
2  0.15  1.5  a

Independly of the dataframe library used to fit the transformer, the tranform_numpy method only accepts NumPy arrays and returns a transformed NumPy array. Note that this transformer should only be used when the number of rows is small e.g. in real-time environment.

>>> X = pd.DataFrame(
... {'A': [0.1, 0.2, np.nan], 'B': [1, 2, np.nan], 'C': ['z', 'a', 'a']})
>>> obj.transform_numpy(X.to_numpy())
array([[0.1, 1.0, 'z'],
       [0.2, 2.0, 'a'],
       [0.15000000000000002, 1.5, 'a']], dtype=object)
fit(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series] = None) → gators.imputers.numerics_imputer.NumericsImputer[source]

Fit the transformer on the pandas/koalas dataframe X.

Parameters
XDataFrame.

Input dataframe.

ySeries, default None.

Target values.

Returns
self‘NumericsImputer’

Instance of itself.

transform_numpy(X: numpy.ndarray) → numpy.ndarray[source]

Transform the NumPy array X.

Parameters
Xnp.ndarray:

Input array.

Returns
Xnp.ndarray:

Transformed array.

static check_array(X: numpy.ndarray)

Validate array.

Parameters
Xnp.ndarray

Array.

check_array_is_numerics(X: numpy.ndarray)

Check if array is only numerics.

Parameters
Xnp.ndarray

Array.

static check_binary_target(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])

Raise an error if the target is not binary.

Parameters
ySeries

Target values.

static check_dataframe(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Validate dataframe.

Parameters
XDataFrame

Dataframe.

static check_dataframe_contains_numerics(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Check if dataframe is only numerics.

Parameters
XDataFrame

Dataframe.

static check_dataframe_is_numerics(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Check if dataframe is only numerics.

Parameters
XDataFrame

Dataframe.

check_dataframe_with_objects(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Check if dataframe contains object columns.

Parameters
XDataFrame

Dataframe.

check_datatype(dtype, accepted_dtypes)

Check if dataframe is only numerics.

Parameters
XDataFrame

Dataframe.

static check_multiclass_target(y: Union[pd.Series, ks.Series, dd.Series])

Raise an error if the target is not discrete.

Parameters
ySeries

Target values.

check_nans(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], columns: List[str])

Raise an error if X contains NaN values.

Parameters
XDataFrame

Dataframe.

theta_vecList[float]

List of columns.

static check_regression_target(y: Union[pd.Series, ks.Series, dd.Series])

Raise an error if the target is not discrete.

Parameters
ySeries

Target values.

static check_target(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])

Validate target.

Parameters
XDataFrame

Dataframe.

ySeries

Target values.

compute_statistics(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], value: Optional[Union[float, int, str]]) → Dict[str, Union[float, int, str]]

Compute the imputation values.

Parameters
XDataFrame

Dataframe. used to compute the imputation values.

valueUnion[float, int, str, None]

Value used for imputation.

Returns
statisticsDict[str, Union[float, int, str]]

Imputation value mapping.

fit_transform(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series] = None) → Union[pd.DataFrame, ks.DataFrame, dd.DataFrame]

Fit and Transform the dataframe X.

Parameters
XDataFrame.

Input dataframe.

ySeries, default None.

Input target.

Returns
XDataFrame

Transformed dataframe.

static get_column_names(inplace: bool, columns: List[str], suffix: str)

Return the names of the modified columns.

Parameters
inplacebool

If True return columns. If False return columns__suffix.

columnsList[str]

List of columns.

suffixstr

Suffix used if inplace is False.

Returns
List[str]

List of column names.

get_params(deep=True)

Get parameters for this estimator.

Parameters
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsdict

Parameter names mapped to their values.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters
**paramsdict

Estimator parameters.

Returns
selfestimator instance

Estimator instance.

transform(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame]) → Union[pd.DataFrame, ks.DataFrame, dd.DataFrame]

Transform the dataframe X.

Parameters
XDataFrame.

Input dataframe.

Returns
XDataFrame

Transformed dataframe.