From 542f2c727afd64a85013fd2bf627130175c9f38a Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Thu, 3 Mar 2022 15:24:36 +0100 Subject: [PATCH 1/9] TST Adapt test_affinity_propagation.py to test implementations on 32bit datasets --- .../tests/test_affinity_propagation.py | 58 ++++++++++++------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index 26b40c8befc9e..0451b0f2b758d 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -27,11 +27,14 @@ random_state=0, ) +DTYPES = (np.float32, np.float64) -def test_affinity_propagation(): + +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation(dtype): # Affinity Propagation algorithm # Compute similarities - S = -euclidean_distances(X, squared=True) + S = -euclidean_distances(X.astype(dtype), squared=True) preference = np.median(S) * 10 # Compute Affinity Propagation cluster_centers_indices, labels = affinity_propagation( @@ -95,34 +98,38 @@ def test_affinity_propagation_params_validation(input, params, err_type, err_msg AffinityPropagation(**params).fit(input) -def test_affinity_propagation_predict(): +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation_predict(dtype): # Test AffinityPropagation.predict af = AffinityPropagation(affinity="euclidean", random_state=63) - labels = af.fit_predict(X) - labels2 = af.predict(X) + labels = af.fit_predict(X.astype(dtype)) + labels2 = af.predict(X.astype(dtype)) assert_array_equal(labels, labels2) -def test_affinity_propagation_predict_error(): +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation_predict_error(dtype): # Test exception in AffinityPropagation.predict # Not fitted. + Y = X.astype(dtype) af = AffinityPropagation(affinity="euclidean") with pytest.raises(ValueError): - af.predict(X) + af.predict(Y) # Predict not supported when affinity="precomputed". - S = np.dot(X, X.T) + S = np.dot(Y, Y.T) af = AffinityPropagation(affinity="precomputed", random_state=57) af.fit(S) with pytest.raises(ValueError): - af.predict(X) + af.predict(Y) -def test_affinity_propagation_fit_non_convergence(): +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation_fit_non_convergence(dtype): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array and training samples should be labelled # as noise (-1) - X = np.array([[0, 0], [1, 1], [-2, -2]]) + X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(dtype) # Force non-convergence by allowing only a single iteration af = AffinityPropagation(preference=-10, max_iter=1, random_state=82) @@ -133,8 +140,9 @@ def test_affinity_propagation_fit_non_convergence(): assert_array_equal(np.array([-1, -1, -1]), af.labels_) -def test_affinity_propagation_equal_mutual_similarities(): - X = np.array([[-1, 1], [1, -1]]) +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation_equal_mutual_similarities(dtype): + X = np.array([[-1, 1], [1, -1]]).astype(dtype) S = -euclidean_distances(X, squared=True) # setting preference > similarity @@ -165,10 +173,11 @@ def test_affinity_propagation_equal_mutual_similarities(): assert_array_equal([0, 0], labels) -def test_affinity_propagation_predict_non_convergence(): +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation_predict_non_convergence(dtype): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array - X = np.array([[0, 0], [1, 1], [-2, -2]]) + X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=dtype) # Force non-convergence by allowing only a single iteration with pytest.warns(ConvergenceWarning): @@ -182,8 +191,11 @@ def test_affinity_propagation_predict_non_convergence(): assert_array_equal(np.array([-1, -1, -1]), y) -def test_affinity_propagation_non_convergence_regressiontest(): - X = np.array([[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]]) +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation_non_convergence_regressiontest(dtype): + X = np.array( + [[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]], dtype=dtype + ) af = AffinityPropagation(affinity="euclidean", max_iter=2, random_state=34) msg = ( "Affinity propagation did not converge, this model may return degenerate" @@ -195,9 +207,10 @@ def test_affinity_propagation_non_convergence_regressiontest(): assert_array_equal(np.array([0, 0, 0]), af.labels_) -def test_equal_similarities_and_preferences(): +@pytest.mark.parametrize("dtype", DTYPES) +def test_equal_similarities_and_preferences(dtype): # Unequal distances - X = np.array([[0, 0], [1, 1], [-2, -2]]) + X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(dtype) S = -euclidean_distances(X, squared=True) assert not _equal_similarities_and_preferences(S, np.array(0)) @@ -205,7 +218,7 @@ def test_equal_similarities_and_preferences(): assert not _equal_similarities_and_preferences(S, np.array([0, 1])) # Equal distances - X = np.array([[0, 0], [1, 1]]) + X = np.array([[0, 0], [1, 1]]).astype(dtype) S = -euclidean_distances(X, squared=True) # Different preferences @@ -237,10 +250,11 @@ def test_affinity_propagation_random_state(): @pytest.mark.parametrize("centers", [csr_matrix(np.zeros((1, 10))), np.zeros((1, 10))]) -def test_affinity_propagation_convergence_warning_dense_sparse(centers): +@pytest.mark.parametrize("dtype", DTYPES) +def test_affinity_propagation_convergence_warning_dense_sparse(centers, dtype): """Non-regression, see #13334""" rng = np.random.RandomState(42) - X = rng.rand(40, 10) + X = rng.rand(40, 10).astype(dtype) y = (4 * rng.rand(40)).astype(int) ap = AffinityPropagation(random_state=46) ap.fit(X, y) From d1fed6327af17c8baf00bc36cdffe3eaba2e2145 Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Fri, 18 Mar 2022 10:42:09 +0100 Subject: [PATCH 2/9] TST Use global_dtype --- .../tests/test_affinity_propagation.py | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index 883a6e744ff17..3bc6655e35713 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -29,15 +29,15 @@ random_state=0, ) -DTYPES = (np.float32, np.float64) - -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation(dtype): +def test_affinity_propagation(global_dtype): # Affinity Propagation algorithm # Compute similarities - S = -euclidean_distances(X.astype(dtype), squared=True) + S = -euclidean_distances(X.astype(global_dtype), squared=True) + assert S.dtype == global_dtype + preference = np.median(S) * 10 + # Compute Affinity Propagation cluster_centers_indices, labels = affinity_propagation( S, preference=preference, random_state=39 @@ -55,6 +55,12 @@ def test_affinity_propagation(dtype): af = AffinityPropagation(preference=preference, verbose=True, random_state=37) labels = af.fit(X).labels_ + # Fitted attribute must have the same dtype + # TODO: preserve dtype for cluster_centers_ + # assert af.cluster_centers_.dtype == global_dtype + # TODO: preserve dtype for affinity_matrix_ + # assert af.affinity_matrix_.dtype == global_dtype + assert_array_equal(labels, labels_precomputed) cluster_centers_indices = af.cluster_centers_indices_ @@ -100,38 +106,36 @@ def test_affinity_propagation_params_validation(input, params, err_type, err_msg AffinityPropagation(**params).fit(input) -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation_predict(dtype): +def test_affinity_propagation_predict(global_dtype): # Test AffinityPropagation.predict af = AffinityPropagation(affinity="euclidean", random_state=63) - labels = af.fit_predict(X.astype(dtype)) - labels2 = af.predict(X.astype(dtype)) + labels = af.fit_predict(X.astype(global_dtype)) + labels2 = af.predict(X.astype(global_dtype)) assert_array_equal(labels, labels2) -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation_predict_error(dtype): +def test_affinity_propagation_predict_error(global_dtype): # Test exception in AffinityPropagation.predict # Not fitted. - Y = X.astype(dtype) + global X + X = X.astype(global_dtype) af = AffinityPropagation(affinity="euclidean") with pytest.raises(ValueError): - af.predict(Y) + af.predict(X) # Predict not supported when affinity="precomputed". - S = np.dot(Y, Y.T) + S = np.dot(X, X.T) af = AffinityPropagation(affinity="precomputed", random_state=57) af.fit(S) with pytest.raises(ValueError): - af.predict(Y) + af.predict(X) -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation_fit_non_convergence(dtype): +def test_affinity_propagation_fit_non_convergence(global_dtype): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array and training samples should be labelled # as noise (-1) - X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(dtype) + X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(global_dtype) # Force non-convergence by allowing only a single iteration af = AffinityPropagation(preference=-10, max_iter=1, random_state=82) @@ -142,9 +146,8 @@ def test_affinity_propagation_fit_non_convergence(dtype): assert_array_equal(np.array([-1, -1, -1]), af.labels_) -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation_equal_mutual_similarities(dtype): - X = np.array([[-1, 1], [1, -1]]).astype(dtype) +def test_affinity_propagation_equal_mutual_similarities(global_dtype): + X = np.array([[-1, 1], [1, -1]]).astype(global_dtype) S = -euclidean_distances(X, squared=True) # setting preference > similarity @@ -175,11 +178,10 @@ def test_affinity_propagation_equal_mutual_similarities(dtype): assert_array_equal([0, 0], labels) -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation_predict_non_convergence(dtype): +def test_affinity_propagation_predict_non_convergence(global_dtype): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array - X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=dtype) + X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype) # Force non-convergence by allowing only a single iteration with pytest.warns(ConvergenceWarning): @@ -193,10 +195,9 @@ def test_affinity_propagation_predict_non_convergence(dtype): assert_array_equal(np.array([-1, -1, -1]), y) -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation_non_convergence_regressiontest(dtype): +def test_affinity_propagation_non_convergence_regressiontest(global_dtype): X = np.array( - [[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]], dtype=dtype + [[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]], dtype=global_dtype ) af = AffinityPropagation(affinity="euclidean", max_iter=2, random_state=34) msg = ( @@ -209,19 +210,20 @@ def test_affinity_propagation_non_convergence_regressiontest(dtype): assert_array_equal(np.array([0, 0, 0]), af.labels_) -@pytest.mark.parametrize("dtype", DTYPES) -def test_equal_similarities_and_preferences(dtype): +def test_equal_similarities_and_preferences(global_dtype): # Unequal distances - X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(dtype) + X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(global_dtype) S = -euclidean_distances(X, squared=True) + assert S.dtype == global_dtype assert not _equal_similarities_and_preferences(S, np.array(0)) assert not _equal_similarities_and_preferences(S, np.array([0, 0])) assert not _equal_similarities_and_preferences(S, np.array([0, 1])) # Equal distances - X = np.array([[0, 0], [1, 1]]).astype(dtype) + X = np.array([[0, 0], [1, 1]]).astype(global_dtype) S = -euclidean_distances(X, squared=True) + assert S.dtype == global_dtype # Different preferences assert not _equal_similarities_and_preferences(S, np.array([0, 1])) @@ -252,11 +254,10 @@ def test_affinity_propagation_random_state(): @pytest.mark.parametrize("centers", [csr_matrix(np.zeros((1, 10))), np.zeros((1, 10))]) -@pytest.mark.parametrize("dtype", DTYPES) -def test_affinity_propagation_convergence_warning_dense_sparse(centers, dtype): +def test_affinity_propagation_convergence_warning_dense_sparse(centers, global_dtype): """Non-regression, see #13334""" rng = np.random.RandomState(42) - X = rng.rand(40, 10).astype(dtype) + X = rng.rand(40, 10).astype(global_dtype) y = (4 * rng.rand(40)).astype(int) ap = AffinityPropagation(random_state=46) ap.fit(X, y) From 4272be852b69f31593a88942176512220648406c Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Wed, 23 Mar 2022 11:51:34 +0100 Subject: [PATCH 3/9] TST Do not copy on when casting --- .../tests/test_affinity_propagation.py | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index 3bc6655e35713..04510c89d5954 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -29,12 +29,13 @@ random_state=0, ) +# TODO: test dtype perservation for fitted attributes + def test_affinity_propagation(global_dtype): # Affinity Propagation algorithm # Compute similarities - S = -euclidean_distances(X.astype(global_dtype), squared=True) - assert S.dtype == global_dtype + S = -euclidean_distances(X.astype(global_dtype, copy=False), squared=True) preference = np.median(S) * 10 @@ -55,12 +56,6 @@ def test_affinity_propagation(global_dtype): af = AffinityPropagation(preference=preference, verbose=True, random_state=37) labels = af.fit(X).labels_ - # Fitted attribute must have the same dtype - # TODO: preserve dtype for cluster_centers_ - # assert af.cluster_centers_.dtype == global_dtype - # TODO: preserve dtype for affinity_matrix_ - # assert af.affinity_matrix_.dtype == global_dtype - assert_array_equal(labels, labels_precomputed) cluster_centers_indices = af.cluster_centers_indices_ @@ -114,11 +109,9 @@ def test_affinity_propagation_predict(global_dtype): assert_array_equal(labels, labels2) -def test_affinity_propagation_predict_error(global_dtype): +def test_affinity_propagation_predict_error(): # Test exception in AffinityPropagation.predict # Not fitted. - global X - X = X.astype(global_dtype) af = AffinityPropagation(affinity="euclidean") with pytest.raises(ValueError): af.predict(X) @@ -131,11 +124,11 @@ def test_affinity_propagation_predict_error(global_dtype): af.predict(X) -def test_affinity_propagation_fit_non_convergence(global_dtype): +def test_affinity_propagation_fit_non_convergence(): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array and training samples should be labelled # as noise (-1) - X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(global_dtype) + X = np.array([[0, 0], [1, 1], [-2, -2]]) # Force non-convergence by allowing only a single iteration af = AffinityPropagation(preference=-10, max_iter=1, random_state=82) @@ -178,10 +171,10 @@ def test_affinity_propagation_equal_mutual_similarities(global_dtype): assert_array_equal([0, 0], labels) -def test_affinity_propagation_predict_non_convergence(global_dtype): +def test_affinity_propagation_predict_non_convergence(): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array - X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype) + X = np.array([[0, 0], [1, 1], [-2, -2]]) # Force non-convergence by allowing only a single iteration with pytest.warns(ConvergenceWarning): @@ -195,10 +188,8 @@ def test_affinity_propagation_predict_non_convergence(global_dtype): assert_array_equal(np.array([-1, -1, -1]), y) -def test_affinity_propagation_non_convergence_regressiontest(global_dtype): - X = np.array( - [[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]], dtype=global_dtype - ) +def test_affinity_propagation_non_convergence_regressiontest(): + X = np.array([[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]]) af = AffinityPropagation(affinity="euclidean", max_iter=2, random_state=34) msg = ( "Affinity propagation did not converge, this model may return degenerate" @@ -214,16 +205,13 @@ def test_equal_similarities_and_preferences(global_dtype): # Unequal distances X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(global_dtype) S = -euclidean_distances(X, squared=True) - assert S.dtype == global_dtype - assert not _equal_similarities_and_preferences(S, np.array(0)) assert not _equal_similarities_and_preferences(S, np.array([0, 0])) assert not _equal_similarities_and_preferences(S, np.array([0, 1])) # Equal distances X = np.array([[0, 0], [1, 1]]).astype(global_dtype) S = -euclidean_distances(X, squared=True) - assert S.dtype == global_dtype # Different preferences assert not _equal_similarities_and_preferences(S, np.array([0, 1])) From 88780f90aab8a49e8e22a9ca2e83cb0bf56ff192 Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Thu, 24 Mar 2022 14:43:18 +0100 Subject: [PATCH 4/9] Apply review comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jérémie du Boisberranger --- .../tests/test_affinity_propagation.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index 04510c89d5954..833e5f8ac3e2f 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -10,7 +10,10 @@ from scipy.sparse import csr_matrix from sklearn.exceptions import ConvergenceWarning -from sklearn.utils._testing import assert_array_equal +from sklearn.utils._testing import ( + assert_array_equal, + assert_allclose, +) from sklearn.cluster import AffinityPropagation from sklearn.cluster._affinity_propagation import _equal_similarities_and_preferences @@ -36,9 +39,7 @@ def test_affinity_propagation(global_dtype): # Affinity Propagation algorithm # Compute similarities S = -euclidean_distances(X.astype(global_dtype, copy=False), squared=True) - preference = np.median(S) * 10 - # Compute Affinity Propagation cluster_centers_indices, labels = affinity_propagation( S, preference=preference, random_state=39 @@ -104,8 +105,9 @@ def test_affinity_propagation_params_validation(input, params, err_type, err_msg def test_affinity_propagation_predict(global_dtype): # Test AffinityPropagation.predict af = AffinityPropagation(affinity="euclidean", random_state=63) - labels = af.fit_predict(X.astype(global_dtype)) - labels2 = af.predict(X.astype(global_dtype)) + X_ = X.astype(global_dtype, copy=False) + labels = af.fit_predict(X_) + labels2 = af.predict(X_) assert_array_equal(labels, labels2) @@ -124,23 +126,23 @@ def test_affinity_propagation_predict_error(): af.predict(X) -def test_affinity_propagation_fit_non_convergence(): +def test_affinity_propagation_fit_non_convergence(global_dtype): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array and training samples should be labelled # as noise (-1) - X = np.array([[0, 0], [1, 1], [-2, -2]]) + X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype) # Force non-convergence by allowing only a single iteration af = AffinityPropagation(preference=-10, max_iter=1, random_state=82) with pytest.warns(ConvergenceWarning): af.fit(X) - assert_array_equal(np.empty((0, 2)), af.cluster_centers_) + assert_allclose(np.empty((0, 2)), af.cluster_centers_) assert_array_equal(np.array([-1, -1, -1]), af.labels_) def test_affinity_propagation_equal_mutual_similarities(global_dtype): - X = np.array([[-1, 1], [1, -1]]).astype(global_dtype) + X = np.array([[-1, 1], [1, -1]], dtype=global_dtype) S = -euclidean_distances(X, squared=True) # setting preference > similarity @@ -203,14 +205,15 @@ def test_affinity_propagation_non_convergence_regressiontest(): def test_equal_similarities_and_preferences(global_dtype): # Unequal distances - X = np.array([[0, 0], [1, 1], [-2, -2]]).astype(global_dtype) + X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype) S = -euclidean_distances(X, squared=True) + assert not _equal_similarities_and_preferences(S, np.array(0)) assert not _equal_similarities_and_preferences(S, np.array([0, 0])) assert not _equal_similarities_and_preferences(S, np.array([0, 1])) # Equal distances - X = np.array([[0, 0], [1, 1]]).astype(global_dtype) + X = np.array([[0, 0], [1, 1]], dtype=global_dtype) S = -euclidean_distances(X, squared=True) # Different preferences @@ -245,7 +248,7 @@ def test_affinity_propagation_random_state(): def test_affinity_propagation_convergence_warning_dense_sparse(centers, global_dtype): """Non-regression, see #13334""" rng = np.random.RandomState(42) - X = rng.rand(40, 10).astype(global_dtype) + X = rng.rand(40, 10).astype(global_dtype, copy=False) y = (4 * rng.rand(40)).astype(int) ap = AffinityPropagation(random_state=46) ap.fit(X, y) From 3ee457287d03f5ea444bc4d493c51481af375f75 Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Thu, 24 Mar 2022 14:56:05 +0100 Subject: [PATCH 5/9] DOC Improve comment regarding dtype preservation --- sklearn/cluster/tests/test_affinity_propagation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index 833e5f8ac3e2f..ddf720ac20a1b 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -32,7 +32,8 @@ random_state=0, ) -# TODO: test dtype perservation for fitted attributes +# TODO: AffinityPropagation must preserve dtype for its fitted attributes +# and test must be created accordingly to this new behavior. def test_affinity_propagation(global_dtype): From fe5b107f3f28c5b736c5fe24056b5df8a360b1f7 Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Tue, 29 Mar 2022 13:03:35 +0200 Subject: [PATCH 6/9] Apply review comments --- sklearn/cluster/tests/test_affinity_propagation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index ddf720ac20a1b..5ea4197618b04 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -34,6 +34,7 @@ # TODO: AffinityPropagation must preserve dtype for its fitted attributes # and test must be created accordingly to this new behavior. +# For more details, see: https://github.com/scikit-learn/scikit-learn/issues/11000 def test_affinity_propagation(global_dtype): From 6684b79c4d4e3764cdd20b2b7147cac9f81ed7fe Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Tue, 17 May 2022 11:12:32 +0200 Subject: [PATCH 7/9] DOC --- sklearn/cluster/tests/test_affinity_propagation.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index 5ea4197618b04..6daa8b05f6ad8 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -248,7 +248,11 @@ def test_affinity_propagation_random_state(): @pytest.mark.parametrize("centers", [csr_matrix(np.zeros((1, 10))), np.zeros((1, 10))]) def test_affinity_propagation_convergence_warning_dense_sparse(centers, global_dtype): - """Non-regression, see #13334""" + """ + Check that having sparse or dense `centers` format should not + influence the convergence. + Non-regression test for gh-13334. + """ rng = np.random.RandomState(42) X = rng.rand(40, 10).astype(global_dtype, copy=False) y = (4 * rng.rand(40)).astype(int) From 30b8a3ee56f5940136671777aaa5f48c18bec37e Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Fri, 10 Jun 2022 14:11:45 +0200 Subject: [PATCH 8/9] TST Review comments Co-authored-by: Guillaume Lemaitre --- sklearn/cluster/tests/test_affinity_propagation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index 6daa8b05f6ad8..987d697951ba0 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -264,11 +264,11 @@ def test_affinity_propagation_convergence_warning_dense_sparse(centers, global_d assert_array_equal(ap.predict(X), np.zeros(X.shape[0], dtype=int)) -def test_affinity_propagation_float32(): +def test_correct_clusters(global_dtype): # Test to fix incorrect clusters due to dtype change # (non-regression test for issue #10832) X = np.array( - [[1, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1]], dtype="float32" + [[1, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1]], dtype=global_dtype ) afp = AffinityPropagation(preference=1, affinity="precomputed", random_state=0).fit( X From 0ea2b5e8b6ad9298136c4f424de479079392a0de Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Thu, 3 Nov 2022 14:55:49 +0100 Subject: [PATCH 9/9] TST adding global_dtype to 2 addtional tests --- sklearn/cluster/tests/test_affinity_propagation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sklearn/cluster/tests/test_affinity_propagation.py b/sklearn/cluster/tests/test_affinity_propagation.py index a08f30f2a74b7..cc696620a0e4d 100644 --- a/sklearn/cluster/tests/test_affinity_propagation.py +++ b/sklearn/cluster/tests/test_affinity_propagation.py @@ -183,10 +183,10 @@ def test_affinity_propagation_equal_mutual_similarities(global_dtype): assert_array_equal([0, 0], labels) -def test_affinity_propagation_predict_non_convergence(): +def test_affinity_propagation_predict_non_convergence(global_dtype): # In case of non-convergence of affinity_propagation(), the cluster # centers should be an empty array - X = np.array([[0, 0], [1, 1], [-2, -2]]) + X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype) # Force non-convergence by allowing only a single iteration with pytest.warns(ConvergenceWarning): @@ -200,8 +200,10 @@ def test_affinity_propagation_predict_non_convergence(): assert_array_equal(np.array([-1, -1, -1]), y) -def test_affinity_propagation_non_convergence_regressiontest(): - X = np.array([[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]]) +def test_affinity_propagation_non_convergence_regressiontest(global_dtype): + X = np.array( + [[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]], dtype=global_dtype + ) af = AffinityPropagation(affinity="euclidean", max_iter=2, random_state=34) msg = ( "Affinity propagation did not converge, this model may return degenerate"