onlinerake.Targets¶
- class onlinerake.Targets(**kwargs)[source]¶
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 (float | tuple[float, str]) –
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).
Examples
>>> from onlinerake import Targets >>> # Binary features only >>> binary = Targets(owns_car=0.4, is_subscriber=0.2, likes_coffee=0.7) >>> print(binary.feature_names) ['is_subscriber', 'likes_coffee', 'owns_car'] >>> print(binary["owns_car"]) 0.4 >>> print("owns_car" in binary) True
>>> # 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 >>> print(targets.feature_names) ['age', 'college', 'gender', 'income']
- Raises:
ValueError – If any binary target proportion is not between 0 and 1, or if the tuple syntax is malformed.
- Parameters:
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
binary_featuresGet list of binary feature names.
continuous_featuresGet list of continuous feature names.
feature_namesGet ordered list of feature names.
has_continuous_featuresCheck if any continuous features are defined.
n_featuresGet number of features.