GeometricWidthDiscretiser#

class feature_engine.discretisation.GeometricWidthDiscretiser(variables=None, bins=10, return_object=False, return_boundaries=False, precision=7)[source]#

The GeometricWidthDiscretiser() divides continuous numerical variables into intervals of increasing width. The width of each succeeding interval is larger than the previous interval by a constant amount (cw).

The constant amount is calculated as:

\[cw = (Max - Min)^{1/n}\]

were Max and Min are the variable’s maximum and minimum value, and n is the number of intervals.

The sizes of the intervals themselves are calculated with a geometric progression:

\[a_{i+1} = a_i cw\]

Thus, the first interval’s width equals cw, the second interval’s width equals 2 * cw, and so on.

Note that the proportion of observations per interval may vary.

This discretisation technique is great when the distribution of the variable is right skewed.

Note: The width of some bins might be very small. Thus, to allow this transformer to work properly, it might help to increase the precision value, that is, the number of decimal values allowed to define each bin. If the variable has a narrow range or you are sorting into several bins, allow greater precision (i.e., if precision = 3, then 0.001; if precision = 7, then 0.0001).

The GeometricWidthDiscretiser() works only with numerical variables. A list of variables to discretise can be indicated, or the discretiser will automatically select all numerical variables in the train set.

More details in the User Guide.

Parameters
variables: list, default=None

The list of numerical variables to transform. If None, the transformer will automatically find and select all numerical variables.

bins: int, default=10

Desired number of intervals / bins.

return_object: bool, default=False

Whether the the discrete variable should be returned as type numeric or type object. If you would like to encode the discrete variables with Feature-engine’s categorical encoders, use True. Alternatively, keep the default to False.

return_boundaries: bool, default=False

Whether the output should be the interval boundaries. If True, it returns the interval boundaries. If False, it returns integers.

precision: int, default=3

The precision at which to store and display the bins labels.

Attributes
binner_dict_:

Dictionary with the interval limits per variable.

variables_:

The group of variables that will be transformed.

feature_names_in_:

List with the names of features seen during fit.

n_features_in_:

The number of features in the train set used in fit.

References

1

J. Reiser, “Classification Systems”, https://www.slideshare.net/johnjreiser/classification-systems

2

Geometric Interval Classification http://wiki.gis.com/wiki/index.php/Geometric_Interval_Classification

3

Geometric progression https://en.wikipedia.org/wiki/Geometric_progression

Methods

fit:

Find the interval limits.

fit_transform:

Fit to data, then transform it.

get_feature_names_out:

Get output feature names for transformation.

get_params:

Get parameters for this estimator.

set_params:

Set the parameters of this estimator.

transform:

Sort continuous variable values into the intervals.

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

Learn the boundaries of the geometric width intervals / bins for each variable.

Parameters
X: pandas dataframe of shape = [n_samples, n_features]

The training dataset. Can be the entire dataframe, not just the variables to be transformed.

y: None

y is not needed in this encoder. You can pass y or None.

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
Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters.

Returns
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_feature_names_out(input_features=None)[source]#

Get output feature names for transformation. In other words, returns the variable names of transformed dataframe.

Parameters
input_featuresarray or list, default=None

This parameter exits only for compatibility with the Scikit-learn pipeline.

  • If None, then feature_names_in_ is used as feature names in.

  • If an array or list, then input_features must match feature_names_in_.

Returns
feature_names_out: list

Transformed feature names.

rtype

List[Union[str, int]] ..

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]#

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)[source]#

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)[source]#

Sort the variable values into the intervals.

Parameters
X: pandas dataframe of shape = [n_samples, n_features]

The data to transform.

Returns
X_new: pandas dataframe of shape = [n_samples, n_features]

The transformed data with the discrete variables.

rtype

DataFrame ..