Skip to content

Commit 02b2859

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 3a7869b commit 02b2859

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

machine_learning/xgboost_classifier.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sklearn.model_selection import train_test_split
88
from xgboost import XGBClassifier
99

10+
1011
def data_handling(data: dict) -> tuple:
1112
# Split dataset into features and target
1213
# data is features
@@ -20,6 +21,7 @@ def data_handling(data: dict) -> tuple:
2021
"""
2122
return (data["data"], data["target"])
2223

24+
2325
class XGBClassifier:
2426
"""
2527
An implementation of a gradient boosting classifier inspired by XGBoost.
@@ -38,14 +40,19 @@ class XGBClassifier:
3840
Maximum depth of the regression trees.
3941
random_state : int, default=0
4042
Random seed.
41-
42-
**Important:**
43+
44+
**Important:**
4345
Due to limitations of our custom DecisionTree (which only supports one-dimensional input),
4446
only the first feature (column 0) of the dataset is used when training each tree.
4547
"""
4648

47-
def __init__(self, n_estimators: int = 100, learning_rate: float = 0.3,
48-
max_depth: int = 3, random_state: int = 0):
49+
def __init__(
50+
self,
51+
n_estimators: int = 100,
52+
learning_rate: float = 0.3,
53+
max_depth: int = 3,
54+
random_state: int = 0,
55+
):
4956
self.n_estimators = n_estimators
5057
self.learning_rate = learning_rate
5158
self.max_depth = max_depth
@@ -85,7 +92,9 @@ def fit(self, X: np.ndarray, y: np.ndarray) -> None:
8592
for t in range(self.n_estimators):
8693
# Compute probabilities using softmax.
8794
exp_F = np.exp(F)
88-
p = exp_F / np.sum(exp_F, axis=1, keepdims=True) # shape: (n_samples, num_class)
95+
p = exp_F / np.sum(
96+
exp_F, axis=1, keepdims=True
97+
) # shape: (n_samples, num_class)
8998
trees_per_class = []
9099

91100
for k in range(self.num_class):
@@ -146,6 +155,7 @@ def predict(self, X: np.ndarray) -> np.ndarray:
146155
proba = self.predict_proba(X)
147156
return np.argmax(proba, axis=1)
148157

158+
149159
def xgboost(features: np.ndarray, target: np.ndarray) -> XGBClassifier:
150160
"""
151161
# THIS TEST IS BROKEN!! >>> xgboost(np.array([[5.1, 3.6, 1.4, 0.2]]), np.array([0]))

0 commit comments

Comments
 (0)