Skip to content

Metrics

deepgboost.metric.regression.BaseMetric

Abstract base for all DeepGBoost evaluation metrics.

Source code in src/deepgboost/metric/regression.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class BaseMetric:
    """Abstract base for all DeepGBoost evaluation metrics."""

    name: str = ""
    higher_is_better: bool = False

    def __call__(
        self,
        y_true: np.ndarray,
        y_pred: np.ndarray,
    ) -> float:
        raise NotImplementedError

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}()"

deepgboost.metric.regression.RMSEMetric

Bases: BaseMetric

Root Mean Squared Error.

Source code in src/deepgboost/metric/regression.py
25
26
27
28
29
30
31
32
33
34
35
36
class RMSEMetric(BaseMetric):
    """Root Mean Squared Error."""

    name = "rmse"
    higher_is_better = False

    def __call__(
        self,
        y_true: np.ndarray,
        y_pred: np.ndarray,
    ) -> float:
        return float(np.sqrt(np.mean((y_true - y_pred) ** 2)))

deepgboost.metric.regression.MAEMetric

Bases: BaseMetric

Mean Absolute Error.

Source code in src/deepgboost/metric/regression.py
39
40
41
42
43
44
45
46
47
48
49
50
class MAEMetric(BaseMetric):
    """Mean Absolute Error."""

    name = "mae"
    higher_is_better = False

    def __call__(
        self,
        y_true: np.ndarray,
        y_pred: np.ndarray,
    ) -> float:
        return float(np.mean(np.abs(y_true - y_pred)))

deepgboost.metric.regression.R2ScoreMetric

Bases: BaseMetric

Coefficient of determination R².

Source code in src/deepgboost/metric/regression.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class R2ScoreMetric(BaseMetric):
    """Coefficient of determination R²."""

    name = "r2"
    higher_is_better = True

    def __call__(
        self,
        y_true: np.ndarray,
        y_pred: np.ndarray,
    ) -> float:
        ss_res = np.sum((y_true - y_pred) ** 2)
        ss_tot = np.sum((y_true - y_true.mean()) ** 2)
        if ss_tot == 0.0:
            return 1.0 if ss_res == 0.0 else 0.0
        return float(1.0 - ss_res / ss_tot)

deepgboost.metric.classification.AccuracyMetric

Bases: BaseMetric

Classification accuracy.

Source code in src/deepgboost/metric/classification.py
10
11
12
13
14
15
16
17
class AccuracyMetric(BaseMetric):
    """Classification accuracy."""

    name = "accuracy"
    higher_is_better = True

    def __call__(self, y_true: np.ndarray, y_pred: np.ndarray) -> float:
        return float(np.mean(y_true == y_pred))

deepgboost.metric.classification.LogLossMetric

Bases: BaseMetric

Binary cross-entropy (log-loss).

y_pred should be probabilities in [0, 1].

Source code in src/deepgboost/metric/classification.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class LogLossMetric(BaseMetric):
    """
    Binary cross-entropy (log-loss).

    y_pred should be probabilities in [0, 1].
    """

    name = "logloss"
    higher_is_better = False

    def __call__(
        self,
        y_true: np.ndarray,
        y_pred: np.ndarray,
    ) -> float:
        p = np.clip(y_pred, 1e-7, 1 - 1e-7)
        return float(
            -np.mean(y_true * np.log(p) + (1.0 - y_true) * np.log(1.0 - p)),
        )

deepgboost.metric.classification.AUCMetric

Bases: BaseMetric

Area Under the ROC Curve (binary classification).

Uses the trapezoidal rule. y_pred should be probabilities.

Source code in src/deepgboost/metric/classification.py
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
class AUCMetric(BaseMetric):
    """
    Area Under the ROC Curve (binary classification).

    Uses the trapezoidal rule.  y_pred should be probabilities.
    """

    name = "auc"
    higher_is_better = True

    def __call__(
        self,
        y_true: np.ndarray,
        y_pred: np.ndarray,
    ) -> float:
        order = np.argsort(y_pred)[::-1]
        y_sorted = y_true[order]
        n_pos = y_sorted.sum()
        n_neg = len(y_sorted) - n_pos
        if n_pos == 0 or n_neg == 0:
            return 0.5
        tp = np.cumsum(y_sorted)
        fp = np.cumsum(1 - y_sorted)
        tpr = tp / n_pos
        fpr = fp / n_neg
        tpr = np.concatenate([[0.0], tpr])
        fpr = np.concatenate([[0.0], fpr])
        trapz_fn = getattr(np, "trapezoid", None) or getattr(np, "trapz", None)
        return float(trapz_fn(tpr, fpr))