-
Notifications
You must be signed in to change notification settings - Fork 3
/
_majority_classifier.py
79 lines (64 loc) · 2.59 KB
/
_majority_classifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from typing import Any, Dict
from typing_extensions import Self
from truelearn.models import EventModel
from truelearn.base import BaseClassifier
from .._constraint import TypeConstraint
class MajorityClassifier(BaseClassifier):
"""A classifier that predicts based on \
the number of learner's engagement and non-engagement.
If the number of engagements on the training data is greater than
the number of non-engagement, the classifier predicts Engage (True);
otherwise, it predicts Non-Engage (False).
Examples:
>>> from truelearn.learning import MajorityClassifier
>>> from truelearn.models import EventModel
>>> majority = MajorityClassifier()
>>> majority
MajorityClassifier()
>>> # prepare an event model with empty knowledge
>>> events = [EventModel(), EventModel(), EventModel()]
>>> engage_stats = [False, True, True]
>>> for event, engage_stats in zip(events, engage_stats):
... majority = majority.fit(event, engage_stats)
... print(majority.predict(event))
...
False
False
True
"""
_parameter_constraints: Dict[str, Any] = {
**BaseClassifier._parameter_constraints,
"engagement": TypeConstraint(int),
"non_engagement": TypeConstraint(int),
"threshold": TypeConstraint(float),
}
def __init__(
self, *, engagement: int = 0, non_engagement: int = 0, threshold: float = 0.5
) -> None:
"""Init MajorityClassifier object.
Args:
*: Use to reject positional arguments.
engagement: The number of learner's engagements.
non_engagement: The number of learner's non_engagements.
threshold: A float that determines the classification threshold.
Raises:
TrueLearnTypeError:
Types of parameters do not satisfy their constraints.
TrueLearnValueError:
Values of parameters do not satisfy their constraints.
"""
super().__init__()
self._engagement = engagement
self._non_engagement = non_engagement
self._threshold = threshold
self._validate_params()
def fit(self, x: EventModel, y: bool) -> Self:
if y:
self._engagement += 1
else:
self._non_engagement += 1
return self
def predict(self, x: EventModel) -> bool:
return self.predict_proba(x) > self._threshold
def predict_proba(self, x: EventModel) -> float:
return self._engagement / max(1, self._engagement + self._non_engagement)