80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
"""
|
|
Testing for Clustering methods
|
|
|
|
"""
|
|
|
|
import numpy as np
|
|
|
|
from sklearn.utils.testing import assert_equal
|
|
from sklearn.utils.testing import assert_array_equal
|
|
from sklearn.utils.testing import assert_raises
|
|
|
|
from sklearn.cluster.affinity_propagation_ import AffinityPropagation
|
|
from sklearn.cluster.affinity_propagation_ import affinity_propagation
|
|
from sklearn.datasets.samples_generator import make_blobs
|
|
from sklearn.metrics import euclidean_distances
|
|
|
|
n_clusters = 3
|
|
centers = np.array([[1, 1], [-1, -1], [1, -1]]) + 10
|
|
X, _ = make_blobs(n_samples=60, n_features=2, centers=centers,
|
|
cluster_std=0.4, shuffle=True, random_state=0)
|
|
|
|
|
|
def test_affinity_propagation():
|
|
# Affinity Propagation algorithm
|
|
# Compute similarities
|
|
S = -euclidean_distances(X, squared=True)
|
|
preference = np.median(S) * 10
|
|
# Compute Affinity Propagation
|
|
cluster_centers_indices, labels = affinity_propagation(
|
|
S, preference=preference)
|
|
|
|
n_clusters_ = len(cluster_centers_indices)
|
|
|
|
assert_equal(n_clusters, n_clusters_)
|
|
|
|
af = AffinityPropagation(preference=preference, affinity="precomputed")
|
|
labels_precomputed = af.fit(S).labels_
|
|
|
|
af = AffinityPropagation(preference=preference, verbose=True)
|
|
labels = af.fit(X).labels_
|
|
|
|
assert_array_equal(labels, labels_precomputed)
|
|
|
|
cluster_centers_indices = af.cluster_centers_indices_
|
|
|
|
n_clusters_ = len(cluster_centers_indices)
|
|
assert_equal(np.unique(labels).size, n_clusters_)
|
|
assert_equal(n_clusters, n_clusters_)
|
|
|
|
# Test also with no copy
|
|
_, labels_no_copy = affinity_propagation(S, preference=preference,
|
|
copy=False)
|
|
assert_array_equal(labels, labels_no_copy)
|
|
|
|
# Test input validation
|
|
assert_raises(ValueError, affinity_propagation, S[:, :-1])
|
|
assert_raises(ValueError, affinity_propagation, S, damping=0)
|
|
af = AffinityPropagation(affinity="unknown")
|
|
assert_raises(ValueError, af.fit, X)
|
|
|
|
|
|
def test_affinity_propagation_predict():
|
|
# Test AffinityPropagation.predict
|
|
af = AffinityPropagation(affinity="euclidean")
|
|
labels = af.fit_predict(X)
|
|
labels2 = af.predict(X)
|
|
assert_array_equal(labels, labels2)
|
|
|
|
|
|
def test_affinity_propagation_predict_error():
|
|
# Test exception in AffinityPropagation.predict
|
|
# Not fitted.
|
|
af = AffinityPropagation(affinity="euclidean")
|
|
assert_raises(ValueError, af.predict, X)
|
|
|
|
# Predict not supported when affinity="precomputed".
|
|
S = np.dot(X, X.T)
|
|
af = AffinityPropagation(affinity="precomputed")
|
|
af.fit(S)
|
|
assert_raises(ValueError, af.predict, X)
|