gators.encoders.WOEEncoder

class gators.encoders.WOEEncoder(regularization: float = 0.5, inplace: bool = True)[source]

Encode all categorical variables using the weight of evidence technique.

Parameters
regularizationfloat, default 0.5.

Insure that the weights of evidence are finite.

inplacebool, default to True.

If True, replace in-place the categorical values by numerical ones. If False, keep the categorical columns and create new encoded columns.

Examples

Imports and initialization:

>>> from gators.encoders import WOEEncoder
>>> obj = WOEEncoder()

The fit, transform, and fit_transform methods accept:

  • dask dataframes:

>>> import dask.dataframe as dd
>>> import pandas as pd
>>> X = dd.from_pandas(pd.DataFrame({'A': ['a', 'a', 'b'], 'B': ['c', 'd', 'd']}), npartitions=1)
>>> y = dd.from_pandas(pd.Series([1, 1, 0], name='TARGET'), npartitions=1)
  • koalas dataframes:

>>> import databricks.koalas as ks
>>> X = ks.DataFrame({'A': ['a', 'a', 'b'], 'B': ['c', 'd', 'd']})
>>> y = ks.Series([1, 1, 0], name='TARGET')
  • and pandas dataframes:

>>> import pandas as pd
>>> X = pd.DataFrame({'A': ['a', 'a', 'b'], 'B': ['c', 'd', 'd']})
>>> y = pd.Series([1, 1, 0], name='TARGET')

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

>>> obj.fit_transform(X, y)
          A         B
0  1.203973  0.693147
1  1.203973 -0.405465
2 -1.504077 -0.405465

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.

>>> obj.transform_numpy(X.to_numpy())
array([[ 1.2039728 ,  0.69314718],
       [ 1.2039728 , -0.40546511],
       [-1.5040774 , -0.40546511]])
fit(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series]) → gators.encoders.woe_encoder.WOEEncoder[source]

Fit the encoder.

Parameters
XDataFrame:

Input dataframe.

ySeries, default None.

Target values.

Returns
WOEEncoder:

Instance of itself.

generate_mapping(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series]) → Dict[str, Dict[str, float]][source]

Generate the mapping to perform the encoding.

Parameters
XDataFrame

Input dataframe.

ySeries:

Target values.

Returns
Dict[str, Dict[str, float]]

Mapping.

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.

static decompose_mapping(mapping: Dict[str, Dict[str, float]]) → Tuple[List[str], numpy.ndarray, numpy.ndarray]

Decompose the mapping.

Parameters
mappingDict[str, Dict[str, float]]

The dictionary keys are the categorical columns, the keys are the mapping itself for the assocaited column.

Returns
columnsList[float]

List of columns.

values_vecnp.ndarray

Values to encode.

encoded_values_vecnp.ndarray

Values used to encode.

display_mapping(cmap: Union[str, colormap], k=5, decimal=2, title='')

Display the encoder mapping in a jupyter notebook. Parameters ———- cmap : Union[str, ‘colormap’]

Matplotlib colormap.

kint, default 5.

Number of mappings displayed.

decimalsint, default 2.

Number of decimal places to use.

titlestr, default ‘’.

Plot title.

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.

transform_numpy(X: numpy.ndarray) → numpy.ndarray

Transform the array X.

Parameters
Xnp.ndarray

Input array.

Returns
——-
Xnp.ndarray

Encoded array.