Skip to content

Commit e3d68b6

Browse files
lorentzenchrglemaitre
authored andcommitted
MRG Common Private Loss Module with tempita (scikit-learn#20567)
1 parent 398173f commit e3d68b6

11 files changed

+3644
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ _configtest.o.d
7676
.mypy_cache/
7777

7878
# files generated from a template
79+
sklearn/_loss/_loss.pyx
7980
sklearn/utils/_seq_dataset.pyx
8081
sklearn/utils/_seq_dataset.pxd
8182
sklearn/utils/_weight_vector.pyx

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ allow_redefinition = True
6969
[check-manifest]
7070
# ignore files missing in VCS
7171
ignore =
72+
sklearn/_loss/_loss.pyx
7273
sklearn/linear_model/_sag_fast.pyx
7374
sklearn/utils/_seq_dataset.pyx
7475
sklearn/utils/_seq_dataset.pxd

sklearn/_loss/__init__.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
The :mod:`sklearn._loss` module includes loss function classes suitable for
3+
fitting classification and regression tasks.
4+
"""
5+
6+
from .loss import (
7+
HalfSquaredError,
8+
AbsoluteError,
9+
PinballLoss,
10+
HalfPoissonLoss,
11+
HalfGammaLoss,
12+
HalfTweedieLoss,
13+
HalfBinomialLoss,
14+
HalfMultinomialLoss,
15+
)
16+
17+
18+
__all__ = [
19+
"HalfSquaredError",
20+
"AbsoluteError",
21+
"PinballLoss",
22+
"HalfPoissonLoss",
23+
"HalfGammaLoss",
24+
"HalfTweedieLoss",
25+
"HalfBinomialLoss",
26+
"HalfMultinomialLoss",
27+
]

sklearn/_loss/_loss.pxd

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# cython: language_level=3
2+
3+
import numpy as np
4+
cimport numpy as np
5+
6+
np.import_array()
7+
8+
9+
# Fused types for y_true, y_pred, raw_prediction
10+
ctypedef fused Y_DTYPE_C:
11+
np.npy_float64
12+
np.npy_float32
13+
14+
15+
# Fused types for gradient and hessian
16+
ctypedef fused G_DTYPE_C:
17+
np.npy_float64
18+
np.npy_float32
19+
20+
21+
# Struct to return 2 doubles
22+
ctypedef struct double_pair:
23+
double val1
24+
double val2
25+
26+
27+
# C base class for loss functions
28+
cdef class CyLossFunction:
29+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
30+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
31+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
32+
33+
34+
cdef class CyHalfSquaredError(CyLossFunction):
35+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
36+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
37+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
38+
39+
40+
cdef class CyAbsoluteError(CyLossFunction):
41+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
42+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
43+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
44+
45+
46+
cdef class CyPinballLoss(CyLossFunction):
47+
cdef readonly double quantile # readonly makes it accessible from Python
48+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
49+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
50+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
51+
52+
53+
cdef class CyHalfPoissonLoss(CyLossFunction):
54+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
55+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
56+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
57+
58+
59+
cdef class CyHalfGammaLoss(CyLossFunction):
60+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
61+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
62+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
63+
64+
65+
cdef class CyHalfTweedieLoss(CyLossFunction):
66+
cdef readonly double power # readonly makes it accessible from Python
67+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
68+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
69+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
70+
71+
72+
cdef class CyHalfBinomialLoss(CyLossFunction):
73+
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
74+
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
75+
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil

0 commit comments

Comments
 (0)