Skip to content

Discretizer


class atom.data_cleaning.Discretizer(strategy="quantile", bins=5, labels=None, device="cpu", engine=None, verbose=0, random_state=None)[source]

Bin continuous data into intervals.

For each feature, the bin edges are computed during fit and, together with the number of bins, they define the intervals. Ignores categorical columns.

This class can be accessed from atom through the discretize method. Read more in the user guide.

Tip

The transformation returns categorical columns. Use the Encoder class to convert them back to numerical types.

Parameters strategy: str, default="quantile"
Strategy used to define the widths of the bins. Choose from:

  • "uniform": All bins have identical widths.
  • "quantile": All bins have the same number of points.
  • "kmeans": Values in each bin have the same nearest center of a 1D k-means cluster.
  • "custom": Use custom bin edges provided through bins.

bins: int, sequence or dict, default=5
Bin number or bin edges in which to split every column.

  • If int: Number of bins to produce for all columns. Only for strategy!="custom".
  • If sequence:

    • For strategy!="custom": Number of bins per column. The n-th value corresponds to the n-th column that is transformed. Categorical columns are ignored.
    • For strategy="custom": Bin edges with length=n_bins - 1. The outermost edges are always -inf and +inf, e.g., bins [1, 2] indicate (-inf, 1], (1, 2], (2, inf].
  • If dict: One of the aforementioned options per column, where the key is the column's name. Columns that are not in the dictionary are not transformed.

labels: sequence, dict or None, default=None
Label names with which to replace the binned intervals.

  • If None: Use default labels of the form (min_edge, max_edge].
  • If sequence: Labels to use for all columns.
  • If dict: Labels per column, where the key is the column's name. Columns that are not in the dictionary use the default labels.

device: str, default="cpu"
Device on which to run the estimators. Use any string that follows the SYCL_DEVICE_FILTER filter selector, e.g. device="gpu" to use the GPU. Read more in the user guide.

engine: str or None, default=None
Execution engine to use for estimators. If None, the default value is used. Choose from:

  • "sklearn" (default)
  • "cuml"

verbose: int, default=0
Verbosity level of the class. Choose from:

  • 0 to not print anything.
  • 1 to print basic information.
  • 2 to print detailed information.

random_state: int or None, default=None
Seed used by the random number generator. If None, the random number generator is the RandomState used by np.random. Only for strategy="quantile".

Attributes feature_names_in_: np.ndarray
Names of features seen during fit.

n_features_in_: int
Number of features seen during fit.


See Also

Encoder

Perform encoding of categorical features.

Imputer

Handle missing values in the data.

Normalizer

Transform the data to follow a Normal/Gaussian distribution.


Example

>>> from atom import ATOMClassifier
>>> from sklearn.datasets import load_breast_cancer

>>> X, y = load_breast_cancer(return_X_y=True, as_frame=True)

>>> atom = ATOMClassifier(X, y, random_state=1)
>>> print(atom["mean radius"])

0      13.48
1      18.31
2      17.93
3      15.13
4       8.95
       ...  
564    14.34
565    13.17
566    17.30
567    17.68
568    14.80
Name: mean radius, Length: 569, dtype: float64

>>> atom.discretize(
...     strategy="custom",
...     bins=[13, 18],
...     labels=["small", "medium", "large"],
...     verbose=2,
...     columns="mean radius",
... )

Fitting Discretizer...
Binning the features...
 --> Discretizing feature mean radius in 3 bins.

>>> print(atom["mean radius"])

0      medium
1       large
2      medium
3      medium
4       small
        ...  
564    medium
565    medium
566    medium
567    medium
568    medium
Name: mean radius, Length: 569, dtype: category
Categories (3, object): ['small' < 'medium' < 'large']
>>> from atom.data_cleaning import Discretizer
>>> from sklearn.datasets import load_breast_cancer

>>> X, y = load_breast_cancer(return_X_y=True, as_frame=True)
>>> print(X["mean radius"])

0      17.99
1      20.57
2      19.69
3      11.42
4      20.29
       ...  
564    21.56
565    20.13
566    16.60
567    20.60
568     7.76
Name: mean radius, Length: 569, dtype: float64

>>> discretizer = Discretizer(
...     strategy="custom",
...     bins={"mean radius": [13, 18]},
...     labels=["small", "medium", "large"],
...     verbose=2,
... )
>>> X = discretizer.fit_transform(X)

Fitting Discretizer...
Binning the features...
 --> Discretizing feature mean radius in 3 bins.

>>> print(X["mean radius"])

0      medium
1       large
2       large
3       small
4       large
        ...  
564     large
565     large
566    medium
567     large
568     small
Name: mean radius, Length: 569, dtype: category
Categories (3, object): ['small' < 'medium' < 'large']


Methods

fitFit to data.
fit_transformFit to data, then transform it.
get_feature_names_outGet output feature names for transformation.
get_paramsGet parameters for this estimator.
inverse_transformDo nothing.
set_outputSet output container.
set_paramsSet the parameters of this estimator.
transformBin the data into intervals.


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

Fit to data.

Parameters X: dataframe-like
Feature set with shape=(n_samples, n_features).

y: sequence, dataframe-like or None, default=None
Do nothing. Implemented for continuity of the API.

Returns Self
Estimator instance.



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

Fit to data, then transform it.

Parameters X: dataframe-like or None, default=None
Feature set with shape=(n_samples, n_features). If None, X is ignored.

y: sequence, dataframe-like or None, default=None
Target column(s) corresponding to X. If None, y is ignored.

**fit_params
Additional keyword arguments for the fit method.

Returns dataframe
Transformed feature set. Only returned if provided.

series or dataframe
Transformed target column. Only returned if provided.



method get_feature_names_out(input_features=None)[source]

Get output feature names for transformation.

Parameters input_features : array-like of str or None, default=None
Input features.

  • If input_features is None, then feature_names_in_ is used as feature names in. If feature_names_in_ is not defined, then the following input feature names are generated: ["x0", "x1", ..., "x(n_features_in_ - 1)"].
  • If input_features is an array-like, then input_features must match feature_names_in_ if feature_names_in_ is defined.

Returns feature_names_out : ndarray of str objects
Same as input features.



method get_params(deep=True)[source]

Get parameters for this estimator.

Parameters deep : bool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns params : dict
Parameter names mapped to their values.



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

Do nothing.

Returns the input unchanged. Implemented for continuity of the API.

Parameters X: dataframe-like or None, default=None
Feature set with shape=(n_samples, n_features). If None, X is ignored.

y: sequence, dataframe-like or None, default=None
Target column(s) corresponding to X. If None, y is ignored.

Returns dataframe
Feature set. Only returned if provided.

series or dataframe
Target column(s). Only returned if provided.



method set_output(transform=None)[source]

Set output container.

See sklearn's user guide on how to use the set_output API. See here a description of the choices.

Parameters transform: str or None, default=None
Configure the output of the transform, fit_transform, and inverse_transform method. If None, the configuration is not changed. Choose from:

  • "numpy"
  • "pandas" (default)
  • "pandas-pyarrow"
  • "polars"
  • "polars-lazy"
  • "pyarrow"
  • "modin"
  • "dask"
  • "pyspark"
  • "pyspark-pandas"

Returns Self
Estimator instance.



method set_params(**params)[source]

Set the parameters of this estimator.

Parameters **params : dict
Estimator parameters.

Returns self : estimator instance
Estimator instance.



method transform(X, y=None)[source]

Bin the data into intervals.

Parameters X: dataframe-like
Feature set with shape=(n_samples, n_features).

y: sequence, dataframe-like or None, default=None
Do nothing. Implemented for continuity of the API.

Returns dataframe
Transformed feature set.