Lagger¶
Uses lagged values as features.
Parameters¶
-
by (Union[str, List[str], NoneType]) – defaults to
None
An optional feature by which to group the lagged values.
-
drop_nones – defaults to
True
Whether or not features should be included with a
None
value if not enough values have been seen yet. -
lags (Dict[str, Union[int, Tuple[int]]])
Indicates which lags to compute for each feature. This may be specified as a single number, or as a tuple of numbers.
Examples¶
Let's say we have daily data about the number of customers and the revenue for two shops.
>>> X = [
... {'shop': '7/11', 'customers': 10, 'revenue': 420},
... {'shop': 'Kmart', 'customers': 10, 'revenue': 386},
... {'shop': '7/11', 'customers': 20, 'revenue': 145},
... {'shop': 'Kmart', 'customers': 5, 'revenue': 87},
... {'shop': '7/11', 'customers': 15, 'revenue': 276},
... {'shop': 'Kmart', 'customers': 10, 'revenue': 149},
... {'shop': '7/11', 'customers': 30, 'revenue': 890},
... {'shop': 'Kmart', 'customers': 40, 'revenue': 782},
... {'shop': '7/11', 'customers': 20, 'revenue': 403},
... {'shop': 'Kmart', 'customers': 35, 'revenue': 218},
... ]
At each time step, we want to use the number of customers and revenue from 2 time steps ago:
>>> from river.feature_extraction import Lagger
>>> lagger = Lagger(customers=2, revenue=2)
>>> for x in X:
... lagger = lagger.learn_one(x)
... print(lagger.transform_one(x))
{}
{}
{'customers-2': 10, 'revenue-2': 420}
{'customers-2': 10, 'revenue-2': 386}
{'customers-2': 20, 'revenue-2': 145}
{'customers-2': 5, 'revenue-2': 87}
{'customers-2': 15, 'revenue-2': 276}
{'customers-2': 10, 'revenue-2': 149}
{'customers-2': 30, 'revenue-2': 890}
{'customers-2': 40, 'revenue-2': 782}
We can also specify multiple lags for a given feature.
>>> lagger = Lagger(customers=[1, 2, 3])
>>> for x in X:
... lagger = lagger.learn_one(x)
... print(lagger.transform_one(x))
{}
{'customers-1': 10}
{'customers-1': 10, 'customers-2': 10}
{'customers-1': 20, 'customers-2': 10, 'customers-3': 10}
{'customers-1': 5, 'customers-2': 20, 'customers-3': 10}
{'customers-1': 15, 'customers-2': 5, 'customers-3': 20}
{'customers-1': 10, 'customers-2': 15, 'customers-3': 5}
{'customers-1': 30, 'customers-2': 10, 'customers-3': 15}
{'customers-1': 40, 'customers-2': 30, 'customers-3': 10}
{'customers-1': 20, 'customers-2': 40, 'customers-3': 30}
As of now, we're looking at lagged values over all the data. It might make more sense to look at the lags per shop.
>>> lagger = Lagger(customers=1, by='shop')
>>> for x in X:
... lagger = lagger.learn_one(x)
... print(lagger.transform_one(x))
{}
{}
{'customers-1_by_shop': 10}
{'customers-1_by_shop': 10}
{'customers-1_by_shop': 20}
{'customers-1_by_shop': 5}
{'customers-1_by_shop': 15}
{'customers-1_by_shop': 10}
{'customers-1_by_shop': 30}
{'customers-1_by_shop': 40}
Past values are stored in a window. At the beginning, these windows are empty. By default,
features are omitted if the window is not long enough. You can also include them with a value
of None
:
>>> lagger = Lagger(drop_nones=False, customers=1, by='shop')
>>> for x in X:
... lagger = lagger.learn_one(x)
... print(lagger.transform_one(x))
{'customers-1_by_shop': None}
{'customers-1_by_shop': None}
{'customers-1_by_shop': 10}
{'customers-1_by_shop': 10}
{'customers-1_by_shop': 20}
{'customers-1_by_shop': 5}
{'customers-1_by_shop': 15}
{'customers-1_by_shop': 10}
{'customers-1_by_shop': 30}
{'customers-1_by_shop': 40}
Note that lags can also be specified with a dict
:
>>> lags = {'customers': 2, 'revenue': 2}
>>> lagger = Lagger(**lags)
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
.
A lot of transformers don't actually have to do anything during the learn_one
step because they are stateless. For this reason the default behavior of this function is to do nothing. Transformers that however do something during the learn_one
can override this method.
Parameters
- x (dict)
Returns
Transformer: self
transform_one
Transform a set of features x
.
Parameters
- x (dict)
Returns
dict: The transformed values.