Skip to content

Commit 166b3ad

Browse files
grlee77adrinjalali
authored andcommitted
MNT consistently call import_array() after cimport of numpy (#17054)
1 parent ffdfae5 commit 166b3ad

File tree

16 files changed

+35
-2
lines changed

16 files changed

+35
-2
lines changed

sklearn/_isotonic.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ cimport numpy as np
1111
cimport cython
1212
from cython cimport floating
1313

14+
np.import_array()
15+
1416

1517
def _inplace_contiguous_isotonic_regression(floating[::1] y, floating[::1] w):
1618
cdef:

sklearn/cluster/_dbscan_inner.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ from libcpp.vector cimport vector
99
cimport numpy as np
1010
import numpy as np
1111

12+
np.import_array()
13+
1214

1315
# Work around Cython bug: C++ exceptions are not caught unless thrown within
1416
# a cdef function with an "except +" declaration.

sklearn/ensemble/_hist_gradient_boosting/_binning.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ from libc.math cimport isnan
1616

1717
from .common cimport X_DTYPE_C, X_BINNED_DTYPE_C
1818

19+
np.import_array()
20+
21+
1922
def _map_to_bins(const X_DTYPE_C [:, :] data,
2023
list binning_thresholds,
2124
const unsigned char missing_values_bin_idx,

sklearn/ensemble/_hist_gradient_boosting/_gradient_boosting.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ cimport numpy as np
1313
from .common import Y_DTYPE
1414
from .common cimport Y_DTYPE_C
1515

16+
np.import_array()
17+
1618

1719
def _update_raw_predictions(
1820
Y_DTYPE_C [::1] raw_predictions, # OUT

sklearn/ensemble/_hist_gradient_boosting/_loss.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ from libc.math cimport exp, log
1515
from .common cimport Y_DTYPE_C
1616
from .common cimport G_H_DTYPE_C
1717

18+
np.import_array()
19+
1820

1921
def _update_gradients_least_squares(
2022
G_H_DTYPE_C [::1] gradients, # OUT

sklearn/ensemble/_hist_gradient_boosting/_predictor.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ from .common import Y_DTYPE
1818
from .common cimport X_BINNED_DTYPE_C
1919
from .common cimport node_struct
2020

21+
np.import_array()
22+
2123

2224
def _predict_from_numeric_data(
2325
node_struct [:] nodes,

sklearn/ensemble/_hist_gradient_boosting/common.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import numpy as np
33
cimport numpy as np
44

5+
np.import_array()
6+
57

68
ctypedef np.npy_float64 X_DTYPE_C
79
ctypedef np.npy_uint8 X_BINNED_DTYPE_C

sklearn/ensemble/_hist_gradient_boosting/histogram.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ from .common cimport hist_struct
1717
from .common cimport X_BINNED_DTYPE_C
1818
from .common cimport G_H_DTYPE_C
1919

20+
np.import_array()
21+
2022
# Notes:
2123
# - IN views are read-only, OUT views are write-only
2224
# - In a lot of functions here, we pass feature_idx and the whole 2d

sklearn/ensemble/_hist_gradient_boosting/splitting.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ from .common cimport hist_struct
2727
from .common import HISTOGRAM_DTYPE
2828
from .common cimport MonotonicConstraint
2929

30+
np.import_array()
31+
3032

3133
cdef struct split_info_struct:
3234
# Same as the SplitInfo class, but we need a C struct to use it in the
@@ -809,7 +811,7 @@ cpdef inline Y_DTYPE_C compute_node_value(
809811
"""
810812

811813
cdef:
812-
Y_DTYPE_C value
814+
Y_DTYPE_C value
813815

814816
value = -sum_gradient / (sum_hessian + l2_regularization + 1e-15)
815817

sklearn/linear_model/_sag_fast.pyx.tp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ from ..utils._seq_dataset cimport SequentialDataset32, SequentialDataset64
5858

5959
from libc.stdio cimport printf
6060

61+
np.import_array()
6162

6263

6364
{{for name, c_type, np_type in get_dispatch(dtypes)}}

sklearn/manifold/_barnes_hut_tsne.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ from cython.parallel cimport prange, parallel
1818

1919
from ..neighbors._quad_tree cimport _QuadTree
2020

21+
np.import_array()
22+
2123

2224
cdef char* EMPTY_STRING = ""
2325

sklearn/manifold/_utils.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ cimport cython
55
import numpy as np
66
cimport numpy as np
77
from libc.stdio cimport printf
8+
9+
np.import_array()
10+
11+
812
cdef extern from "numpy/npy_math.h":
913
float NPY_INFINITY
1014

sklearn/neighbors/_typedefs.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import numpy as np
44
cimport numpy as np
55
from libc.math cimport sqrt
66

7+
np.import_array()
8+
9+
710
# use a hack to determine the associated numpy data types
811
# NOTE: the following requires the buffer interface, only available in
912
# numpy 1.5+. We'll choose the DTYPE by hand instead.

sklearn/preprocessing/_csr_polynomial_expansion.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from scipy.sparse import csr_matrix
88
from numpy cimport ndarray
99
cimport numpy as np
1010

11+
np.import_array()
1112
ctypedef np.int32_t INDEX_T
1213

1314
ctypedef fused DATA_T:

sklearn/svm/_libsvm_sparse.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ cimport numpy as np
44
from scipy import sparse
55
from ..exceptions import ConvergenceWarning
66

7+
np.import_array()
8+
79

810
cdef extern from *:
911
ctypedef char* const_char_p "const char*"
@@ -186,7 +188,7 @@ def libsvm_sparse_train ( int n_features,
186188

187189
# copy model.nSV
188190
# TODO: do only in classification
189-
cdef np.ndarray n_class_SV
191+
cdef np.ndarray n_class_SV
190192
n_class_SV = np.empty(n_class, dtype=np.int32)
191193
copy_nSV(n_class_SV.data, model)
192194

sklearn/utils/_logistic_sigmoid.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from libc.math cimport log, exp
77
import numpy as np
88
cimport numpy as np
99

10+
np.import_array()
1011
ctypedef np.float64_t DTYPE_t
1112

1213

0 commit comments

Comments
 (0)