Skip to main content

Incremental machine learning in Python

Project description

creme_logo

creme is a library for online machine learning, also known as incremental learning. Online learning is a machine learning regime where a model learns one observation at a time. This is in contrast to batch learning where all the data is processed in one go. Incremental learning is desirable when the data is too big to fit in memory, or simply when you want to handle streaming data. In addition to many online machine learning algorithms, creme provides utilities for extracting features from a stream of data.

Here are some benefits of using creme (and online machine learning in general):

  • Incremental: models can update themselves in real-time.
  • Adaptive: models can adapt to concept drift.
  • Production-ready: models that work in development can naturally be brought into production.
  • Efficient: models don't have to be retrained and require little compute power, which lowers their carbon footprint

Useful links

Installation

:point_up: creme is intended to work with Python 3.6 and above.

creme can simply be installed with pip.

pip install creme

You can also install the bleeding edge version as so:

pip install git+https://github.com/creme-ml/creme
# Or through SSH:
pip install git+ssh://git@github.com/creme-ml/creme.git

If you're looking to contribute to creme and want to have a development setup, then please check out the contribution guidelines.

Example

In the following example we'll use a linear regression to forecast the number of available bikes in bike stations from the city of Toulouse. Each observation looks like this:

>>> import pprint
>>> from creme import datasets

>>> X_y = datasets.fetch_bikes()
>>> x, y = next(X_y)

>>> pprint.pprint(x)
{'clouds': 75,
 'description': 'light rain',
 'humidity': 81,
 'moment': datetime.datetime(2016, 4, 1, 0, 0, 7),
 'pressure': 1017.0,
 'station': 'metro-canal-du-midi',
 'temperature': 6.54,
 'wind': 9.3}

>>> print(f'Number of bikes: {y}')
Number of bikes: 1

We will include all the available numeric features in our model. We will also use target encoding by calculating a running average of the target per station and hour. Before being fed to the linear regression, the features will be scaled using a StandardScaler. Note that each of these steps works in a streaming fashion, including the feature extraction. We'll evaluate the model by asking it to forecast 30 minutes ahead while delaying the true answers, which ensures that we're simulating a production scenario. Finally we will print the current score every 20,000 predictions.

>>> import datetime as dt
>>> from creme import compose
>>> from creme import datasets
>>> from creme import feature_extraction
>>> from creme import linear_model
>>> from creme import metrics
>>> from creme import model_selection
>>> from creme import preprocessing
>>> from creme import stats

>>> X_y = datasets.fetch_bikes()

>>> def add_hour(x):
...     x['hour'] = x['moment'].hour
...     return x

>>> model = compose.Whitelister('clouds', 'humidity', 'pressure', 'temperature', 'wind')
>>> model += (
...     add_hour |
...     feature_extraction.TargetAgg(by=['station', 'hour'], how=stats.Mean())
... )
>>> model += feature_extraction.TargetAgg(by='station', how=stats.EWMean(0.5))
>>> model |= preprocessing.StandardScaler()
>>> model |= linear_model.LinearRegression()

>>> model_selection.online_qa_score(
...     X_y=X_y,
...     model=model,
...     metric=metrics.MAE(),
...     on='moment',
...     lag=dt.timedelta(minutes=30),
...     print_every=30_000
... )
[30,000] MAE: 2.193069
[60,000] MAE: 2.249345
[90,000] MAE: 2.288321
[120,000] MAE: 2.265257
[150,000] MAE: 2.2674
[180,000] MAE: 2.282485
MAE: 2.285921

You can visualize the pipeline as so:

>>> model
Pipeline (
    TransformerUnion (
        Whitelister (
            whitelist=['clouds', 'humidity', 'pressure', 'temperature', 'wind']
        ),
        Pipeline (
            FuncTransformer (
                func=add_hour
            ),
            TargetAgg (
                by=['station', 'hour']
                how=Mean: 0.
                target_name='target'
            )
        ),
        TargetAgg (
            by=['station']
            how=EWMean: 0.
            target_name='target'
        )
    ),
    StandardScaler (),
    LinearRegression (
        optimizer=SGD
        loss=Squared
        l2=0.0001
        intercept=0.0
        intercept_lr=0.01
        clip_gradient=1000000000000.0
    )
)

We can also draw the pipeline.

>>> dot = model.draw()
bikes_pipeline

By only using a few lines of code, we've built a robust model and evaluated it by simulating a production scenario. You can find a more detailed version of this example here. creme is a framework that has a lot to offer, and as such we kindly refer you to the documentation if you want to know more.

Contributing

Like many subfields of machine learning, online learning is far from being an exact science and so there is still a lot to do. Feel free to contribute in any way you like, we're always open to new ideas and approaches. If you want to contribute to the code base please check out the CONTRIBUTING.md file. Also take a look at the issue tracker and see if anything takes your fancy.

Last but not least you are more than welcome to share with us on how you're using creme or online learning in general! We believe that online learning solves a lot of pain points in practice, and would love to share experiences.

This project follows the all-contributors specification. Contributions of any kind are welcome!

Max Halford
Max Halford

📆 💻
AdilZouitine
AdilZouitine

💻
Raphael Sourty
Raphael Sourty

💻
Geoffrey Bolmier
Geoffrey Bolmier

💻
vincent d warmerdam
vincent d warmerdam

💻
VaysseRobin
VaysseRobin

💻
Lygon Bowen-West
Lygon Bowen-West

💻
Florent Le Gac
Florent Le Gac

💻
Adrian Rosebrock
Adrian Rosebrock

📝
Jovan Veljanoski
Jovan Veljanoski

💻

License

See the license file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

creme-0.4.4.tar.gz (327.3 kB view hashes)

Uploaded Source

Built Distributions

creme-0.4.4-cp37-cp37m-win_amd64.whl (558.7 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

creme-0.4.4-cp37-cp37m-win32.whl (531.4 kB view hashes)

Uploaded CPython 3.7m Windows x86

creme-0.4.4-cp37-cp37m-manylinux2010_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

creme-0.4.4-cp37-cp37m-manylinux2010_i686.whl (1.0 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

creme-0.4.4-cp37-cp37m-manylinux1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.7m

creme-0.4.4-cp37-cp37m-manylinux1_i686.whl (1.0 MB view hashes)

Uploaded CPython 3.7m

creme-0.4.4-cp37-cp37m-macosx_10_6_intel.whl (730.5 kB view hashes)

Uploaded CPython 3.7m macOS 10.6+ intel

creme-0.4.4-cp36-cp36m-win_amd64.whl (559.3 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

creme-0.4.4-cp36-cp36m-win32.whl (531.8 kB view hashes)

Uploaded CPython 3.6m Windows x86

creme-0.4.4-cp36-cp36m-manylinux2010_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

creme-0.4.4-cp36-cp36m-manylinux2010_i686.whl (1.0 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

creme-0.4.4-cp36-cp36m-manylinux1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.6m

creme-0.4.4-cp36-cp36m-manylinux1_i686.whl (1.0 MB view hashes)

Uploaded CPython 3.6m

creme-0.4.4-cp36-cp36m-macosx_10_6_intel.whl (735.8 kB view hashes)

Uploaded CPython 3.6m macOS 10.6+ intel

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page