Skip to content

MNT Use cimport numpy as cnp for sklearn/tree #23315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions sklearn/tree/_criterion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

# See _criterion.pyx for implementation details.

import numpy as np
cimport numpy as np

from ._tree cimport DTYPE_t # Type of X
from ._tree cimport DOUBLE_t # Type of y, sample_weight
from ._tree cimport SIZE_t # Type for indices and counters
Expand Down
10 changes: 5 additions & 5 deletions sklearn/tree/_criterion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ from libc.string cimport memset
from libc.math cimport fabs

import numpy as np
cimport numpy as np
np.import_array()
cimport numpy as cnp
cnp.import_array()

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

def __cinit__(self, SIZE_t n_outputs,
np.ndarray[SIZE_t, ndim=1] n_classes):
cnp.ndarray[SIZE_t, ndim=1] n_classes):
"""Initialize attributes for this criterion.

Parameters
Expand Down Expand Up @@ -874,8 +874,8 @@ cdef class MAE(RegressionCriterion):
MAE = (1 / n)*(\sum_i |y_i - f_i|), where y_i is the true
value and f_i is the predicted value."""

cdef np.ndarray left_child
cdef np.ndarray right_child
cdef cnp.ndarray left_child
cdef cnp.ndarray right_child
cdef DOUBLE_t[::1] node_medians

def __cinit__(self, SIZE_t n_outputs, SIZE_t n_samples):
Expand Down
3 changes: 0 additions & 3 deletions sklearn/tree/_splitter.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

# See _splitter.pyx for details.

import numpy as np
cimport numpy as np

from ._criterion cimport Criterion

from ._tree cimport DTYPE_t # Type of X
Expand Down
2 changes: 0 additions & 2 deletions sklearn/tree/_splitter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ from libc.string cimport memcpy
from libc.string cimport memset

import numpy as np
cimport numpy as np
np.import_array()

from scipy.sparse import csc_matrix

Expand Down
30 changes: 15 additions & 15 deletions sklearn/tree/_tree.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
# See _tree.pyx for details.

import numpy as np
cimport numpy as np
cimport numpy as cnp

ctypedef np.npy_float32 DTYPE_t # Type of X
ctypedef np.npy_float64 DOUBLE_t # Type of y, sample_weight
ctypedef np.npy_intp SIZE_t # Type for indices and counters
ctypedef np.npy_int32 INT32_t # Signed 32 bit integer
ctypedef np.npy_uint32 UINT32_t # Unsigned 32 bit integer
ctypedef cnp.npy_float32 DTYPE_t # Type of X
ctypedef cnp.npy_float64 DOUBLE_t # Type of y, sample_weight
ctypedef cnp.npy_intp SIZE_t # Type for indices and counters
ctypedef cnp.npy_int32 INT32_t # Signed 32 bit integer
ctypedef cnp.npy_uint32 UINT32_t # Unsigned 32 bit integer

from ._splitter cimport Splitter
from ._splitter cimport SplitRecord
Expand Down Expand Up @@ -62,14 +62,14 @@ cdef class Tree:
cdef int _resize(self, SIZE_t capacity) nogil except -1
cdef int _resize_c(self, SIZE_t capacity=*) nogil except -1

cdef np.ndarray _get_value_ndarray(self)
cdef np.ndarray _get_node_ndarray(self)
cdef cnp.ndarray _get_value_ndarray(self)
cdef cnp.ndarray _get_node_ndarray(self)

cpdef np.ndarray predict(self, object X)
cpdef cnp.ndarray predict(self, object X)

cpdef np.ndarray apply(self, object X)
cdef np.ndarray _apply_dense(self, object X)
cdef np.ndarray _apply_sparse_csr(self, object X)
cpdef cnp.ndarray apply(self, object X)
cdef cnp.ndarray _apply_dense(self, object X)
cdef cnp.ndarray _apply_sparse_csr(self, object X)

cpdef object decision_path(self, object X)
cdef object _decision_path_dense(self, object X)
Expand Down Expand Up @@ -98,6 +98,6 @@ cdef class TreeBuilder:
cdef SIZE_t max_depth # Maximal tree depth
cdef double min_impurity_decrease # Impurity threshold for early stopping

cpdef build(self, Tree tree, object X, np.ndarray y,
np.ndarray sample_weight=*)
cdef _check_input(self, object X, np.ndarray y, np.ndarray sample_weight)
cpdef build(self, Tree tree, object X, cnp.ndarray y,
cnp.ndarray sample_weight=*)
cdef _check_input(self, object X, cnp.ndarray y, cnp.ndarray sample_weight)
Loading