Skip to content

Commit

Permalink
Fix numpy 2.0 deprecations (#5866)
Browse files Browse the repository at this point in the history
Split from #5799

* `product` -> `prod`
* Coercing single element arrays to it's scalar value via `int` or similar appears deprecated, so adjusted not to do this where possible

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - Dante Gama Dessavre (https://github.com/dantegd)

URL: #5866
  • Loading branch information
mroeschke authored Apr 25, 2024
1 parent 4394a2e commit d3d0ff3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 40 deletions.
10 changes: 3 additions & 7 deletions python/cuml/datasets/blobs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -209,12 +209,8 @@ def make_blobs(
proba_samples_per_center = np.array(n_samples_per_center) / np.sum(
n_samples_per_center
)
np_seed = int(generator.randint(n_samples, size=1))
np.random.seed(np_seed)
shuffled_sample_indices = cp.array(
np.random.choice(
n_centers, n_samples, replace=True, p=proba_samples_per_center
)
shuffled_sample_indices = generator.choice(
n_centers, n_samples, replace=True, p=proba_samples_per_center
)
for i, (n, std) in enumerate(zip(n_samples_per_center, cluster_std)):
center_indices = cp.where(shuffled_sample_indices == i)
Expand Down
15 changes: 4 additions & 11 deletions python/cuml/datasets/classification.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -120,7 +120,7 @@ def make_classification(
[-0.8817671 -0.84549576 0.1845096 0.02556021]]
>>> print(y)
[0 1 0 1 1 0 0 1 0 0]
[1 0 1 1 1 1 1 1 1 0]
Parameters
----------
Expand Down Expand Up @@ -229,8 +229,6 @@ def make_classification(
cuml.internals.set_api_output_type("cupy")

generator = _create_rs_generator(random_state)
np_seed = int(generator.randint(n_samples, size=1))
np.random.seed(np_seed)

# Count features, clusters and samples
if n_informative + n_redundant + n_repeated > n_features:
Expand Down Expand Up @@ -307,13 +305,8 @@ def make_classification(
proba_samples_per_cluster = np.array(n_samples_per_cluster) / np.sum(
n_samples_per_cluster
)
shuffled_sample_indices = cp.array(
np.random.choice(
n_clusters,
n_samples,
replace=True,
p=proba_samples_per_cluster,
)
shuffled_sample_indices = generator.choice(
n_clusters, n_samples, replace=True, p=proba_samples_per_cluster
)
for k, centroid in enumerate(centroids):
centroid_indices = cp.where(shuffled_sample_indices == k)
Expand Down
4 changes: 2 additions & 2 deletions python/cuml/internals/array.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -447,7 +447,7 @@ def is_host_accessible(self):
@cached_property
def size(self):
return (
host_xpy.product(self._array_interface["shape"])
host_xpy.prod(self._array_interface["shape"])
* host_xpy.dtype(self._array_interface["typestr"]).itemsize
)

Expand Down
4 changes: 2 additions & 2 deletions python/cuml/linear_model/ridge.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2019-2023, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -101,7 +101,7 @@ class Ridge(UniversalBase,
>>> from cuml import Ridge
>>> from cuml.linear_model import Ridge
>>> alpha = cp.array([1e-5])
>>> alpha = 1e-5
>>> ridge = Ridge(alpha=alpha, fit_intercept=True, normalize=False,
... solver="eig")
Expand Down
8 changes: 4 additions & 4 deletions python/cuml/multiclass/multiclass.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,7 +63,7 @@ class MulticlassClassifier(Base, ClassifierMixin):
>>> cls.fit(X,y)
MulticlassClassifier()
>>> cls.predict(X)
array([2, 0, 2, 2, 2, 1, 1, 0, 1, 1])
array([1, 1, 1, 1, 1, 1, 2, 1, 1, 2])
Parameters
----------
Expand Down Expand Up @@ -228,7 +228,7 @@ class OneVsRestClassifier(MulticlassClassifier):
>>> cls.fit(X,y)
OneVsRestClassifier()
>>> cls.predict(X)
array([2, 0, 2, 2, 2, 1, 1, 0, 1, 1])
array([1, 1, 1, 1, 1, 1, 2, 1, 1, 2])
Parameters
Expand Down Expand Up @@ -303,7 +303,7 @@ class OneVsOneClassifier(MulticlassClassifier):
>>> cls.fit(X,y)
OneVsOneClassifier()
>>> cls.predict(X)
array([2, 0, 2, 2, 2, 1, 1, 0, 1, 1])
array([1, 1, 1, 1, 1, 1, 2, 1, 1, 2])
Parameters
----------
Expand Down
10 changes: 5 additions & 5 deletions python/cuml/neighbors/nearest_neighbors.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2019-2022, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -253,11 +253,11 @@ class NearestNeighbors(UniversalBase,
>>> # print results
>>> print(indices)
0 1 2
0 0 1 3
1 1 0 2
0 0 3 1
1 1 3 0
2 2 4 0
3 3 0 2
4 4 2 3
3 3 0 1
4 4 2 0
>>> print(distances) # doctest: +SKIP
0 1 2
0 0.007812 24.786566 26.399996
Expand Down
12 changes: 5 additions & 7 deletions python/cuml/tests/test_coordinate_descent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,6 @@


@pytest.mark.parametrize("datatype", [np.float32, np.float64])
@pytest.mark.parametrize("X_type", ["ndarray"])
@pytest.mark.parametrize("alpha", [0.1, 0.001])
@pytest.mark.parametrize("algorithm", ["cyclic", "random"])
@pytest.mark.parametrize(
Expand All @@ -42,7 +41,7 @@
],
)
@pytest.mark.filterwarnings("ignore:Objective did not converge::sklearn[.*]")
def test_lasso(datatype, X_type, alpha, algorithm, nrows, column_info):
def test_lasso(datatype, alpha, algorithm, nrows, column_info):
ncols, n_info = column_info
X, y = make_regression(
n_samples=nrows, n_features=ncols, n_informative=n_info, random_state=0
Expand All @@ -53,7 +52,7 @@ def test_lasso(datatype, X_type, alpha, algorithm, nrows, column_info):
X, y, train_size=0.8, random_state=0
)
cu_lasso = cuLasso(
alpha=np.array([alpha]),
alpha=alpha,
fit_intercept=True,
max_iter=1000,
selection=algorithm,
Expand Down Expand Up @@ -165,7 +164,6 @@ def test_weighted_cd(datatype, model, fit_intercept, distribution):


@pytest.mark.parametrize("datatype", [np.float32, np.float64])
@pytest.mark.parametrize("X_type", ["ndarray"])
@pytest.mark.parametrize("alpha", [0.2, 0.7])
@pytest.mark.parametrize("algorithm", ["cyclic", "random"])
@pytest.mark.parametrize(
Expand All @@ -180,7 +178,7 @@ def test_weighted_cd(datatype, model, fit_intercept, distribution):
],
)
@pytest.mark.filterwarnings("ignore:Objective did not converge::sklearn[.*]")
def test_elastic_net(datatype, X_type, alpha, algorithm, nrows, column_info):
def test_elastic_net(datatype, alpha, algorithm, nrows, column_info):
ncols, n_info = column_info
X, y = make_regression(
n_samples=nrows, n_features=ncols, n_informative=n_info, random_state=0
Expand All @@ -192,7 +190,7 @@ def test_elastic_net(datatype, X_type, alpha, algorithm, nrows, column_info):
)

elastic_cu = cuElasticNet(
alpha=np.array([alpha]),
alpha=alpha,
fit_intercept=True,
max_iter=1000,
selection=algorithm,
Expand Down
4 changes: 2 additions & 2 deletions python/cuml/tests/test_incremental_pca.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_partial_fit(
sk_t = sk_ipca.transform(X)
sk_inv = sk_ipca.inverse_transform(sk_t)

assert array_equal(cu_inv, sk_inv, 5e-5, with_sign=True)
assert array_equal(cu_inv, sk_inv, 6e-5, with_sign=True)


def test_exceptions():
Expand Down

0 comments on commit d3d0ff3

Please sign in to comment.