CustomKBinsDiscretizer#
- class ray.data.preprocessors.CustomKBinsDiscretizer(columns: List[str], bins: Iterable[float] | pandas.IntervalIndex | Dict[str, Iterable[float] | pandas.IntervalIndex], *, right: bool = True, include_lowest: bool = False, duplicates: str = 'raise', dtypes: Dict[str, pandas.CategoricalDtype | Type[integer]] | None = None, output_columns: List[str] | None = None)[source]#
Bases:
_AbstractKBinsDiscretizerBin values into discrete intervals using custom bin edges.
Columns must contain numerical values.
Examples
Use
CustomKBinsDiscretizerto bin continuous features.>>> import pandas as pd >>> import ray >>> from ray.data.preprocessors import CustomKBinsDiscretizer >>> df = pd.DataFrame({ ... "value_1": [0.2, 1.4, 2.5, 6.2, 9.7, 2.1], ... "value_2": [10, 15, 13, 12, 23, 25], ... }) >>> ds = ray.data.from_pandas(df) >>> discretizer = CustomKBinsDiscretizer( ... columns=["value_1", "value_2"], ... bins=[0, 1, 4, 10, 25] ... ) >>> discretizer.transform(ds).to_pandas() value_1 value_2 0 0 2 1 1 3 2 1 3 3 2 3 4 2 3 5 1 3
CustomKBinsDiscretizercan also be used in append mode by providing the name of the output_columns that should hold the encoded values.>>> discretizer = CustomKBinsDiscretizer( ... columns=["value_1", "value_2"], ... bins=[0, 1, 4, 10, 25], ... output_columns=["value_1_discretized", "value_2_discretized"] ... ) >>> discretizer.fit_transform(ds).to_pandas() value_1 value_2 value_1_discretized value_2_discretized 0 0.2 10 0 2 1 1.4 15 1 3 2 2.5 13 1 3 3 6.2 12 2 3 4 9.7 23 2 3 5 2.1 25 1 3
You can also specify different bin edges per column.
>>> discretizer = CustomKBinsDiscretizer( ... columns=["value_1", "value_2"], ... bins={"value_1": [0, 1, 4], "value_2": [0, 18, 35, 70]}, ... ) >>> discretizer.transform(ds).to_pandas() value_1 value_2 0 0.0 0 1 1.0 0 2 1.0 0 3 <NA> 0 4 <NA> 1 5 1.0 1
- Parameters:
bins (Iterable[float] | pandas.IntervalIndex | Dict[str, Iterable[float] | pandas.IntervalIndex]) – Defines custom bin edges. Can be an iterable of numbers, a
pd.IntervalIndex, or a dict mapping columns to either of them. Note thatpd.IntervalIndexfor bins must be non-overlapping.right (bool) – Indicates whether bins include the rightmost edge.
include_lowest (bool) – Indicates whether the first interval should be left-inclusive.
duplicates (str) – Can be either ‘raise’ or ‘drop’. If bin edges are not unique, raise
ValueErroror drop non-uniques.dtypes (Dict[str, pandas.CategoricalDtype | Type[integer]] | None) – An optional dictionary that maps columns to
pd.CategoricalDtypeobjects ornp.integertypes. If you don’t include a column indtypesor specify it as an integer dtype, the outputted column will consist of ordered integers corresponding to bins. If you use apd.CategoricalDtype, the outputted column will be apd.CategoricalDtypewith the categories being mapped to bins. You can usepd.CategoricalDtype(categories, ordered=True)to preserve information about bin order.output_columns (List[str] | None) – The names of the transformed columns. If None, the transformed columns will be the same as the input columns. If not None, the length of
output_columnsmust match the length ofcolumns, othwerwise an error will be raised.
See also
UniformKBinsDiscretizerIf you want to bin data into uniform width bins.
PublicAPI (alpha): This API is in alpha and may change before becoming stable.
Methods
Deserialize a preprocessor from serialized data.
Fit this Preprocessor to the Dataset.
Return the fit status of this preprocessor.
Fit this Preprocessor to the Dataset and then transform the Dataset.
Return the names of the columns this preprocessor reads.
Return the names of the columns this preprocessor writes.
Get the preprocessor class identifier for this preprocessor class.
Get the version number for this preprocessor class.
Return whether this preprocessor holds computed statistics.
Batch format hint for upstream producers to try yielding best block format.
Serialize this preprocessor to a string or bytes.
Set the preprocessor class identifier for this preprocessor class.
Set the version number for this preprocessor class.
Transform the given dataset.
Transform a single batch of data.
Attributes
The custom bin edges.
The columns to discretize.
The output dtype for each column.
What to do if bin edges aren't unique.
Whether the first interval is left-inclusive.
The names of the transformed columns.
Whether bins include the rightmost edge.
The aggregations that
fit()runs to compute statistics.