Skip to content

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.40319886163907, 1.6410242429846016, 2.9102505212582352, 1.2502497289471197, 1.6599819506345908, 0.9905000367712694, 2.733090767224682, 4.620746211242217, 2.852398674434833, 2.476005155469144, 3.7388007297528816, 5.646400674828576, 3.6227497381819425, 4.749089309393084, 1.4884201177246816, 0.6620783374639668, 5.373190414753098, 5.275452969586568, 3.2621017095010485, 5.393146766246767, 1.223265495606927, 2.0411736263752953, 5.553580538930083, 4.10986679876461, 6.235554966861734, 0.4643626338300455, 0.7817900181257065, 0.8011940519896001, 4.052099753388222, 2.909807284332097, 3.34510955200856, 0.3493140121007998, 5.402607818879578, 3.3087431285569493, 1.8770747192053943, 4.01284630848113, 2.474480980660175, 0.2501484424124193, 5.8711331722594995, 4.057584696495738, 3.8164961475303794, 0.06634520341680332, 5.8565647315792235, 0.1847353689415499, 5.382451632052383, 3.011163589617232, 1.7522981577704155, 4.2331865374090984, 2.9867710560333927, 4.433570469239939, 2.4306908395425553, 4.410215591556303, 5.380236343065923, 2.640146753638325, 3.2230721786004044, 4.287528278000641, 3.7882931545627048, 4.667545445964917, 2.921414671761127, 1.625105282481997, 5.351157082446773, 2.2249830555738033, 0.41897902600140907, 4.798923547622566, 0.5031333518481544, 3.2612135038369066, 2.9675308240998395, 1.1389886230155741, 0.6804135746493921, 5.001111709332776, 1.3026367122959899, 5.442518462695327, 2.6475839991470536, 3.2467017672206873, 2.5583103981305917, 4.121664457069394, 1.6644131911945859, 5.635212438790749, 2.0520138088916995, 2.7215315131799485, 1.2082918732869392, 0.6419157988676313, 5.355430804977489, 3.863611291623673, 5.493444748277504, 3.9141646461679795, 4.702303680693544, 2.6397887943831235, 3.4407364580864517, 0.7644840870081152, 5.025314138394125, 3.1860810516030793, 0.6846299445357678, 3.901352049120776, 0.7555553455686381, 3.7131726729981533, 2.0153584522084187, 5.571985454181369, 0.006569980732336922, 2.7015961406802824], 'rng': <random.Random object at 0x7fb947bf3610>, '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.