AdaBoost¶
toyml.ensemble.adaboost.AdaBoost
dataclass
¶
AdaBoost(weak_learner: type[BaseWeakLeaner], n_weak_learner: int = 5, predict_labels_: list[int] | None = None, training_error_rate_: float | None = None, _n: int = -1, _labels: list[int] = list(), _weights: list[float] = list(), _base_clf_labels: list[list[int]] = list(), _weak_learner_predicts: list[Callable[..., Any]] = list(), _alphas: list[float] = list())
The implementation of AdaBoost algorithm.
Examples:
>>> from toyml.ensemble.adaboost import AdaBoost, OneDimensionClassifier
>>> dataset = [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
>>> labels = [1, 1, 1, -1, -1, -1, 1, 1, 1, -1]
>>> ada = AdaBoost(weak_learner=OneDimensionClassifier, n_weak_learner=3).fit(dataset, labels)
>>> print(f"Training dataset error rate: {ada.training_error_rate_}")
Training dataset error rate: 0.0
>>> test_sample = [1.5]
>>> print(f"The label of {test_sample} is {ada.predict(test_sample)}")
The label of [1.5] is 1
References
- Li Hang
- Zhou Zhihua
weak_learner
instance-attribute
¶
weak_learner: type[BaseWeakLeaner]
The weak learner to be used in the AdaBoost algorithm.
n_weak_learner
class-attribute
instance-attribute
¶
n_weak_learner: int = 5
The number of weak learners to be used in the AdaBoost algorithm.
predict_labels_
class-attribute
instance-attribute
¶
The prediction labels of the training dataset.
training_error_rate_
class-attribute
instance-attribute
¶
training_error_rate_: float | None = None
The error rate of the training dataset.
_labels
class-attribute
instance-attribute
¶
The labels of the training dataset.
_weights
class-attribute
instance-attribute
¶
The weights of samples in the training dataset.
_base_clf_labels
class-attribute
instance-attribute
¶
The prediction labels of the base classifiers.
_weak_learner_predicts
class-attribute
instance-attribute
¶
The prediction functions of the weak learners.
_alphas
class-attribute
instance-attribute
¶
The alpha values of the weak learners.
fit
¶
Fit the AdaBoost model.
Source code in toyml/ensemble/adaboost.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
predict
¶
Predict the label of the input sample.
Source code in toyml/ensemble/adaboost.py
134 135 136 137 138 139 140 141 142 |
|
toyml.ensemble.adaboost.OneDimensionClassifier
dataclass
¶
OneDimensionClassifier(_sign_mode: SignMode = POS_NEG, _best_cut: float = inf, error_rate_: float = inf, predict_labels_: list[int] | None = None)
Bases: BaseWeakLeaner
Binary classifier with one dimension feature.
Ref: Li Hang, 1 ed, E8.1.3
fit
¶
Fit the one-dimension classifier.
Source code in toyml/ensemble/adaboost.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
predict
¶
Predict the label of the input sample.
Source code in toyml/ensemble/adaboost.py
179 180 181 182 183 184 185 186 187 188 189 190 |
|
get_error_rate
¶
get_error_rate() -> float
Get the error rate of the training dataset.
Source code in toyml/ensemble/adaboost.py
192 193 194 195 196 197 |
|
get_predict_labels
¶
Get the prediction labels of the training dataset.
Source code in toyml/ensemble/adaboost.py
199 200 201 202 203 204 |
|
_get_candidate_cuts
staticmethod
¶
Get the candidate cuts of the training dataset.
Source code in toyml/ensemble/adaboost.py
206 207 208 209 210 211 |
|
get_best_cut
¶
get_best_cut(dataset: list[list[float]], weights: list[float], labels: list[int]) -> tuple[SignMode, float, float]
Get the best cut of the training dataset.
Source code in toyml/ensemble/adaboost.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
_get_cut_error_rate
¶
_get_cut_error_rate(cut: float, points: list[float], weights: list[float], labels: list[int], sign_mode: SignMode) -> float
Get the error rate of the training dataset.
Source code in toyml/ensemble/adaboost.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|