Skip to content

Commit

Permalink
feat: add nochange and majority class classifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
nuwangunasekara authored and hmgomes committed May 2, 2024
1 parent 7c1507f commit 0c822c1
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/capymoa/classifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from ._knn import KNN
from ._sgbt import StreamingGradientBoostedTrees
from ._oza_boost import OzaBoost
from ._majority_class import MajorityClass
from ._no_change import NoChange

__all__ = [
"AdaptiveRandomForest",
Expand All @@ -20,5 +22,7 @@
"PassiveAggressiveClassifier",
"SGDClassifier",
"StreamingGradientBoostedTrees",
"OzaBoost"
"OzaBoost",
"MajorityClass",
"NoChange"
]
48 changes: 48 additions & 0 deletions src/capymoa/classifier/_majority_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations
from typing import Union

from capymoa.base import (
MOAClassifier,
)
from capymoa.stream import Schema
from capymoa._utils import build_cli_str_from_mapping_and_locals

from moa.classifiers.functions import MajorityClass as _MOA_MajorityClass


class MajorityClass(MOAClassifier):
"""Majority class classifier.
Always predicts the class that has been observed most frequently the in the training data.
Example usages:
>>> from capymoa.datasets import ElectricityTiny
>>> from capymoa.classifier import MajorityClass
>>> from capymoa.evaluation import prequential_evaluation
>>> stream = ElectricityTiny()
>>> schema = stream.get_schema()
>>> learner = MajorityClass(schema)
>>> results = prequential_evaluation(stream, learner, max_instances=1000)
>>> results["cumulative"].accuracy()
50.2
"""

def __init__(
self,
schema: Schema | None = None,
):
"""Majority class classifier.
:param schema: The schema of the stream.
"""

mapping = {
}

config_str = build_cli_str_from_mapping_and_locals(mapping, locals())
super(MajorityClass, self).__init__(
moa_learner=_MOA_MajorityClass,
schema=schema,
CLI=config_str,
)
48 changes: 48 additions & 0 deletions src/capymoa/classifier/_no_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations
from typing import Union

from capymoa.base import (
MOAClassifier,
)
from capymoa.stream import Schema
from capymoa._utils import build_cli_str_from_mapping_and_locals

from moa.classifiers.functions import NoChange as _MOA_NoChange


class NoChange(MOAClassifier):
"""NoChange classifier.
Always predicts the last class seen.
Example usages:
>>> from capymoa.datasets import ElectricityTiny
>>> from capymoa.classifier import NoChange
>>> from capymoa.evaluation import prequential_evaluation
>>> stream = ElectricityTiny()
>>> schema = stream.get_schema()
>>> learner = NoChange(schema)
>>> results = prequential_evaluation(stream, learner, max_instances=1000)
>>> results["cumulative"].accuracy()
85.9
"""

def __init__(
self,
schema: Schema | None = None,
):
"""NoChange class classifier.
:param schema: The schema of the stream.
"""

mapping = {
}

config_str = build_cli_str_from_mapping_and_locals(mapping, locals())
super(NoChange, self).__init__(
moa_learner=_MOA_NoChange,
schema=schema,
CLI=config_str,
)
10 changes: 8 additions & 2 deletions tests/test_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
NaiveBayes,
KNN,
StreamingGradientBoostedTrees,
OzaBoost
OzaBoost,
MajorityClass,
NoChange
)
from capymoa.base import Classifier
from capymoa.base import MOAClassifier
Expand Down Expand Up @@ -42,6 +44,8 @@
(partial(SGDClassifier), 84.7, 83.0, None),
(partial(StreamingGradientBoostedTrees), 88.75, 88.0, None),
(partial(OzaBoost), 89.95, 89.0, None),
(partial(MajorityClass), 60.199999999999996, 66.0, None),
(partial(NoChange), 85.95, 81.0, None),
],
ids=[
"OnlineBagging",
Expand All @@ -54,7 +58,9 @@
"PassiveAggressiveClassifier",
"SGDClassifier",
"StreamingGradientBoostedTrees",
"OzaBoost"
"OzaBoost",
"MajorityClass",
"NoChange"
],
)
def test_classifiers(
Expand Down

0 comments on commit 0c822c1

Please sign in to comment.