onlinerake.Targets¶
- class onlinerake.Targets(**kwargs: float | tuple[float, str])[source]¶
Bases:
objectTarget 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).
- 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.
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
Get list of binary feature names.
Get list of continuous feature names.
Get ordered list of feature names.
Check if any continuous features are defined.
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:
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.
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.
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.
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:
- 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:
- 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:
- 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:
- 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