E56A MNT Updates _isotonic.pyx to use memoryviews instead of `cnp.ndarray`… · scikit-learn/scikit-learn@10eaa52 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10eaa52

Browse files
authored
MNT Updates _isotonic.pyx to use memoryviews instead of cnp.ndarray (#26068)
1 parent 9863359 commit 10eaa52

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def check_package_status(package, min_version):
196196
{"sources": ["_check_build.pyx"]},
197197
],
198198
"": [
199-
{"sources": ["_isotonic.pyx"], "include_np": True},
199+
{"sources": ["_isotonic.pyx"]},
200200
],
201201
"_loss": [
202202
{"sources": ["_loss.pyx.tp"]},

sklearn/_isotonic.pyx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
# pool at each step.
66

77
import numpy as np
8-
cimport numpy as cnp
98
from cython cimport floating
109

11-
cnp.import_array()
1210

1311

1412
def _inplace_contiguous_isotonic_regression(floating[::1] y, floating[::1] w):
@@ -62,9 +60,9 @@ def _inplace_contiguous_isotonic_regression(floating[::1] y, floating[::1] w):
6260
i = k
6361

6462

65-
def _make_unique(cnp.ndarray[dtype=floating] X,
66-
cnp.ndarray[dtype=floating] y,
67-
cnp.ndarray[dtype=floating] sample_weights):
63+
def _make_unique(const floating[::1] X,
64+
const floating[::1] y,
65+
const floating[::1] sample_weights):
6866
"""Average targets for duplicate X, drop duplicates.
6967
7068
Aggregates duplicate X values into a single X value where
@@ -75,10 +73,14 @@ def _make_unique(cnp.ndarray[dtype=floating] X,
7573
"""
7674
unique_values = len(np.unique(X))
7775

78-
cdef cnp.ndarray[dtype=floating] y_out = np.empty(unique_values,
79-
dtype=X.dtype)
80-
cdef cnp.ndarray[dtype=floating] x_out = np.empty_like(y_out)
81-
cdef cnp.ndarray[dtype=floating] weights_out = np.empty_like(y_out)
76+
if floating is float:
77+
dtype = np.float32
78+
else:
79+
dtype = np.float64
80+
81+
cdef floating[::1] y_out = np.empty(unique_values, dtype=dtype)
82+
cdef floating[::1] x_out = np.empty_like(y_out)
83+
cdef floating[::1] weights_out = np.empty_like(y_out)
8284

8385
cdef floating current_x = X[0]
8486
cdef floating current_y = 0
@@ -88,7 +90, 9564 7 @@ def _make_unique(cnp.ndarray[dtype=floating] X,
8890
cdef int j
8991
cdef floating x
9092
cdef int n_samples = len(X)
91-
cdef floating eps = np.finfo(X.dtype).resolution
93+
cdef floating eps = np.finfo(dtype).resolution
9294

9395
for j in range(n_samples):
9496
x = X[j]
@@ -108,4 +110,8 @@ def _make_unique(cnp.ndarray[dtype=floating] X,
108110
x_out[i] = current_x
109111
weights_out[i] = current_weight
110112
y_out[i] = current_y / current_weight
111-
return x_out[:i+1], y_out[:i+1], weights_out[:i+1]
113+
return(
114+
np.asarray(x_out[:i+1]),
115+
np.asarray(y_out[:i+1]),
116+
np.asarray(weights_out[:i+1]),
117+
)

0 commit comments

Comments
 (0)
0