Skip to content

Commit e318b88

Browse files
committed
Fixing Adaboost algorithm
1 parent bce8654 commit e318b88

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

machine_learning/adaboost.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
array([0, 1])
1313
"""
1414

15-
from typing import Any
16-
1715
import numpy as np
16+
from typing import Any
1817

1918

2019
class AdaBoost:
@@ -33,7 +32,7 @@ def fit(self, feature_matrix: np.ndarray, target: np.ndarray) -> None:
3332
feature_matrix: (n_samples, n_features) feature matrix
3433
target: (n_samples,) labels (0 or 1)
3534
"""
36-
n_samples, n_features = feature_matrix.shape
35+
n_samples, _n_features = feature_matrix.shape
3736
sample_weights = np.ones(n_samples) / n_samples # Initialize sample weights
3837
self.models = []
3938
self.alphas = []
@@ -74,22 +73,17 @@ def predict(self, feature_matrix: np.ndarray) -> np.ndarray:
7473
return np.where(clf_preds >= 0, 1, 0)
7574

7675
def _build_stump(
77-
self,
78-
feature_matrix: np.ndarray,
79-
target_signed: np.ndarray,
80-
sample_weights: np.ndarray,
76+
self, feature_matrix: np.ndarray, target_signed: np.ndarray, sample_weights: np.ndarray
8177
) -> dict[str, Any]:
8278
"""Find the best decision stump for current weights."""
83-
n_samples, n_features = feature_matrix.shape
79+
_n_samples, n_features = feature_matrix.shape
8480
min_error = float("inf")
8581
best_stump: dict[str, Any] = {}
8682
for feature in range(n_features):
8783
thresholds = np.unique(feature_matrix[:, feature])
8884
for threshold in thresholds:
8985
for polarity in [1, -1]:
90-
pred = self._stump_predict(
91-
feature_matrix, feature, threshold, polarity
92-
)
86+
pred = self._stump_predict(feature_matrix, feature, threshold, polarity)
9387
error = np.sum(sample_weights * (pred != target_signed))
9488
if error < min_error:
9589
min_error = error

0 commit comments

Comments
 (0)