Skip to content

Commit 7501a86

Browse files
authored
MNT cleanup Cython imports in tree module (scikit-learn#33947)
1 parent 8c62b6b commit 7501a86

7 files changed

Lines changed: 12 additions & 18 deletions

File tree

sklearn/tree/_criterion.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Authors: The scikit-learn developers
22
# SPDX-License-Identifier: BSD-3-Clause
33

4+
from libc.math cimport INFINITY
45
from libc.string cimport memcpy
56
from libc.string cimport memset
6-
from libc.math cimport INFINITY
77

88
import numpy as np
99
cimport numpy as cnp

sklearn/tree/_partitioner.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from cython cimport floating
77

88
from sklearn.utils._typedefs cimport (
9-
float32_t, float64_t, int8_t, int32_t, intp_t, uint8_t, uint32_t
9+
float32_t, float64_t, int32_t, intp_t, uint8_t
1010
)
1111
from sklearn.tree._splitter cimport SplitRecord
1212

sklearn/tree/_partitioner.pyx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and sparse data stored in a Compressed Sparse Column (CSC) format.
1111
# SPDX-License-Identifier: BSD-3-Clause
1212

1313
from cython cimport final
14-
from libc.math cimport isnan, log2
14+
from libc.math cimport INFINITY, isnan, log2
1515
from libc.stdlib cimport qsort
1616
from libc.string cimport memcpy, memmove
1717

@@ -26,9 +26,6 @@ from sklearn.tree._splitter cimport SplitRecord
2626
# in SparsePartitioner
2727
cdef float32_t EXTRACT_NNZ_SWITCH = 0.1
2828

29-
# Allow for 32 bit float comparisons
30-
cdef float32_t INFINITY_32t = np.inf
31-
3229

3330
@final
3431
cdef class DensePartitioner:
@@ -138,8 +135,8 @@ cdef class DensePartitioner:
138135
intp_t p
139136
float32_t current_feature_value
140137
intp_t[::1] samples = self.samples
141-
float32_t min_feature_value = INFINITY_32t
142-
float32_t max_feature_value = -INFINITY_32t
138+
float32_t min_feature_value = INFINITY
139+
float32_t max_feature_value = -INFINITY
143140
float32_t[::1] feature_values = self.feature_values
144141
intp_t n_missing = 0
145142
bint seen_non_missing = False

sklearn/tree/_splitter.pyx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ of splitting strategies:
2020
# Authors: The scikit-learn developers
2121
# SPDX-License-Identifier: BSD-3-Clause
2222

23+
from libc.math cimport INFINITY
2324
from libc.string cimport memcpy
2425

25-
from sklearn.utils._typedefs cimport int8_t
2626
from sklearn.tree._criterion cimport Criterion
2727
from sklearn.tree._partitioner cimport (
2828
FEATURE_THRESHOLD, DensePartitioner, SparsePartitioner,
@@ -41,9 +41,6 @@ ctypedef fused Partitioner:
4141
SparsePartitioner
4242

4343

44-
cdef float64_t INFINITY = np.inf
45-
46-
4744
cdef inline void _init_split(SplitRecord* self, intp_t start_pos) noexcept nogil:
4845
self.impurity_left = INFINITY
4946
self.impurity_right = INFINITY

sklearn/tree/_tree.pxd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import numpy as np
77
cimport numpy as cnp
88

9-
from sklearn.utils._typedefs cimport float32_t, float64_t, intp_t, int32_t, uint8_t, uint32_t
9+
from sklearn.utils._typedefs cimport (
10+
float32_t, float64_t, intp_t, int32_t, uint8_t, uint32_t
11+
)
12+
from sklearn.tree._splitter cimport Splitter, SplitRecord
1013

11-
from sklearn.tree._splitter cimport Splitter
12-
from sklearn.tree._splitter cimport SplitRecord
1314

1415
cdef struct Node:
1516
# Base storage structure for the nodes in a Tree object

sklearn/tree/_tree.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from cpython cimport Py_INCREF, PyObject, PyTypeObject
55

6+
from libc.math cimport INFINITY, isnan
67
from libc.stdlib cimport free
78
from libc.string cimport memcpy
89
from libc.string cimport memset
910
from libc.stdint cimport INTPTR_MAX
10-
from libc.math cimport isnan
1111
from libcpp.vector cimport vector
1212
from libcpp.algorithm cimport pop_heap
1313
from libcpp.algorithm cimport push_heap
@@ -42,7 +42,6 @@ cdef extern from "numpy/arrayobject.h":
4242
from numpy import float32 as DTYPE
4343
from numpy import float64 as DOUBLE
4444

45-
cdef float64_t INFINITY = np.inf
4645
cdef float64_t EPSILON = np.finfo('double').eps
4746

4847
# Some handy constants (BestFirstTreeBuilder)

sklearn/tree/_utils.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
cimport numpy as cnp
77
from sklearn.tree._tree cimport Node
88
from sklearn.neighbors._quad_tree cimport Cell
9-
from sklearn.utils._typedefs cimport float32_t, float64_t, intp_t, uint8_t, int32_t, uint32_t
9+
from sklearn.utils._typedefs cimport float32_t, float64_t, intp_t, uint8_t, uint32_t
1010

1111

1212
cdef enum:

0 commit comments

Comments
 (0)