skoot.preprocessing
.YeoJohnsonTransformer¶
-
class
skoot.preprocessing.
YeoJohnsonTransformer
(cols=None, n_jobs=1, as_df=True, brack=(-2, 2), dtype=<class 'numpy.float32'>)[source][source]¶ Apply the Yeo-Johnson transformation to a dataset.
Estimate a lambda parameter for each feature, and transform it to a distribution more-closely resembling a Gaussian bell using the Yeo-Johnson transformation.
The Yeo-Johnson transformation, unlike the
BoxCoxTransformer
, allows for zero and negative values of \(y\) and as defined as:\(y_{i} = \left\{\begin{matrix} ((y_{i} + 1)^\lambda - 1)/\lambda & \textup{if } \lambda \neq 0, y \geq 0 \\ log(y_{i} + 1) & \textup{if } \lambda = 0, y \geq 0 \\ -[(-y_{i} + 1)^{(2 - \lambda)} - 1]/(2 - \lambda) & \textup{if } \lambda \neq 2, y < 0 \\ -log(-y_{i} + 1) & \textup{if } \lambda = 2, y < 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 thecols
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 thetransform
method. If False, will return a Numpyndarray
instead. Since most skutil transformers depend on explicitly-namedDataFrame
features, theas_df
parameter is True by default.brack : tuple, optional (default=(-2, 2))
Either a triple (xa, xb, xc) where xa < xb < xc and func(xb) < func(xa), func(xc) or a pair (xa, xb) which are used as a starting interval for a downhill bracket search. Providing the pair (xa, xb) does not always mean the obtained solution will satisfy xa <= x <= xb.
dtype : type, optional (default=np.float32)
The type of float to which to cast the vector. Default is float32 to avoid overflows.
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, brack=(-2, 2), dtype=<class 'numpy.float32'>)[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 ifcols
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.
-