saealib.CategoricalVariable#

class saealib.CategoricalVariable(categories)[source]#

Bases: Variable

Categorical variable; stored internally as integer index.

Values are encoded as indices in [0, n_categories - 1] and stored as float64 in population arrays. Use encode() / decode() to convert between category values and their integer indices.

Parameters:

categories (list) – Ordered list of category values. Must be non-empty.

Examples

>>> v = CategoricalVariable(["red", "green", "blue"])
>>> v.encode("green")
1
>>> v.decode(1)
'green'

Methods

__init__

decode

Return the category value for index.

encode

Return the integer index for category.

repair

Round x and clip to a valid category index.

Method Details

CategoricalVariable.__init__(categories)[source]#
Parameters:

categories (list)

Return type:

None

CategoricalVariable.decode(index)[source]#

Return the category value for index.

Parameters:

index (int or float) – Index into categories; rounded to nearest integer.

Return type:

category value

CategoricalVariable.encode(category)[source]#

Return the integer index for category.

Parameters:

category – A value present in categories.

Return type:

int

CategoricalVariable.repair(x)[source]#

Round x and clip to a valid category index.

Parameters:

x (np.ndarray) – Values for this dimension.

Returns:

Rounded and clipped values in [0, n_categories - 1].

Return type:

np.ndarray