a2rl.DiscreteTokenizer#

class a2rl.DiscreteTokenizer(n_bins=100, num_bins_strategy='quantile')[source]#

Bases: Tokenizer

Discretize numeric columns and label encode categorical columns.

The resulted tokens are unique across columns.

By default, the fitting step asserts every column to have sufficient variance (i.e., more than one unique value). Although this check can be switched off, you’re still strongly recommended against it, as single-value columns brings no information gain to the optimization process. As an example, there’s nothing offline RL can learn from a historical data with a constant action. The same arguments applies for states and rewards as well.

Parameters:

Examples

>>> from a2rl import WiDataFrame, DiscreteTokenizer
>>> df = WiDataFrame(
...     data=[
...         [ 10,  5,  20, "x", "a"],
...         [ 10,  5,  40, "x", "a"],
...         [ 50,  5,  50, "y", "b"],
...         [ 50, 85,  60, "y", "b"],
...         [ 90, 85,  80, "z", "b"],
...         [ 90, 85, 100, "z", "a"],
...     ],
...     columns=list("ABCDE"),
...     states=["s1", "s2"],
...     actions=["a"],
...     rewards=["r"],
... )
>>> df
    A   B    C  D  E
0  10   5   20  x  a
1  10   5   40  x  a
2  50   5   50  y  b
3  50  85   60  y  b
4  90  85   80  z  b
5  90  85  100  z  a

>>> t = DiscreteTokenizer(n_bins=5, num_bins_strategy="uniform").fit(df)
>>> df_tok = t.transform(df)
>>> df_tok
   A  B   C   D   E
0  0  5  10  15  18
1  0  5  11  15  18
2  2  5  11  16  19
3  2  9  12  16  19
4  4  9  13  17  19
5  4  9  14  17  18

Fit-transform in one go.

>>> t.fit_transform(df)
   A  B   C   D   E
0  0  5  10  15  18
1  0  5  11  15  18
2  2  5  11  16  19
3  2  9  12  16  19
4  4  9  13  17  19
5  4  9  14  17  18

Reconstruct the approximated original data frame.

>>> t.inverse_transform(df_tok)
      A     B     C  D  E
0  18.0  13.0  28.0  x  a
1  18.0  13.0  44.0  x  a
2  50.0  13.0  44.0  y  b
3  50.0  77.0  60.0  y  b
4  82.0  77.0  76.0  z  b
5  82.0  77.0  92.0  z  a

Methods

check_categorical_columns(df)

Input validation on the all-categorical input dataframe.

check_numerical_columns(df)

Input validation on the all-numerical input dataframe.

fit(df[, check])

Fit the quantizer for the numeric columns, and the label encoder for the categorical columns.

fit_transform(df[, check])

Call fit() then transform().

inverse_transform(df)

Revert the tokenized (i.e., discretized) data-frame bins back to their original space.

transform(df)

Discretize the numberic columns into tokens, label encode the categorical columns into tokens, then disambiguate the tokens across all columns.

valid_tokens(col)

Get the valid tokens for column col.

valid_tokens_of_col_idx(col_idx)

Get the valid tokens for column index col_idx.

valid_tokens_of_col_name(col_name)

Get the valid tokens for column name col_name.