River 是一个用于 在线机器学习 的 Python 库。它的目标是成为在流式数据上进行机器学习的最用户友好的库。River 是 creme 和 scikit-multiflow 合并的成果。
作为一个简单示例,我们将训练一个逻辑回归来对 网站钓鱼数据集 进行分类。以下是该数据集中第一条观测数据的样子。
>>> from pprint import pprint
>>> from river import datasets
>>> dataset = datasets.Phishing()
>>> for x, y in dataset:
... pprint(x)
... print(y)
... break
{'age_of_domain': 1,
'anchor_from_other_domain': 0.0,
'empty_server_form_handler': 0.0,
'https': 0.0,
'ip_in_url': 1,
'is_popular': 0.5,
'long_url': 1.0,
'popup_window': 0.0,
'request_from_other_domain': 0.0}
True
现在让我们以流式方式在数据集上运行模型。我们依次交替进行预测和模型更新。同时,我们更新一个性能指标来查看模型的表现如何。
>>> from river import compose
>>> from river import linear_model
>>> from river import metrics
>>> from river import preprocessing
>>> model = compose.Pipeline(
... preprocessing.StandardScaler(),
... linear_model.LogisticRegression()
... )
>>> metric = metrics.Accuracy()
>>> for x, y in dataset:
... y_pred = model.predict_one(x) # make a prediction
... metric.update(y, y_pred) # update the metric
... model.learn_one(x, y) # make the model learn
>>> metric
Accuracy: 89.28%
当然,这只是一个简单的例子。欢迎你查看文档中的 介绍 部分以获得更详细的教程。
River 旨在兼容 Python 3.11 及以上版本。可以通过 pip 进行安装:
pip install river
有适用于 Linux、MacOS 和 Windows 的 wheel 包。这意味着你很可能无需从源码构建 River。
River 的核心在线接口(learn_one / predict_one)不依赖 pandas。小批量接口(learn_many、predict_many、predict_proba_many、transform_many)基于 pandas 构建,并且是可选安装:
pip install "river[pandas]"
你可以从 GitHub 安装最新的开发版本,如下所示:
pip install git+https://github.com/online-ml/river --upgrade
pip install git+ssh://[email protected]/online-ml/river.git --upgrade # using SSH
此方法要求你的机器上安装有 Cython 和 Rust。
River 提供了以下算法家族的在线实现:
River 还提供了其他在线工具:
查看 API 以获取全面的概览。
你应该问问自己是否需要在线机器学习。答案很可能是否定的。大多数情况下,批量学习就能很好地完成任务。如果你符合以下情况,那么在线方法可能正合适:
River 的一些特点包括:
欢迎以任何方式贡献,我们始终对新想法和新方法持开放态度。
如果你想对代码库进行修改,请查看贡献指南。
如果 River 对你有帮助,并且你想在科学论文中引用它,请参考发表在 JMLR 上的 论文:
@article{montiel2021river,
title={River: machine learning for streaming data in Python},
author={Montiel, Jacob and Halford, Max and Mastelini, Saulo Martiello
and Bolmier, Geoffrey and Sourty, Raphael and Vaysse, Robin and Zouitine, Adil
and Gomes, Heitor Murilo and Read, Jesse and Abdessalem, Talel and others},
year={2021}
}
River 是根据 3-Clause BSD 许可证 发布的免费开源软件。