Skip to main content

stream-learn

CircleCI codecov Documentation Status PyPI version

The stream-learn module is a set of tools necessary for processing data streams using scikit-learn estimators. The batch processing approach is used here, where the dataset is passed to the classifier in smaller, consecutive subsets called chunks. The module consists of five sub-modules:

  • streams - containing a data stream generator that allows obtaining both stationary and dynamic distributions in accordance with various types of concept drift (also in the field of a priori probability, i.e. dynamically unbalanced data) and a parser of the standard ARFF file format.
  • evaluators - containing classes for running experiments on stream data in accordance with the Test-Then-Train and Prequential methodology.
  • classifiers - containing sample stream classifiers,
  • ensembles - containing standard hybrid models of stream data classification,
  • metrics - containing typical classification quality metrics in data streams.

You can read more about each module in the documentation page.

Citation policy

If you use stream-learn in a scientific publication, we would appreciate citation to the following paper:

@article{Ksieniewicz2022,
  doi = {10.1016/j.neucom.2021.10.120},
  url = {https://doi.org/10.1016/j.neucom.2021.10.120},
  year = {2022},
  month = jan,
  publisher = {Elsevier {BV}},
  author = {P. Ksieniewicz and P. Zyblewski},
  title = {stream-learn {\textemdash} open-source Python library for difficult data stream batch analysis},
  journal = {Neurocomputing}
}

Quick start guide

Installation

To use the stream-learn package, it will be absolutely useful to install it. Fortunately, it is available in the PyPI repository, so you may install it using pip:

pip3 install -U stream-learn

stream-learn is also avaliable with conda:

conda install stream-learn -c w4k2 -c conda-forge

You can also install the module cloned from Github using the setup.py file if you have a strange, but perhaps legitimate need:

git clone https://github.com/w4k2/stream-learn.git
cd stream-learn
make install

Preparing experiments

1. Classifier

In order to conduct experiments, a declaration of four elements is necessary. The first is the estimator, which must be compatible with the scikit-learn API and, in addition, implement the partial_fit() method, allowing you to re-fit the already built model. For example, we'll use the standard Gaussian Naive Bayes algorithm:

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()

2. Data Stream

The next element is the data stream that we aim to process. In the example we will use a synthetic stream consisting of shocking number of 100 chunks and containing precisely one concept drift. We will prepare it using the StreamGenerator() class of the stream-learn module:

from strlearn.streams import StreamGenerator
stream = StreamGenerator(n_chunks=100, n_drifts=1)

3. Metrics

The third requirement of the experiment is to specify the metrics used in the evaluation of the methods. In the example, we will use the accuracy metric available in scikit-learn and the precision from the stream-learn module:

from sklearn.metrics import accuracy_score
from strlearn.metrics import precision
metrics = [accuracy_score, precision]

4. Evaluator

The last necessary element of processing is the evaluator, i.e. the method of conducting the experiment. For example, we will choose the Test-Then-Train paradigm, described in more detail in User Guide. It is important to note, that we need to provide the metrics that we will use in processing at the point of initializing the evaluator. In the case of none metrics given, it will use default pair of accuracy and balanced accuracy scores:

from strlearn.evaluators import TestThenTrain
evaluator = TestThenTrain(metrics)

Processing and understanding results

Once all processing requirements have been met, we can proceed with the evaluation. To start processing, call the evaluator's process method, feeding it with the stream and classifier::

evaluator.process(stream, clf)

The results obtained are stored in the scores atribute of evaluator. If we print it on the screen, we may be able to observe that it is a three-dimensional numpy array with dimensions (1, 29, 2).

  • The first dimension is the index of a classifier submitted for processing. In the example above, we used only one model, but it is also possible to pass a tuple or list of classifiers that will be processed in parallel (See User Guide).
  • The second dimension specifies the instance of evaluation, which in the case of Test-Then-Train methodology directly means the index of the processed chunk.
  • The third dimension indicates the metric used in the processing.

Using this knowledge, we may finally try to illustrate the results of our simple experiment in the form of a plot::

import matplotlib.pyplot as plt

plt.figure(figsize=(6,3))

for m, metric in enumerate(metrics):
    plt.plot(evaluator.scores[0, :, m], label=metric.__name__)

plt.title("Basic example of stream processing")
plt.ylim(0, 1)
plt.ylabel('Quality')
plt.xlabel('Chunk')

plt.legend()

Metadata

Release files for stream-learn 0.9.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stream-learn 0.9.2
File Size Uploaded
stream_learn-0.9.2.tar.gz 56.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stream-learn 0.9.2
File Interpreter ABI Platform
stream_learn-0.9.2-py3-none-any.whl Python 3 none any Details

Total release size: 142.0 kB

Release files / stream_learn-0.9.2.tar.gz

Download URL stream_learn-0.9.2.tar.gz
Size 56.8 kB
Tags Source
SHA-256 checksum
How to use checksums
26359af0fcf0b051027127d69464be1a77c2cf208173127dc638a1e790834d11
BLAKE2b-256 checksum
How to use checksums
c6cfe6574cf5ad92ddd288a27c04842e25279bff16d680e2b2daf983596ddd11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / stream_learn-0.9.2-py3-none-any.whl

Download URL stream_learn-0.9.2-py3-none-any.whl
Size 85.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d618988a418a79c08c0de78b0eefe6b774fa297c3e4e8c94c1f5e7a51fbb8f00
BLAKE2b-256 checksum
How to use checksums
a3de3baa622f51e593cdcee92a3d0f65a8c434d3c50ea864af170324b32e7abf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release history Release notifications | RSS feed

This release

0.9.2 This release

2 release files

0.9.1

1 release file

0.8.24

2 release files

0.8.20

1 release file

0.8.18

2 release files

0.8.16

2 release files

0.8.14

2 release files

0.8.13

2 release files

0.8.11

2 release files

0.8.10

2 release files

0.8.8

2 release files

0.8.7

2 release files

0.8.6

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

1 release file

0.8

1 release file

0.6

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2.1

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page