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) 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)
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)
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]]) # Force non-convergence by allowing only a single iteration af = AffinityPropagation(preference=-10, max_iter=1).fit(X) # At prediction time, consider new samples as noise since there are no # clusters assert_array_equal(np.array([-1, -1, -1]), af.predict(np.array([[2, 2], [3, 3], [4, 4]])))
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]]) # Force non-convergence by allowing only a single iteration af = AffinityPropagation(preference=-10, max_iter=1).fit(X) # At prediction time, consider new samples as noise since there are no # clusters assert_array_equal(np.array([-1, -1, -1]), af.predict(np.array([[2, 2], [3, 3], [4, 4]])))
def test_affinity_propagation_convergence_warning_dense_sparse(centers): """Non-regression, see #13334""" rng = np.random.RandomState(42) X = rng.rand(40, 10) y = (4 * rng.rand(40)).astype(np.int) ap = AffinityPropagation() ap.fit(X, y) ap.cluster_centers_ = centers with pytest.warns(None) as record: assert_array_equal(ap.predict(X), np.zeros(X.shape[0], dtype=int)) assert len(record) == 0
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]]) # Force non-convergence by allowing only a single iteration af = AffinityPropagation(preference=-10, max_iter=1) assert_warns(ConvergenceWarning, af.fit, X) assert_array_equal(np.empty((0, 2)), af.cluster_centers_) assert_array_equal(np.array([-1, -1, -1]), af.labels_)
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) 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)
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) af_2 = AffinityPropagation(affinity='precomputed') assert_raises(TypeError, af_2.fit, csr_matrix((3, 3)))
def getCentersOfUser(locations): size = len(locations) distance_matrix = numpy.zeros((size, size)) for x in range(0, size): for y in range(x + 1, size): pointA = locations[x] pointB = locations[y] distance_matrix[x][y] = getDistance(pointA.latitude, pointA.longitude, pointB.latitude, pointB.longitude) db = AffinityPropagation(affinity='precomputed').fit(distance_matrix) print(db.cluster_centers_indices_) return db.cluster_centers_indices_
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) af_2 = AffinityPropagation(affinity='precomputed') assert_raises(TypeError, af_2.fit, csr_matrix((3, 3)))
def test_affinity_propagation_convergence_warning_dense_sparse(centers): """Non-regression, see #13334""" rng = np.random.RandomState(42) X = rng.rand(40, 10) y = (4 * rng.rand(40)).astype(np.int) ap = AffinityPropagation() ap.fit(X, y) ap.cluster_centers_ = centers with pytest.warns(None) as record: assert_array_equal(ap.predict(X), np.zeros(X.shape[0], dtype=int)) assert len(record) == 0
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)
def clustering(model): from sklearn.cluster.affinity_propagation_ import AffinityPropagation tsne_plot(model) X = model.docvecs.doctag_syn0 from sklearn.decomposition import PCA pca = PCA(n_components=2) pca_result = pca.fit_transform(X) print('Explained variation per principal component: {}'.format( pca.explained_variance_ratio_)) print("fitt clusters") af = AffinityPropagation(preference=-500).fit(pca_result[:1000]) cluster_centers_indices = af.cluster_centers_indices_ n_clusters_ = len(cluster_centers_indices) print("af clusters: ", n_clusters_) plot(pca_result[:1000])
def test_affinity_propagation_convergence_warning(): # Test warning in AffinityPropagation.fit af = AffinityPropagation(affinity="euclidean", max_iter=1) assert_warns(ConvergenceWarning, af.fit, X) assert_raises(ValueError, af.predict, 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 clusterUsingAffinityPropagation(graph,**kwargs): from sklearn.cluster.affinity_propagation_ import AffinityPropagation S = nx.to_numpy_matrix(graph, weight='w') af = AffinityPropagation(**kwargs).fit(S) return (len(af.cluster_centers_indices_), zip(graph.nodes(), af.labels_))
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)
from sklearn.manifold.spectral_embedding_ import SpectralEmbedding from sklearn.preprocessing.data import StandardScaler from sklearn.manifold.t_sne import TSNE from sklearn.linear_model.theil_sen import TheilSenRegressor from sklearn.mixture.dpgmm import VBGMM from sklearn.feature_selection.variance_threshold import VarianceThreshold import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) clf_dict = {'ARDRegression':ARDRegression(), 'AdaBoostClassifier':AdaBoostClassifier(), 'AdaBoostRegressor':AdaBoostRegressor(), 'AdditiveChi2Sampler':AdditiveChi2Sampler(), 'AffinityPropagation':AffinityPropagation(), 'AgglomerativeClustering':AgglomerativeClustering(), 'BaggingClassifier':BaggingClassifier(), 'BaggingRegressor':BaggingRegressor(), 'BayesianGaussianMixture':BayesianGaussianMixture(), 'BayesianRidge':BayesianRidge(), 'BernoulliNB':BernoulliNB(), 'BernoulliRBM':BernoulliRBM(), 'Binarizer':Binarizer(), 'Birch':Birch(), 'CCA':CCA(), 'CalibratedClassifierCV':CalibratedClassifierCV(), 'DBSCAN':DBSCAN(), 'DPGMM':DPGMM(), 'DecisionTreeClassifier':DecisionTreeClassifier(), 'DecisionTreeRegressor':DecisionTreeRegressor(),