Skip to content

Commit 03aa496

Browse files
MNT Remove fixtures for Numpy 1.3 and 1.4 compatibility (#20323)
Co-authored-by: Thomas J. Fan <[email protected]>
1 parent 460c961 commit 03aa496

File tree

2 files changed

+10
-32
lines changed

2 files changed

+10
-32
lines changed

sklearn/neighbors/_dist_metrics.pxd

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ cdef class DistanceMetric:
5151
# Because we don't expect to instantiate a lot of these objects, the
5252
# extra memory overhead of this setup should not be an issue.
5353
cdef DTYPE_t p
54-
#cdef DTYPE_t[::1] vec
55-
#cdef DTYPE_t[:, ::1] mat
56-
cdef np.ndarray vec
57-
cdef np.ndarray mat
58-
cdef DTYPE_t* vec_ptr
59-
cdef DTYPE_t* mat_ptr
54+
cdef DTYPE_t[::1] vec
55+
cdef DTYPE_t[:, ::1] mat
6056
cdef ITYPE_t size
6157
cdef object func
6258
cdef object kwargs

sklearn/neighbors/_dist_metrics.pyx

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!python
22
#cython: boundscheck=False
33
#cython: wraparound=False
4+
#cython: initializedcheck=False
45
#cython: cdivision=True
56

67
# By Jake Vanderplas (2013) <[email protected]>
@@ -12,17 +13,6 @@ cimport numpy as np
1213
np.import_array() # required in order to use C-API
1314

1415

15-
######################################################################
16-
# Numpy 1.3-1.4 compatibility utilities
17-
cdef DTYPE_t* get_vec_ptr(np.ndarray[DTYPE_t, ndim=1, mode='c'] vec):
18-
return &vec[0]
19-
20-
21-
cdef DTYPE_t* get_mat_ptr(np.ndarray[DTYPE_t, ndim=2, mode='c'] mat):
22-
return &mat[0, 0]
23-
######################################################################
24-
25-
2616
# First, define a function to get an ndarray from a memory bufffer
2717
cdef extern from "arrayobject.h":
2818
object PyArray_SimpleNewFromData(int nd, np.npy_intp* dims,
@@ -209,8 +199,6 @@ cdef class DistanceMetric:
209199
self.p = 2
210200
self.vec = np.zeros(1, dtype=DTYPE, order='c')
211201
self.mat = np.zeros((1, 1), dtype=DTYPE, order='c')
212-
self.vec_ptr = get_vec_ptr(self.vec)
213-
self.mat_ptr = get_mat_ptr(self.mat)
214202
self.size = 1
215203

216204
def __reduce__(self):
@@ -224,8 +212,8 @@ cdef class DistanceMetric:
224212
get state for pickling
225213
"""
226214
if self.__class__.__name__ == "PyFuncDistance":
227-
return (float(self.p), self.vec, self.mat, self.func, self.kwargs)
228-
return (float(self.p), self.vec, self.mat)
215+
return (float(self.p), np.asarray(self.vec), np.asarray(self.mat), self.func, self.kwargs)
216+
return (float(self.p), np.asarray(self.vec), np.asarray(self.mat))
229217

230218
def __setstate__(self, state):
231219
"""
@@ -237,8 +225,6 @@ cdef class DistanceMetric:
237225
if self.__class__.__name__ == "PyFuncDistance":
238226
self.func = state[3]
239227
self.kwargs = state[4]
240-
self.vec_ptr = get_vec_ptr(self.vec)
241-
self.mat_ptr = get_mat_ptr(self.mat)
242228
self.size = self.vec.shape[0]
243229

244230
@classmethod
@@ -468,7 +454,6 @@ cdef class SEuclideanDistance(DistanceMetric):
468454
"""
469455
def __init__(self, V):
470456
self.vec = np.asarray(V, dtype=DTYPE)
471-
self.vec_ptr = get_vec_ptr(self.vec)
472457
self.size = self.vec.shape[0]
473458
self.p = 2
474459

@@ -482,7 +467,7 @@ cdef class SEuclideanDistance(DistanceMetric):
482467
cdef np.intp_t j
483468
for j in range(size):
484469
tmp = x1[j] - x2[j]
485-
d += tmp * tmp / self.vec_ptr[j]
470+
d += tmp * tmp / self.vec[j]
486471
return d
487472

488473
cdef inline DTYPE_t dist(self, const DTYPE_t* x1, const DTYPE_t* x2,
@@ -630,7 +615,6 @@ cdef class WMinkowskiDistance(DistanceMetric):
630615
"For p=inf, use ChebyshevDistance.")
631616
self.p = p
632617
self.vec = np.asarray(w, dtype=DTYPE)
633-
self.vec_ptr = get_vec_ptr(self.vec)
634618
self.size = self.vec.shape[0]
635619

636620
def _validate_data(self, X):
@@ -643,7 +627,7 @@ cdef class WMinkowskiDistance(DistanceMetric):
643627
cdef DTYPE_t d=0
644628
cdef np.intp_t j
645629
for j in range(size):
646-
d += pow(self.vec_ptr[j] * fabs(x1[j] - x2[j]), self.p)
630+
d += pow(self.vec[j] * fabs(x1[j] - x2[j]), self.p)
647631
return d
648632

649633
cdef inline DTYPE_t dist(self, const DTYPE_t* x1, const DTYPE_t* x2,
@@ -691,13 +675,11 @@ cdef class MahalanobisDistance(DistanceMetric):
691675
raise ValueError("V/VI must be square")
692676

693677
self.mat = np.asarray(VI, dtype=float, order='C')
694-
self.mat_ptr = get_mat_ptr(self.mat)
695678

696679
self.size = self.mat.shape[0]
697680

698681
# we need vec as a work buffer
699682
self.vec = np.zeros(self.size, dtype=DTYPE)
700-
self.vec_ptr = get_vec_ptr(self.vec)
701683

702684
def _validate_data(self, X):
703685
if X.shape[1] != self.size:
@@ -710,13 +692,13 @@ cdef class MahalanobisDistance(DistanceMetric):
710692

711693
# compute (x1 - x2).T * VI * (x1 - x2)
712694
for i in range(size):
713-
self.vec_ptr[i] = x1[i] - x2[i]
695+
self.vec[i] = x1[i] - x2[i]
714696

715697
for i in range(size):
716698
tmp = 0
717699
for j in range(size):
718-
tmp += self.mat_ptr[i * size + j] * self.vec_ptr[j]
719-
d += tmp * self.vec_ptr[i]
700+
tmp += self.mat[i, j] * self.vec[j]
701+
d += tmp * self.vec[i]
720702
return d
721703

722704
cdef inline DTYPE_t dist(self, const DTYPE_t* x1, const DTYPE_t* x2,

0 commit comments

Comments
 (0)