skoot.preprocessing.BoxCoxTransformer

class skoot.preprocessing.BoxCoxTransformer(cols=None, n_jobs=1, as_df=True, min_value=1e-12, dtype=<class 'numpy.float32'>, suppress_warnings=False)[source][source]

Apply the Box-Cox transformation to select features in a dataframe.

Estimate a lambda parameter for each feature, and transform it to a distribution more-closely resembling a Gaussian bell using the Box-Cox transformation.

The Box-Cox transformation cannot handle zeros or negative values in \(y\). Skoot attempts to deal with this scenario by imposing a ceiling function of min_value for any values that are <= 0. The transformation is defined as:

\(y_{i} = \left\{\begin{matrix} \frac{y_{i}^\lambda - 1}{\lambda} & \textup{if } \lambda \neq 0, \\ ln(y_{i}) & \textup{if } \lambda = 0 \end{matrix}\right.\)
Parameters:

cols : array-like, shape=(n_features,), optional (default=None)

The names of the columns on which to apply the transformation. If no column names are provided, the transformer will be fit on the entire frame. Note that the transformation will also only apply to the specified columns, and any other non-specified columns will still be present after transformation. Note that since this transformer can only operate on numeric columns, not explicitly setting the cols parameter may result in errors for categorical data.

n_jobs : int, 1 by default

The number of jobs to use for the computation. This works by estimating each of the feature lambdas in parallel.

If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used.

as_df : bool, optional (default=True)

Whether to return a Pandas DataFrame in the transform method. If False, will return a Numpy ndarray instead. Since most skutil transformers depend on explicitly-named DataFrame features, the as_df parameter is True by default.

min_value : float, optional (default=1e-12)

The minimum value as a ceiling function for values in prescribed features. Values below this amount will be set to min_value.

dtype : type, optional (default=np.float32)

The type of float to which to cast the vector. Default is float32 to avoid overflows.

suppress_warnings : bool, optional (default=False)

Whether to suppress warnings in the scipy.stats.boxcox function. Default is False.

Attributes

lambda_ (list) The lambda values corresponding to each feature
fit_cols_ (list) The list of column names on which the transformer was fit. This is used to validate the presence of the features in the test set during the transform stage.

Methods

fit(X[, y]) Fit the transformer.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
transform(X) Apply the transformation to a dataframe.
__init__(cols=None, n_jobs=1, as_df=True, min_value=1e-12, dtype=<class 'numpy.float32'>, suppress_warnings=False)[source][source]

Initialize self. See help(type(self)) for accurate signature.

fit(X, y=None)[source][source]

Fit the transformer.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to fit. The frame will only be fit on the prescribed cols (see __init__) or all of them if cols is None.

y : array-like or None, shape=(n_samples,), optional (default=None)

Pass-through for sklearn.pipeline.Pipeline.

fit_transform(X, y=None, **fit_params)[source]

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:

X : numpy array of shape [n_samples, n_features]

Training set.

y : numpy array of shape [n_samples]

Target values.

Returns:

X_new : numpy array of shape [n_samples, n_features_new]

Transformed array.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:

deep : boolean, optional

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

Returns:

params : mapping of string to any

Parameter names mapped to their values.

set_params(**params)[source]

Set the parameters of this estimator.

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

Returns:self
transform(X)[source]

Apply the transformation to a dataframe.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to transform. The operation will be applied to a copy of the input data, and the result will be returned.

Returns:

X : pd.DataFrame or np.ndarray, shape=(n_samples, n_features)

The operation is applied to a copy of X, and the result set is returned.

Examples using skoot.preprocessing.BoxCoxTransformer