File tree 2 files changed +23
-13
lines changed
Filter options
2 files changed +23
-13
lines changed
Original file line number Diff line number Diff line change @@ -12,13 +12,6 @@ from libcpp.map cimport map as cpp_map
12
12
13
13
import numpy as np
14
14
15
- # Import the C-level symbols of numpy
16
- cimport numpy as cnp
17
-
18
- # Numpy must be initialized. When using numpy from C or Cython you must
19
- # _always_ do that, or you will have segfaults
20
- cnp.import_array()
21
-
22
15
# DTYPE = np.float64
23
16
# ctypedef cnp.float64_t DTYPE_t
24
17
@@ -35,8 +28,11 @@ cnp.import_array()
35
28
36
29
cdef class IntFloatDict:
37
30
38
- def __init__ (self , cnp.ndarray[ITYPE_t , ndim = 1 ] keys,
39
- cnp.ndarray[DTYPE_t , ndim = 1 ] values):
31
+ def __init__ (
32
+ self ,
33
+ ITYPE_t[:] keys ,
34
+ DTYPE_t[:] values ,
35
+ ):
40
36
cdef int i
41
37
cdef int size = values.size
42
38
# Should check that sizes for keys and values are equal, and
@@ -91,10 +87,8 @@ cdef class IntFloatDict:
91
87
The values of the data points
92
88
"""
93
89
cdef int size = self .my_map.size()
94
- cdef cnp.ndarray[ITYPE_t, ndim= 1 ] keys = np.empty(size,
95
- dtype = np.intp)
96
- cdef cnp.ndarray[DTYPE_t, ndim= 1 ] values = np.empty(size,
97
- dtype = np.float64)
90
+ keys = np.empty(size, dtype = np.intp)
91
+ values = np.empty(size, dtype = np.float64)
98
92
self ._to_arrays(keys, values)
99
93
return keys, values
100
94
Original file line number Diff line number Diff line change 1
1
""" Test fast_dict.
2
2
"""
3
3
import numpy as np
4
+ from numpy .testing import assert_array_equal , assert_allclose
4
5
5
6
from sklearn .utils ._fast_dict import IntFloatDict , argmin
6
7
@@ -29,3 +30,18 @@ def test_int_float_dict_argmin():
29
30
values = np .arange (100 , dtype = np .float64 )
30
31
d = IntFloatDict (keys , values )
31
32
assert argmin (d ) == (0 , 0 )
33
+
34
+
35
+ def test_to_arrays ():
36
+ # Test that an IntFloatDict is converted into arrays
37
+ # of keys and values correctly
38
+ keys_in = np .array ([1 , 2 , 3 ], dtype = np .intp )
39
+ values_in = np .array ([4 , 5 , 6 ], dtype = np .float64 )
40
+
41
+ d = IntFloatDict (keys_in , values_in )
42
+ keys_out , values_out = d .to_arrays ()
43
+
44
+ assert keys_out .dtype == keys_in .dtype
45
+ assert values_in .dtype == values_out .dtype
46
+ assert_array_equal (keys_out , keys_in )
47
+ assert_allclose (values_out , values_in )
You can’t perform that action at this time.
0 commit comments