onlinerake.Targets¶

class onlinerake.Targets(**kwargs: float | tuple[float, str])[source]¶

Bases: object

Target population margins for binary and continuous features.

A flexible container for specifying target proportions (for binary features) or target means (for continuous features).

Parameters:

**kwargs –

Named feature targets. Each key is a feature name and each value specifies the target: - For binary features: a float in [0, 1] representing the target

proportion of the population where that feature is 1/True.

  • For continuous features: a tuple (value, "mean") where value is the target mean (any real number).

_targets¶

Internal storage of target values.

Type:

dict[str, float]

_feature_types¶

Maps feature names to “binary” or “continuous”.

Type:

dict[str, str]

_feature_names¶

Sorted list of feature names for consistent ordering.

Type:

list[str]

Private Methods:

_validate_feature_exists: Validates that a feature is defined in targets.

Examples

>>> # Binary features only (backward compatible)
>>> targets = Targets(owns_car=0.4, is_subscriber=0.2, likes_coffee=0.7)
>>> print(targets.feature_names)
['is_subscriber', 'likes_coffee', 'owns_car']
>>> # Mixed binary and continuous features
>>> targets = Targets(
...     gender=0.5,                 # binary: 50% female
...     college=0.35,               # binary: 35% college educated
...     age=(42.0, "mean"),         # continuous: mean age 42
...     income=(65000, "mean"),     # continuous: mean income $65k
... )
>>> print(targets.is_binary("gender"))
True
>>> print(targets.is_continuous("age"))
True
>>> print(targets["age"])
42.0
>>> # Access target values
>>> print(targets['owns_car'])
0.4
>>> # Check if feature exists
>>> print('owns_car' in targets)
True
Raises:

ValueError – If any binary target proportion is not between 0 and 1, or if the tuple syntax is malformed.

Note

Feature names are stored in sorted order for consistent behavior across different Python versions and hash randomization settings.

__init__(**kwargs: float | tuple[float, str]) None[source]¶

Methods

__init__(**kwargs)

as_dict()

Convert targets to a dictionary of values.

feature_type(feature)

Get the type of a feature.

is_binary(feature)

Check if a feature is binary.

is_continuous(feature)

Check if a feature is continuous.

Attributes

binary_features

Get list of binary feature names.

continuous_features

Get list of continuous feature names.

feature_names

Get ordered list of feature names.

has_continuous_features

Check if any continuous features are defined.

n_features

Get number of features.

as_dict() dict[str, float][source]¶

Convert targets to a dictionary of values.

Returns:

Dictionary mapping feature names to target values

(proportions for binary, means for continuous).

Return type:

dict[str, float]

Examples

>>> targets = Targets(owns_car=0.4, is_subscriber=0.2)
>>> targets.as_dict()
{'owns_car': 0.4, 'is_subscriber': 0.2}
property binary_features: list[str]¶

Get list of binary feature names.

Returns:

Sorted list of binary feature names.

Return type:

list[str]

Examples

>>> targets = Targets(gender=0.5, age=(35.0, "mean"))
>>> targets.binary_features
['gender']
property continuous_features: list[str]¶

Get list of continuous feature names.

Returns:

Sorted list of continuous feature names.

Return type:

list[str]

Examples

>>> targets = Targets(gender=0.5, age=(35.0, "mean"))
>>> targets.continuous_features
['age']
property feature_names: list[str]¶

Get ordered list of feature names.

Returns:

Sorted list of feature names.

Return type:

list[str]

Examples

>>> targets = Targets(b=0.5, a=0.3, c=0.7)
>>> targets.feature_names
['a', 'b', 'c']
feature_type(feature: str) str[source]¶

Get the type of a feature.

Parameters:

feature – Feature name to look up.

Returns:

Either “binary” or “continuous”.

Return type:

str

Raises:

KeyError – If feature name is not defined in targets.

Examples

>>> targets = Targets(gender=0.5, age=(35.0, "mean"))
>>> targets.feature_type("gender")
'binary'
>>> targets.feature_type("age")
'continuous'
property has_continuous_features: bool¶

Check if any continuous features are defined.

Returns:

True if at least one continuous feature is defined.

Return type:

bool

is_binary(feature: str) bool[source]¶

Check if a feature is binary.

Parameters:

feature – Feature name to check.

Returns:

True if feature is binary, False otherwise.

Return type:

bool

Raises:

KeyError – If feature name is not defined in targets.

Examples

>>> targets = Targets(gender=0.5, age=(35.0, "mean"))
>>> targets.is_binary("gender")
True
>>> targets.is_binary("age")
False
is_continuous(feature: str) bool[source]¶

Check if a feature is continuous.

Parameters:

feature – Feature name to check.

Returns:

True if feature is continuous, False otherwise.

Return type:

bool

Raises:

KeyError – If feature name is not defined in targets.

Examples

>>> targets = Targets(gender=0.5, age=(35.0, "mean"))
>>> targets.is_continuous("age")
True
>>> targets.is_continuous("gender")
False
property n_features: int¶

Get number of features.

Returns:

Number of features defined in these targets.

Return type:

int

Examples

>>> targets = Targets(a=0.5, b=0.3, c=0.7)
>>> targets.n_features
3