Skip to content

Commit 346362f

Browse files
authored
MAINT: make compatible with NumPy 2 (#525)
Signed-off-by: DerWeh <andreas.weh@web.de>
1 parent d06e9f0 commit 346362f

4 files changed

Lines changed: 28 additions & 28 deletions

File tree

python/interpret-core/interpret/utils/_clean_simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ def typify_classification(vec):
260260
):
261261
dtype = np.bool_
262262
else:
263-
dtype = np.unicode_
263+
dtype = np.str_
264264
else:
265-
dtype = np.unicode_
265+
dtype = np.str_
266266

267267
return vec.astype(dtype, copy=False)
268268

python/interpret-core/interpret/utils/_clean_x.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@
294294
dict,
295295
Ellipsis,
296296
np.csingle,
297-
np.complex_,
298-
np.clongfloat,
297+
np.complex128,
298+
np.clongdouble,
299299
np.void,
300300
]
301301
)
@@ -314,7 +314,7 @@ def _densify_object_ndarray(X_col):
314314
types = set(map(type, X_col))
315315
if len(types) == 1:
316316
if str in types:
317-
return X_col.astype(np.unicode_)
317+
return X_col.astype(np.str_)
318318
elif bool in types:
319319
return X_col.astype(np.bool_)
320320

@@ -353,7 +353,7 @@ def _densify_object_ndarray(X_col):
353353
# it will silently convert negative integers to unsigned!
354354

355355
# TODO : should this be np.float64 with a check for big integers
356-
return X_col.astype(np.unicode_)
356+
return X_col.astype(np.str_)
357357

358358
if all(
359359
one_type is float or issubclass(one_type, np.floating) for one_type in types
@@ -430,7 +430,7 @@ def _densify_object_ndarray(X_col):
430430
# writing our own cython code that can be more efficient at walking through items in an array. If we write
431431
# our own cython there is the added advantage that we can check types in the same loop and therefore eliminate
432432
# the costly "set(map(type, X_col))" calls above
433-
return X_col.astype(np.unicode_)
433+
return X_col.astype(np.str_)
434434

435435

436436
def _process_column_initial(X_col, nonmissings, processing, min_unique_continuous):
@@ -448,9 +448,9 @@ def _process_column_initial(X_col, nonmissings, processing, min_unique_continuou
448448

449449
if issubclass(uniques.dtype.type, np.floating):
450450
floats = uniques.astype(np.float64, copy=False)
451-
uniques = floats.astype(np.unicode_)
451+
uniques = floats.astype(np.str_)
452452
else:
453-
uniques = uniques.astype(np.unicode_, copy=False)
453+
uniques = uniques.astype(np.str_, copy=False)
454454
try:
455455
# we rely here on there being a round trip format within this language from float64 to text to float64
456456

@@ -544,7 +544,7 @@ def _encode_categorical_existing(X_col, nonmissings, categories):
544544

545545
if issubclass(X_col.dtype.type, np.floating):
546546
uniques = uniques.astype(np.float64, copy=False)
547-
uniques = uniques.astype(np.unicode_, copy=False)
547+
uniques = uniques.astype(np.str_, copy=False)
548548

549549
mapping = np.fromiter(
550550
(categories.get(val, -1) for val in uniques), np.int64, count=len(uniques)
@@ -725,7 +725,7 @@ def _process_continuous(X_col, nonmissings):
725725
floats[idx] = one_item_array.astype(dtype=np.float64)[0]
726726
except TypeError:
727727
# use .astype instead of str(one_item_array) here to ensure identical string categories
728-
one_str_array = one_item_array.astype(dtype=np.unicode_)
728+
one_str_array = one_item_array.astype(dtype=np.str_)
729729
try:
730730
# use .astype(..) instead of float(..) to ensure identical conversion results
731731
floats[idx] = one_str_array.astype(dtype=np.float64)[0]
@@ -948,7 +948,7 @@ def _process_pandas_column(X_col, categories, feature_type, min_unique_continuou
948948
# unlike other missing value types, we get back -1's for missing here, so no need to drop them
949949
X_col = X_col.values
950950
is_ordered = X_col.ordered
951-
pd_categories = X_col.categories.values.astype(dtype=np.unicode_, copy=False)
951+
pd_categories = X_col.categories.values.astype(dtype=np.str_, copy=False)
952952
X_col = X_col.codes
953953

954954
if feature_type == "ignore":

python/interpret-core/interpret/utils/_measure_interactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def measure_interactions(
159159
try:
160160
y_discard = y.astype(dtype=np.float64, copy=False)
161161
except (TypeError, ValueError):
162-
y_discard = y.astype(dtype=np.unicode_, copy=False)
162+
y_discard = y.astype(dtype=np.str_, copy=False)
163163

164164
target_type = type_of_target(y_discard)
165165
if target_type == "continuous":

python/interpret-core/tests/utils/test_clean_x.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,15 @@ def test_process_continuous_obj_hard_bad():
628628

629629

630630
def test_process_continuous_str_simple():
631-
vals, bad = _process_continuous(np.array(["1", "2.5"], dtype=np.unicode_), None)
631+
vals, bad = _process_continuous(np.array(["1", "2.5"], dtype=np.str_), None)
632632
assert bad is None
633633
assert vals.dtype == np.float64
634634
assert np.array_equal(vals, np.array([1, 2.5], dtype=np.float64))
635635

636636

637637
def test_process_continuous_str_simple_missing():
638638
vals, bad = _process_continuous(
639-
np.array(["1", "2.5"], dtype=np.unicode_),
639+
np.array(["1", "2.5"], dtype=np.str_),
640640
np.array([True, True, False], dtype=np.bool_),
641641
)
642642
assert bad is None
@@ -649,7 +649,7 @@ def test_process_continuous_str_simple_missing():
649649

650650
def test_process_continuous_str_hard_bad():
651651
vals, bad = _process_continuous(
652-
np.array(["1", "2.5", "bad"], dtype=np.unicode_),
652+
np.array(["1", "2.5", "bad"], dtype=np.str_),
653653
np.array([True, True, True, False], dtype=np.bool_),
654654
)
655655
assert len(bad) == 4
@@ -708,7 +708,7 @@ def test_process_column_initial_obj_obj():
708708

709709
def test_process_column_initial_alphabetical_nomissing():
710710
encoded, c = _process_column_initial(
711-
np.array(["xyz", "abc", "xyz"], dtype=np.unicode_),
711+
np.array(["xyz", "abc", "xyz"], dtype=np.str_),
712712
None,
713713
"nominal_alphabetical",
714714
None,
@@ -723,7 +723,7 @@ def test_process_column_initial_alphabetical_nomissing():
723723

724724
def test_process_column_initial_alphabetical_missing():
725725
encoded, c = _process_column_initial(
726-
np.array(["xyz", "abc", "xyz"], dtype=np.unicode_),
726+
np.array(["xyz", "abc", "xyz"], dtype=np.str_),
727727
np.array([True, True, False, True], dtype=np.bool_),
728728
"nominal_alphabetical",
729729
None,
@@ -738,7 +738,7 @@ def test_process_column_initial_alphabetical_missing():
738738

739739
def test_process_column_initial_prevalence_nomissing():
740740
encoded, c = _process_column_initial(
741-
np.array(["xyz", "abc", "xyz"], dtype=np.unicode_),
741+
np.array(["xyz", "abc", "xyz"], dtype=np.str_),
742742
None,
743743
"nominal_prevalence",
744744
None,
@@ -753,7 +753,7 @@ def test_process_column_initial_prevalence_nomissing():
753753

754754
def test_process_column_initial_prevalence_missing():
755755
encoded, c = _process_column_initial(
756-
np.array(["xyz", "abc", "xyz"], dtype=np.unicode_),
756+
np.array(["xyz", "abc", "xyz"], dtype=np.str_),
757757
np.array([True, True, False, True], dtype=np.bool_),
758758
"nominal_prevalence",
759759
None,
@@ -768,7 +768,7 @@ def test_process_column_initial_prevalence_missing():
768768

769769
def test_process_column_initial_float64_nomissing():
770770
encoded, c = _process_column_initial(
771-
np.array(["11.1", "2.2", "11.1"], dtype=np.unicode_),
771+
np.array(["11.1", "2.2", "11.1"], dtype=np.str_),
772772
None,
773773
"ANYTHING_ELSE",
774774
None,
@@ -783,7 +783,7 @@ def test_process_column_initial_float64_nomissing():
783783

784784
def test_process_column_initial_float64_missing():
785785
encoded, c = _process_column_initial(
786-
np.array(["11.1", "2.2", "11.1"], dtype=np.unicode_),
786+
np.array(["11.1", "2.2", "11.1"], dtype=np.str_),
787787
np.array([True, True, False, True], dtype=np.bool_),
788788
"ANYTHING_ELSE",
789789
None,
@@ -1016,7 +1016,7 @@ def test_encode_categorical_existing_obj_floats():
10161016
np.float16(2.2),
10171017
np.float32(3.3),
10181018
np.float64(4.4),
1019-
np.longfloat(5.5),
1019+
np.longdouble(5.5),
10201020
],
10211021
dtype=np.object_,
10221022
),
@@ -1110,7 +1110,7 @@ def test_encode_categorical_existing_obj_obj():
11101110
def test_encode_categorical_existing_str():
11111111
c = {"abc": 1, "def": 2, "ghi": 3}
11121112
encoded, bad = _encode_categorical_existing(
1113-
np.array(["abc", "ghi", "def", "something"], dtype=np.unicode_),
1113+
np.array(["abc", "ghi", "def", "something"], dtype=np.str_),
11141114
np.array([True, True, False, True, True], dtype=np.bool_),
11151115
c,
11161116
)
@@ -1144,7 +1144,7 @@ def test_encode_categorical_existing_int8():
11441144
def test_encode_categorical_existing_bool():
11451145
c = {"False": 1, "True": 2}
11461146
encoded, bad = _encode_categorical_existing(
1147-
np.array([False, True, False], dtype=np.unicode_),
1147+
np.array([False, True, False], dtype=np.str_),
11481148
np.array([True, True, False, True], dtype=np.bool_),
11491149
c,
11501150
)
@@ -1157,7 +1157,7 @@ def test_encode_categorical_existing_bool():
11571157
def test_encode_categorical_existing_bool_true():
11581158
c = {"True": 1}
11591159
encoded, bad = _encode_categorical_existing(
1160-
np.array([False, True, False], dtype=np.unicode_),
1160+
np.array([False, True, False], dtype=np.str_),
11611161
np.array([True, True, False, True], dtype=np.bool_),
11621162
c,
11631163
)
@@ -1170,7 +1170,7 @@ def test_encode_categorical_existing_bool_true():
11701170
def test_encode_categorical_existing_bool_false():
11711171
c = {"False": 1}
11721172
encoded, bad = _encode_categorical_existing(
1173-
np.array([False, True, False], dtype=np.unicode_),
1173+
np.array([False, True, False], dtype=np.str_),
11741174
np.array([True, True, False, True], dtype=np.bool_),
11751175
c,
11761176
)
@@ -1794,7 +1794,7 @@ def test_unify_columns_pandas_missings_float64():
17941794

17951795

17961796
def test_unify_columns_pandas_missings_longfloat():
1797-
check_pandas_float(np.longfloat, -1.1, 2.2)
1797+
check_pandas_float(np.longdouble, -1.1, 2.2)
17981798

17991799

18001800
def test_unify_columns_pandas_missings_float32():

0 commit comments

Comments
 (0)