Skip to content

categorical

Categorical and ordinal features.

Classes:

Name Description
CategoricalFeature

Categorical feature.

OrdinalFeature

Feature representing an ordered categorical variable.

CategoricalFeature #

Categorical feature.

It represents data (bool, int or str only) that can be divided into distinct groups or categories.

It may be nominal or ordinal. For instance, "Education level" or "gender" could be set as categorical features.

Parameters:

Name Type Description Default

categories #

list[str] | list[int] | None
  1. When building an AnalyzedSchema: If the preprocessor is one of label_encoder, onehot_encoder, then categories are automatically inferred from the preprocessor when fitted, and you should not provide any categories. Else, categories must be provided.
  2. When building a FittedSchema, categories must be provided. The categories' order must match the preprocessor's processing order.
  3. When using the dataset analyze and fit methods, no categories should be provided.
None

Methods:

Name Description
to_model

Convert to CategoricalFeatureType instance.

parse_categories_to_list

Convert categories to an empty list if they are None.

__attrs_post_init__

Return a fake category to be overwritten by the fitted preprocessor inferred categories.

from_model

Instantiate from a json response.

Attributes:

Name Type Description
name Literal['CATEGORICAL']
categories list[str] | list[int] | None

name: Literal['CATEGORICAL'] = 'CATEGORICAL' #

categories: list[str] | list[int] | None = None #

to_model() -> CategoricalFeatureType #

Convert to CategoricalFeatureType instance.

Source code in src/xpdeep/dataset/feature/feature_types/categorical.py
def to_model(self) -> CategoricalFeatureType:
    """Convert to CategoricalFeatureType instance."""
    return CategoricalFeatureType(
        name=self.name,
        categories=self.parse_categories_to_list(self.categories),
    )

parse_categories_to_list(categories: list[str] | list[int] | None) -> list[str] | list[int] #

Convert categories to an empty list if they are None.

Source code in src/xpdeep/dataset/feature/feature_types/categorical.py
@staticmethod
def parse_categories_to_list(categories: list[str] | list[int] | None) -> list[str] | list[int]:
    """Convert categories to an empty list if they are None."""
    return [] if categories is None or len(categories) == 0 else categories

__attrs_post_init__() -> None #

Return a fake category to be overwritten by the fitted preprocessor inferred categories.

Source code in src/xpdeep/dataset/feature/feature_types/categorical.py
def __attrs_post_init__(self) -> None:
    """Return a fake category to be overwritten by the fitted preprocessor inferred categories."""
    if self.categories is None:
        self.categories = ["fake_categories"]

from_model(feature_type_input: CategoricalFeatureType) -> CategoricalFeature #

Instantiate from a json response.

Source code in src/xpdeep/dataset/feature/feature_types/categorical.py
@staticmethod
def from_model(feature_type_input: CategoricalFeatureType) -> CategoricalFeature:
    """Instantiate from a json response."""
    return CategoricalFeature(
        categories=feature_type_input.categories if len(feature_type_input.categories) > 0 else None
    )

OrdinalFeature #

Feature representing an ordered categorical variable.

An ordinal feature encodes categories that have a defined order or ranking, such as "low", "medium", "high". In contrast to nominal (unordered) categories, ordinal features include a notion of distance between categories, quantified by an associated cost matrix.

Parameters:

Name Type Description Default

categories #

list[str] or list[int]

Ordered list of category labels. The order in the list must correspond to the order used in the cost matrix.

required

cost_matrix #

ndarray

Two-dimensional matrix specifying the pairwise cost or distance between categories. The entry at position (i, j) represents the cost of moving from category i to category j.

required

Methods:

Name Description
to_model

Convert to an OrdinalFeatureType instance.

from_model

Instantiate from a json response.

Attributes:

Name Type Description
categories list[str] | list[int]
cost_matrix ndarray
name Literal['ORDINAL']

categories: list[str] | list[int] #

cost_matrix: np.ndarray #

name: Literal['ORDINAL'] = 'ORDINAL' #

to_model() -> OrdinalFeatureType #

Convert to an OrdinalFeatureType instance.

Returns:

Type Description
OrdinalFeatureType

A feature model representation including the category list and cost matrix.

Source code in src/xpdeep/dataset/feature/feature_types/categorical.py
def to_model(self) -> OrdinalFeatureType:
    """Convert to an `OrdinalFeatureType` instance.

    Returns
    -------
    OrdinalFeatureType
        A feature model representation including the category list and cost matrix.
    """
    if len(self.categories) != self.cost_matrix.shape[0] or len(self.categories) != self.cost_matrix.shape[1]:
        msg = (
            "Invalid cost matrix dimension, expect the cost matrix to be a squared matrix"
            "And the dimension should be equal to the number of categories. "
            "Gt { len(self.categories)} categories and a matrix of shape {self.cost_matrix.shape}"
        )
        raise ValueError(msg)

    return OrdinalFeatureType(name=self.name, categories=self.categories, cost_matrix=self.cost_matrix.tolist())

from_model(feature_type_input: OrdinalFeatureType) -> OrdinalFeature #

Instantiate from a json response.

Source code in src/xpdeep/dataset/feature/feature_types/categorical.py
@staticmethod
def from_model(feature_type_input: OrdinalFeatureType) -> OrdinalFeature:
    """Instantiate from a json response."""
    return OrdinalFeature(
        categories=feature_type_input.categories, cost_matrix=np.array(feature_type_input.cost_matrix)
    )