8000 MNT Use cimport numpy as cnp for sklearn/tree (#23315) · scikit-learn/scikit-learn@3850935 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3850935

Browse files
RMeliglemaitrethomasjpfan
authored
MNT Use cimport numpy as cnp for sklearn/tree (#23315)
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
1 parent 47e3358 commit 3850935

File tree

8 files changed

+95
-105
lines changed

8 files changed

+95
-105
lines changed

sklearn/tree/_criterion.pxd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
# See _criterion.pyx for implementation details.
1111

12-
import numpy as np
13-
cimport numpy as np
14-
1512
from ._tree cimport DTYPE_t # Type of X
1613
from ._tree cimport DOUBLE_t # Type of y, sample_weight
1714
from ._tree cimport SIZE_t # Type for indices and counters

sklearn/tree/_criterion.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ from libc.string cimport memset
1717
from libc.math cimport fabs
1818

1919
import numpy as np
20-
cimport numpy as np
21-
np.import_array()
20+
cimport numpy as cnp
21+
cnp.import_array()
2222

2323
from numpy.math cimport INFINITY
2424
from scipy.special.cython_special cimport xlogy
@@ -197,7 +197,7 @@ cdef class ClassificationCriterion(Criterion):
197197
"""Abstract criterion for classification."""
198198

199199
def __cinit__(self, SIZE_t n_outputs,
200-
np.ndarray[SIZE_t, ndim=1] n_classes):
200+
cnp.ndarray[SIZE_t, ndim=1] n_classes):
201201
"""Initialize attributes for this criterion.
202202
203203
Parameters
@@ -874,8 +874,8 @@ cdef class MAE(RegressionCriterion):
874874
MAE = (1 / n)*(\sum_i |y_i - f_i|), where y_i is the true
875875
value and f_i is the predicted value."""
876876

877-
cdef np.ndarray left_child
878-
cdef np.ndarray right_child
877+
cdef cnp.ndarray left_child
878+
cdef cnp.ndarray right_child
879879
cdef DOUBLE_t[::1] node_medians
880880

881881
def __cinit__(self, SIZE_t n_outputs, SIZE_t n_samples):

sklearn/tree/_splitter.pxd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
# See _splitter.pyx for details.
1111

12-
import numpy as np
13-
cimport numpy as np
14-
1512
from ._criterion cimport Criterion
1613

1714
from ._tree cimport DTYPE_t # Type of X

sklearn/tree/_splitter.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ from libc.string cimport memcpy
1919
from libc.string cimport memset
2020

2121
import numpy as np
22-
cimport numpy as np
23-
np.import_array()
2422

2523
from scipy.sparse import csc_matrix
2624

sklearn/tree/_tree.pxd

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
# See _tree.pyx for details.
1212

1313
import numpy as np
14-
cimport numpy as np
14+
cimport numpy as cnp
1515

16-
ctypedef np.npy_float32 DTYPE_t # Type of X
17-
ctypedef np.npy_float64 DOUBLE_t # Type of y, sample_weight
18-
ctypedef np.npy_intp SIZE_t # Type for indices and counters
19-
ctypedef np.npy_int32 INT32_t # Signed 32 bit integer
20-
ctypedef np.npy_uint32 UINT32_t # Unsigned 32 bit integer
16+
ctypedef cnp.npy_float32 DTYPE_t # Type of X
17+
ctypedef cnp.npy_float64 DOUBLE_t # Type of y, sample_weight
18+
ctypedef cnp.npy_intp SIZE_t # Type for indices and counters
19+
ctypedef cnp.npy_int32 INT32_t # Signed 32 bit integer
20+
ctypedef cnp.npy_uint32 UINT32_t # Unsigned 32 bit integer
2121

2222
from ._splitter cimport Splitter
2323
from ._splitter cimport SplitRecord
@@ -62,14 +62,14 @@ cdef class Tree:
6262
cdef int _resize(self, SIZE_t capacity) nogil except -1
6363
cdef int _resize_c(self, SIZE_t capacity=*) nogil except -1
6464

65-
cdef np.ndarray _get_value_ndarray(self)
66-
cdef np.ndarray _get_node_ndarray(self)
65+
cdef cnp.ndarray _get_value_ndarray(self)
66+
cdef cnp.ndarray _get_node_ndarray(self)
6767

68-
cpdef np.ndarray predict(self, object X)
68+
cpdef cnp.ndarray predict(self, object X)
6969

70-
cpdef np.ndarray apply(self, object X)
71-
cdef np.ndarray _apply_dense(self, object X)
72-
cdef np.ndarray _apply_sparse_csr(self, object X)
70+
cpdef cnp.ndarray apply(self, object X)
71+
cdef cnp.ndarray _apply_dense(self, object X)
72+
cdef cnp.ndarray _apply_sparse_csr(self, object X)
7373

7474
cpdef object decision_path(self, object X)
7575
cdef object _decision_path_dense(self, object X)
@@ -98,6 +98,6 @@ cdef class TreeBuilder:
9898
cdef SIZE_t max_depth # Maximal tree depth
9999
cdef double min_impurity_decrease # Impurity threshold for early stopping
100100

101-
cpdef build(self, Tree tree, object X, np.ndarray y,
102-
np.ndarray sample_weight=*)
103-
cdef _check_input(self, object X, np.ndarray y, np.ndarray sample_weight)
101+
cpdef build(self, Tree tree, object X, cnp.ndarray y,
102+
cnp.ndarray sample_weight=*)
103+
cdef _check_input(self, object X, cnp.ndarray y, cnp.ndarray sample_weight)

0 commit comments

Comments
 (0)
0