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

Skip to content

Commit 47e3358

Browse files
authored
MNT Use cimport numpy as cnp for sklearn/neighbors (#23350)
1 parent b6c853b commit 47e3358

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

sklearn/neighbors/_binary_tree.pxi

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
# BinaryTree tree2, ITYPE_t i_node2):
143143
# """Compute the maximum distance between two nodes"""
144144

145-
cimport numpy as np
145+
cimport numpy as cnp
146146
from libc.math cimport fabs, sqrt, exp, cos, pow, log, lgamma
147147
from libc.math cimport fmin, fmax
148148
from libc.stdlib cimport calloc, malloc, free
@@ -167,9 +167,9 @@ from ..utils._heap cimport heap_push
167167
from ..utils._sorting cimport simultaneous_sort as _simultaneous_sort
168168

169169
cdef extern from "numpy/arrayobject.h":
170-
void PyArray_ENABLEFLAGS(np.ndarray arr, int flags)
170+
void PyArray_ENABLEFLAGS(cnp.ndarray arr, int flags)
171171

172-
np.import_array()
172+
cnp.import_array()
173173

174174
# some handy constants
175175
cdef DTYPE_t INF = np.inf
@@ -509,8 +509,8 @@ cdef class NeighborsHeap:
509509
n_nbrs : int
510510
the size of each heap.
511511
"""
512-
cdef np.ndarray distances_arr
513-
cdef np.ndarray indices_arr
512+
cdef cnp.ndarray distances_arr
513+
cdef cnp.ndarray indices_arr
514514

515515
cdef DTYPE_t[:, ::1] distances
516516
cdef ITYPE_t[:, ::1] indices
@@ -641,7 +641,7 @@ cdef class NodeHeap:
641641
642642
heap[i].val < min(heap[2 * i + 1].val, heap[2 * i + 2].val)
643643
"""
644-
cdef np.ndarray data_arr
644+
cdef cnp.ndarray data_arr
645645
cdef NodeHeapData_t[::1] data
646646
cdef ITYPE_t n
647647

@@ -662,7 +662,7 @@ cdef class NodeHeap:
662662
cdef NodeHeapData_t *new_data_ptr
663663
cdef ITYPE_t i
664664
cdef ITYPE_t size = self.data.shape[0]
665-
cdef np.ndarray new_data_arr = np.zeros(new_size,
665+
cdef cnp.ndarray new_data_arr = np.zeros(new_size,
666666
dtype=NodeHeapData)
667667
cdef NodeHeapData_t[::1] new_data = new_data_arr
668668

@@ -767,11 +767,11 @@ VALID_METRIC_IDS = get_valid_metric_ids(VALID_METRICS)
767767
# Binary Tree class
768768
cdef class BinaryTree:
769769

770-
cdef np.ndarray data_arr
771-
cdef np.ndarray sample_weight_arr
772-
cdef np.ndarray idx_array_arr
773-
cdef np.ndarray node_data_arr
774-
cdef np.ndarray node_bounds_arr
770+
cdef cnp.ndarray data_arr
771+
cdef cnp.ndarray sample_weight_arr
772+
cdef cnp.ndarray idx_array_arr
773+
cdef cnp.ndarray node_data_arr
774+
cdef cnp.ndarray node_bounds_arr
775775

776776
cdef readonly const DTYPE_t[:, ::1] data
777777
cdef readonly const DTYPE_t[::1] sample_weight
@@ -1336,16 +1336,16 @@ cdef class BinaryTree:
13361336
distances_npy = np.zeros(Xarr.shape[0], dtype='object')
13371337
for i in range(Xarr.shape[0]):
13381338
# make a new numpy array that wraps the existing data
1339-
indices_npy[i] = np.PyArray_SimpleNewFromData(1, &counts[i], np.NPY_INTP, indices[i])
1339+
indices_npy[i] = cnp.PyArray_SimpleNewFromData(1, &counts[i], cnp.NPY_INTP, indices[i])
13401340
# make sure the data will be freed when the numpy array is garbage collected
1341-
PyArray_ENABLEFLAGS(indices_npy[i], np.NPY_OWNDATA)
1341+
PyArray_ENABLEFLAGS(indices_npy[i], cnp.NPY_OWNDATA)
13421342
# make sure the data is not freed twice
13431343
indices[i] = NULL
13441344

13451345
# make a new numpy array that wraps the existing data
1346-
distances_npy[i] = np.PyArray_SimpleNewFromData(1, &counts[i], np.NPY_DOUBLE, distances[i])
1346+
distances_npy[i] = cnp.PyArray_SimpleNewFromData(1, &counts[i], cnp.NPY_DOUBLE, distances[i])
13471347
# make sure the data will be freed when the numpy array is garbage collected
1348-
PyArray_ENABLEFLAGS(distances_npy[i], np.NPY_OWNDATA)
1348+
PyArray_ENABLEFLAGS(distances_npy[i], cnp.NPY_OWNDATA)
13491349
# make sure the data is not freed twice
13501350
distances[i] = NULL
13511351

@@ -1356,9 +1356,9 @@ cdef class BinaryTree:
13561356
indices_npy = np.zeros(Xarr.shape[0], dtype='object')
13571357
for i in range(Xarr.shape[0]):
13581358
# make a new numpy array that wraps the existing data
1359-
indices_npy[i] = np.PyArray_SimpleNewFromData(1, &counts[i], np.NPY_INTP, indices[i])
1359+
indices_npy[i] = cnp.PyArray_SimpleNewFromData(1, &counts[i], cnp.NPY_INTP, indices[i])
13601360
# make sure the data will be freed when the numpy array is garbage collected
1361-
PyArray_ENABLEFLAGS(indices_npy[i], np.NPY_OWNDATA)
1361+
PyArray_ENABLEFLAGS(indices_npy[i], cnp.NPY_OWNDATA)
13621362
# make sure the data is not freed twice
13631363
indices[i] = NULL
13641364

sklearn/neighbors/_quad_tree.pxd

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
# See quad_tree.pyx for details.
55

6-
import numpy as np
7-
cimport numpy as np
6+
cimport numpy as cnp
87

9-
ctypedef np.npy_float32 DTYPE_t # Type of X
10-
ctypedef np.npy_intp SIZE_t # Type for indices and counters
11-
ctypedef np.npy_int32 INT32_t # Signed 32 bit integer
12-
ctypedef np.npy_uint32 UINT32_t # Unsigned 32 bit integer
8+
ctypedef cnp.npy_float32 DTYPE_t # Type of X
9+
ctypedef cnp.npy_intp SIZE_t # Type for indices and counters
10+
ctypedef cnp.npy_int32 INT32_t # Signed 32 bit integer
11+
ctypedef cnp.npy_uint32 UINT32_t # Unsigned 32 bit integer
1312

1413
# This is effectively an ifdef statement in Cython
1514
# It allows us to write printf debugging lines
@@ -94,4 +93,4 @@ cdef class _QuadTree:
9493
cdef int _resize(self, SIZE_t capacity) nogil except -1
9594
cdef int _resize_c(self, SIZE_t capacity=*) nogil except -1
9695
cdef int _get_cell(self, DTYPE_t[3] point, SIZE_t cell_id=*) nogil except -1
97-
cdef np.ndarray _get_cell_ndarray(self)
96+
cdef cnp.ndarray _get_cell_ndarray(self)

sklearn/neighbors/_quad_tree.pyx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ from ..tree._utils cimport safe_realloc, sizet_ptr_to_ndarray
1313
from ..utils import check_array
1414

1515
import numpy as np
16-
cimport numpy as np
17-
np.import_array()
16+
cimport numpy as cnp
17+
cnp.import_array()
1818

1919
cdef extern from "math.h":
2020
float fabsf(float x) nogil
2121

2222
cdef extern from "numpy/arrayobject.h":
23-
object PyArray_NewFromDescr(PyTypeObject* subtype, np.dtype descr,
24-
int nd, np.npy_intp* dims,
25-
np.npy_intp* strides,
23+
object PyArray_NewFromDescr(PyTypeObject* subtype, cnp.dtype descr,
24+
int nd, cnp.npy_intp* dims,
25+
cnp.npy_intp* strides,
2626
void* data, int flags, object obj)
27-
int PyArray_SetBaseObject(np.ndarray arr, PyObject* obj)
27+
int PyArray_SetBaseObject(cnp.ndarray arr, PyObject* obj)
2828

2929
# Build the corresponding numpy dtype for Cell.
3030
# This works by casting `dummy` to an array of Cell of length 1, which numpy
@@ -527,30 +527,30 @@ cdef class _QuadTree:
527527
if self._resize_c(self.capacity) != 0:
528528
raise MemoryError("resizing tree to %d" % self.capacity)
529529

530-
cells = memcpy(self.cells, (<np.ndarray> cell_ndarray).data,
530+
cells = memcpy(self.cells, (<cnp.ndarray> cell_ndarray).data,
531531
self.capacity * sizeof(Cell))
532532

533533

534534
# Array manipulation methods, to convert it to numpy or to resize
535535
# self.cells array
536536

537-
cdef np.ndarray _get_cell_ndarray(self):
537+
cdef cnp.ndarray _get_cell_ndarray(self):
538538
"""Wraps nodes as a NumPy struct array.
539539
540540
The array keeps a reference to this Tree, which manages the underlying
541541
memory. Individual fields are publicly accessible as properties of the
542542
Tree.
543543
"""
544-
cdef np.npy_intp shape[1]
545-
shape[0] = <np.npy_intp> self.cell_count
546-
cdef np.npy_intp strides[1]
544+
cdef cnp.npy_intp shape[1]
545+
shape[0] = <cnp.npy_intp> self.cell_count
546+
cdef cnp.npy_intp strides[1]
547547
strides[0] = sizeof(Cell)
548-
cdef np.ndarray arr
548+
cdef cnp.ndarray arr
549549
Py_INCREF(CELL_DTYPE)
550550
arr = PyArray_NewFromDescr(<PyTypeObject *> np.ndarray,
551551
CELL_DTYPE, 1, shape,
552552
strides, <void*> self.cells,
553-
np.NPY_DEFAULT, None)
553+
cnp.NPY_DEFAULT, None)
554554
Py_INCREF(self)
555555
if PyArray_SetBaseObject(arr, <PyObject*> self) < 0:
556556
raise ValueError("Can't initialize array!")

0 commit comments

Comments
 (0)
0