Pipelines¶
Pipelines are an integral part of river. We encourage their usage and apply them in many of their examples.
The compose.Pipeline
contains all the logic for building and applying pipelines. A pipeline is essentially a list of estimators that are applied in sequence. The only requirement is that the first n - 1
steps be transformers. The last step can be a regressor, a classifier, a clusterer, a transformer, etc. Here is an example:
from river import compose
from river import linear_model
from river import preprocessing
from river import feature_extraction
model = compose.Pipeline(
preprocessing.StandardScaler(),
feature_extraction.PolynomialExtender(),
linear_model.LinearRegression()
)
You can also use the |
operator, as so:
model = (
preprocessing.StandardScaler() |
feature_extraction.PolynomialExtender() |
linear_model.LinearRegression()
)
Or, equally:
model = preprocessing.StandardScaler()
model |= feature_extraction.PolynomialExtender()
model |= linear_model.LinearRegression()
A pipeline has a draw
method that can be used to visualize it:
model
StandardScaler
{'counts': Counter(),
'means': defaultdict(<class 'float'>, {}),
'vars': defaultdict(<class 'float'>, {}),
'with_std': True}
PolynomialExtender
{'bias_name': 'bias',
'degree': 2,
'include_bias': False,
'interaction_only': False}
LinearRegression
{'_weights': {},
'_y_name': None,
'clip_gradient': 1000000000000.0,
'initializer': Zeros (),
'intercept': 0.0,
'intercept_init': 0.0,
'intercept_lr': Constant({'learning_rate': 0.01}),
'l2': 0.0,
'loss': Squared({}),
'optimizer': SGD({'lr': Constant({'learning_rate': 0.01}), 'n_iterations': 0})}
compose.Pipeline
inherits from base.Estimator
, which means that it has a learn_one
method. You would expect learn_one
to update each estimator, but that's not actually what happens. Instead, the transformers are updated when predict_one
(or predict_proba_one
for that matter) is called. Indeed, in online machine learning, we can update the unsupervised parts of our model when a sample arrives. We don't have to wait for the ground truth to arrive in order to update unsupervised estimators that don't depend on it. In other words, in a pipeline, learn_one
updates the supervised parts, whilst predict_one
updates the unsupervised parts. It's important to be aware of this behavior, as it is quite different to what is done in other libraries that rely on batch machine learning.
Here is a small example to illustrate the previous point:
from river import datasets
dataset = datasets.TrumpApproval()
x, y = next(iter(dataset))
x, y
({'ordinal_date': 736389,
'gallup': 43.843213,
'ipsos': 46.19925042857143,
'morning_consult': 48.318749,
'rasmussen': 44.104692,
'you_gov': 43.636914000000004},
43.75505)
Let us call predict_one
, which will update each transformer, but won't update the linear regression.
model.predict_one(x)
0.0
The prediction is nil because each weight of the linear regression is equal to 0.
model['StandardScaler'].means
defaultdict(float,
{'ordinal_date': 736389.0,
'gallup': 43.843213,
'ipsos': 46.19925042857143,
'morning_consult': 48.318749,
'rasmussen': 44.104692,
'you_gov': 43.636914000000004})
As we can see, the means of each feature have been updated, even though we called predict_one
and not learn_one
.
Note that if you call transform_one
with a pipeline who's last step is not a transformer, then the output from the last transformer (which is thus the penultimate step) will be returned:
model.transform_one(x)
{'ordinal_date': 0.0,
'gallup': 0.0,
'ipsos': 0.0,
'morning_consult': 0.0,
'rasmussen': 0.0,
'you_gov': 0.0,
'ordinal_date*ordinal_date': 0.0,
'gallup*ordinal_date': 0.0,
'ipsos*ordinal_date': 0.0,
'morning_consult*ordinal_date': 0.0,
'ordinal_date*rasmussen': 0.0,
'ordinal_date*you_gov': 0.0,
'gallup*gallup': 0.0,
'gallup*ipsos': 0.0,
'gallup*morning_consult': 0.0,
'gallup*rasmussen': 0.0,
'gallup*you_gov': 0.0,
'ipsos*ipsos': 0.0,
'ipsos*morning_consult': 0.0,
'ipsos*rasmussen': 0.0,
'ipsos*you_gov': 0.0,
'morning_consult*morning_consult': 0.0,
'morning_consult*rasmussen': 0.0,
'morning_consult*you_gov': 0.0,
'rasmussen*rasmussen': 0.0,
'rasmussen*you_gov': 0.0,
'you_gov*you_gov': 0.0}
In many cases, you might want to connect a step to multiple steps. For instance, you might to extract different kinds of features from a single input. An elegant way to do this is to use a compose.TransformerUnion
. Essentially, the latter is a list of transformers who's results will be merged into a single dict
when transform_one
is called. As an example let's say that we want to apply a feature_extraction.RBFSampler
as well as the feature_extraction.PolynomialExtender
. This may be done as so:
model = (
preprocessing.StandardScaler() |
(feature_extraction.PolynomialExtender() + feature_extraction.RBFSampler()) |
linear_model.LinearRegression()
)
model
StandardScaler
{'counts': Counter(),
'means': defaultdict(<class 'float'>, {}),
'vars': defaultdict(<class 'float'>, {}),
'with_std': True}
PolynomialExtender
{'bias_name': 'bias',
'degree': 2,
'include_bias': False,
'interaction_only': False}
RBFSampler
{'gamma': 1.0,
'n_components': 100,
'offsets': [5.438958997841155,
2.2672794029610905,
0.8944821109449446,
2.948499778474644,
4.264669849859532,
0.2598339130425727,
1.8949286401538479,
2.053815342724336,
4.6759253606152065,
0.26560668365416235,
2.5325389138985033,
3.0607984977578857,
0.05824240801657245,
5.875079926012834,
5.142655743510883,
2.9888483108963686,
2.4526790503328213,
6.146309651781183,
1.7647348679570503,
3.4832204721662947,
3.471131294606353,
5.050399652518423,
4.933307380039405,
3.5778318457110445,
2.9451623791650605,
2.913441903381986,
0.5002134128869599,
2.0779112135971336,
5.904816577149786,
2.180551775369539,
0.11887249948331317,
4.880368011502154,
1.4320679037680817,
2.2838411543690884,
2.2676380940269727,
5.466317568846288,
2.1420203183506095,
3.1186698569750173,
3.7771430031962328,
1.1607828604613544,
5.278625118145599,
4.810865395326483,
4.000254957445151,
2.3906845399339387,
2.326012246867101,
4.12441464112942,
0.7520452546555718,
1.7634605391840485,
2.5372144833493935,
1.2601280867556905,
3.7018904566125044,
0.01960703322115244,
5.090922139023127,
3.5812831924996056,
4.494328184504662,
3.233843753584034,
5.740912513357241,
3.9595023798599223,
4.543542353419292,
2.8587943611991258,
3.697132489252557,
3.605691463450341,
0.4520255230950608,
3.8501900660144113,
4.867228392553649,
4.241432301737288,
1.7098053418951003,
0.07993079424364441,
4.7004736069244055,
1.3623733525646258,
4.978979842503292,
0.17599465532233088,
5.827942718321521,
0.19743948386122365,
2.9865628235647725,
1.921943038350649,
3.76112519126591,
4.734064062721446,
3.2435045226777484,
3.0482955481027383,
0.28670363589487313,
6.161187810710436,
4.534764465033644,
2.1751195737384106,
5.681415920284711,
5.8413524900034455,
3.5150851909510936,
4.935033011456816,
3.183023629109263,
0.252088151520186,
0.44614440179036796,
1.5512999716020148,
0.8891404644127363,
5.862164440514792,
2.865291496349226,
3.9007411388684012,
3.08931689785725,
1.1535714882198016,
1.9492991870595326,
3.723295916248842],
'rng': <random.Random object at 0x559625db55d0>,
'seed': None,
'weights': defaultdict(<bound method RBFSampler._random_weights of RBFSampler (
gamma=1.
n_components=100
seed=None
)>, {})}
LinearRegression
{'_weights': {},
'_y_name': None,
'clip_gradient': 1000000000000.0,
'initializer': Zeros (),
'intercept': 0.0,
'intercept_init': 0.0,
'intercept_lr': Constant({'learning_rate': 0.01}),
'l2': 0.0,
'loss': Squared({}),
'optimizer': SGD({'lr': Constant({'learning_rate': 0.01}), 'n_iterations': 0})}
Note that the +
symbol acts as a shorthand notation for creating a compose.TransformerUnion
, which means that we could have declared the above pipeline as so:
model = (
preprocessing.StandardScaler() |
compose.TransformerUnion(
feature_extraction.PolynomialExtender(),
feature_extraction.RBFSampler()
) |
linear_model.LinearRegression()
)
Pipelines provide the benefit of removing a lot of cruft by taking care of tedious details for you. They also enable to clearly define what steps your model is made of. Finally, having your model in a single object means that you can move it around more easily. Note that you can include user-defined functions in a pipeline by using a compose.FuncTransformer
.