TargetAgg¶
Computes a streaming aggregate of the target values.
This transformer is identical to feature_extraction.Agg
, the only difference is that it operates on the target rather than on a feature. At each step, the running statistic how
of target values in group by
is updated with the target. It is therefore a supervised transformer.
Parameters¶
-
by (Union[str, List[str], NoneType])
The feature by which to group the target values. All the data is included in the aggregate if this is
None
. -
how (river.stats.base.Univariate)
The statistic to compute.
-
target_name – defaults to
y
The target name which is used in the result.
Attributes¶
-
state
Return the current values for each group. Examples -------- >>> X = [ ... {"country": "France", "place": "Taco Bell", "revenue": 42}, ... {"country": "Sweden", "place": "Burger King", "revenue": 16}, ... {"country": "France", "place": "Burger King", "revenue": 24}, ... {"country": "Sweden", "place": "Taco Bell", "revenue": 58}, ... {"country": "Sweden", "place": "Burger King", "revenue": 20}, ... {"country": "France", "place": "Taco Bell", "revenue": 50}, ... {"country": "France", "place": "Burger King", "revenue": 10}, ... {"country": "Sweden", "place": "Taco Bell", "revenue": 80}, ... ] >>> from river import feature_extraction as fx >>> from river import stats >>> agg = fx.Agg(on="revenue", by="place", how=stats.Mean()) >>> for x in X: ... agg = agg.learn_one(x) >>> agg.state Taco Bell 57.5 Burger King 17.5 Name: revenue_mean_by_place, dtype: float64 >>> agg = fx.Agg(on="revenue", by=["country", "place"], how=stats.Mean()) >>> for x in X: ... agg = agg.learn_one(x) >>> agg.state country place France Taco Bell 46.0 Sweden Burger King 18.0 France Burger King 17.0 Sweden Taco Bell 69.0 Name: revenue_mean_by_country_and_place, dtype: float64
-
target_name
Examples¶
Consider the following dataset, where the second value of each value is the target:
>>> dataset = [
... ({'country': 'France', 'place': 'Taco Bell'}, 42),
... ({'country': 'Sweden', 'place': 'Burger King'}, 16),
... ({'country': 'France', 'place': 'Burger King'}, 24),
... ({'country': 'Sweden', 'place': 'Taco Bell'}, 58),
... ({'country': 'Sweden', 'place': 'Burger King'}, 20),
... ({'country': 'France', 'place': 'Taco Bell'}, 50),
... ({'country': 'France', 'place': 'Burger King'}, 10),
... ({'country': 'Sweden', 'place': 'Taco Bell'}, 80)
... ]
As an example, let's perform a target encoding of the place
feature. Instead of simply
updating a running average, we use a stats.BayesianMean
which allows us to incorporate
some prior knowledge. This makes subsequent models less prone to overfitting. Indeed, it
dampens the fact that too few samples might have been seen within a group.
>>> from river import feature_extraction
>>> from river import stats
>>> agg = feature_extraction.TargetAgg(
... by='place',
... how=stats.BayesianMean(
... prior=3,
... prior_weight=1
... )
... )
>>> for x, y in dataset:
... print(agg.transform_one(x))
... agg = agg.learn_one(x, y)
{'y_bayes_mean_by_place': 3.0}
{'y_bayes_mean_by_place': 3.0}
{'y_bayes_mean_by_place': 9.5}
{'y_bayes_mean_by_place': 22.5}
{'y_bayes_mean_by_place': 14.333}
{'y_bayes_mean_by_place': 34.333}
{'y_bayes_mean_by_place': 15.75}
{'y_bayes_mean_by_place': 38.25}
Just like with feature_extraction.Agg
, we can specify multiple features on which to
group the data:
>>> agg = feature_extraction.TargetAgg(
... by=['place', 'country'],
... how=stats.BayesianMean(
... prior=3,
... prior_weight=1
... )
... )
>>> for x, y in dataset:
... print(agg.transform_one(x))
... agg = agg.learn_one(x, y)
{'y_bayes_mean_by_place_and_country': 3.0}
{'y_bayes_mean_by_place_and_country': 3.0}
{'y_bayes_mean_by_place_and_country': 3.0}
{'y_bayes_mean_by_place_and_country': 3.0}
{'y_bayes_mean_by_place_and_country': 9.5}
{'y_bayes_mean_by_place_and_country': 22.5}
{'y_bayes_mean_by_place_and_country': 13.5}
{'y_bayes_mean_by_place_and_country': 30.5}
>>> agg.state
place country
Taco Bell France 31.666667
Burger King Sweden 13.000000
France 12.333333
Taco Bell Sweden 47.000000
Name: y_bayes_mean_by_place_and_country, dtype: float64
Methods¶
clone
Return a fresh estimator with the same parameters.
The clone has the same parameters but has not been updated with any data. This works by looking at the parameters from the class signature. Each parameter is either - recursively cloned if it's a River classes. - deep-copied via copy.deepcopy
if not. If the calling object is stochastic (i.e. it accepts a seed parameter) and has not been seeded, then the clone will not be idempotent. Indeed, this method's purpose if simply to return a new instance with the same input parameters.
learn_one
Update with a set of features x
and a target y
.
Parameters
- x (dict)
- y
Returns
SupervisedTransformer: self
transform_one
Transform a set of features x
.
Parameters
- x (dict)
Returns
dict: The transformed values.