Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added capymoa wrapper for OUAE algorithm #184

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/capymoa/classifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ._samknn import SAMkNN
from ._dynamic_weighted_majority import DynamicWeightedMajority
from ._csmote import CSMOTE
from ._online_accuracy_updated_ensemble import OnlineAccuracyUpdatedEnsemble

__all__ = [
"AdaptiveRandomForestClassifier",
Expand All @@ -39,5 +40,6 @@
"HoeffdingAdaptiveTree",
"SAMkNN",
"DynamicWeightedMajority",
"CSMOTE"
]
"CSMOTE",
"OnlineAccuracyUpdatedEnsemble"
]
81 changes: 81 additions & 0 deletions src/capymoa/classifier/_online_accuracy_updated_ensemble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from capymoa.base import MOAClassifier
from moa.classifiers.meta import OnlineAccuracyUpdatedEnsemble as _MOA_OnlineAccuracyUpdatedEnsemble
from capymoa.stream import Schema
from capymoa._utils import build_cli_str_from_mapping_and_locals


class OnlineAccuracyUpdatedEnsemble(MOAClassifier):
"""OnlineAccuracyUpdatedEnsemble

The online version of the Accuracy Updated Ensemble as proposed by
Brzezinski and Stefanowski in "Combining block-based and online methods
in learning ensembles from concept drifting data streams", Information Sciences, 2014.

Reference:

`Combining block-based and online methods in learning ensembles from concept drifting data streams.
Daruisz Brzezinski, Jerzy Stefanowski
IS, 2014.
<https://doi.org/10.1016/j.ins.2013.12.011>`_

Example usages:

>>> from capymoa.datasets import ElectricityTiny
>>> from capymoa.classifier import OnlineAccuracyUpdatedEnsemble
>>> from capymoa.evaluation import prequential_evaluation
>>> stream = ElectricityTiny()
>>> schema = stream.get_schema()
>>> learner = OnlineAccuracyUpdatedEnsemble(schema)
>>> results = prequential_evaluation(stream, learner, max_instances=1000)
>>> results["cumulative"].accuracy()
87.5
"""


def __init__(
self,
schema: Schema,
random_seed: int = 1,
learner_option= 'trees.HoeffdingTree -e 2000000 -g 100 -c 0.01',
member_count_option: int = 5,
window_size_option: float = 50.0,
max_byte_size_option: int = 33554432,
verbose_option: bool = False,
linear_option: bool = False,
):


""" Online Accuracy Updated Ensemble Classifier

:param schema: The schema of the stream.
:param learner_option: Classifier to train.
:param member_count_option: The maximum number of classifiers in an ensemble.
:param window_size_option: The window size used for classifier creation and evaluation.
:param max_byte_size_option: Maximum memory consumed by ensemble.
:param verbose_option: When checked the algorithm outputs additional information about component classifier weights.
:param linear_option: When checked the algorithm uses a linear weighting function.
"""


mapping = {
"learner_option": "-l",
"member_count_option": "-n",
"window_size_option": "-w",
"max_byte_size_option": "-m",
"verbose_option": "-v",
"linear_option": "-f",
}


assert (type(learner_option) == str
), "Only MOA CLI strings are supported for SGBT base_learner, at the moment."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change SGBT to OnlineAccuracyUpdatedEnsemble



config_str = build_cli_str_from_mapping_and_locals(mapping, locals())
self.moa_learner = _MOA_OnlineAccuracyUpdatedEnsemble()
super(OnlineAccuracyUpdatedEnsemble, self).__init__(
schema=schema,
random_seed=random_seed,
CLI=config_str,
moa_learner=self.moa_learner,
)
4 changes: 4 additions & 0 deletions tests/test_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
CSMOTE,
LeveragingBagging,
OnlineAdwinBagging,
OnlineAccuracyUpdatedEnsemble
)

from capymoa.base import Classifier
from capymoa.base import MOAClassifier
from capymoa.datasets import ElectricityTiny
Expand Down Expand Up @@ -62,6 +64,7 @@
(partial(CSMOTE), 80.55, 79.0, None),
(partial(LeveragingBagging), 86.7, 91.0, None),
(partial(OnlineAdwinBagging), 85.25, 92.0, None),
(partial(OnlineAccuracyUpdatedEnsemble), 85.25, 92.0, None),
],
ids=[
"OnlineBagging",
Expand All @@ -77,6 +80,7 @@
"OzaBoost",
"MajorityClass",
"NoChange",
"OnlineAccuracyUpdatedEnsemble",
"OnlineSmoothBoost",
"StreamingRandomPatches",
"HoeffdingAdaptiveTree",
Expand Down
Loading