def test_affinity_propagation(): """ Affinity Propagation algorithm """ #---------------------------------------------------------------------- # Compute similarities #---------------------------------------------------------------------- X_norms = np.sum(X*X, axis=1) S = - X_norms[:,np.newaxis] - X_norms[np.newaxis,:] + 2 * np.dot(X, X.T) p = 10*np.median(S) #---------------------------------------------------------------------- # Compute Affinity Propagation #---------------------------------------------------------------------- cluster_centers_indices, labels = affinity_propagation(S, p) n_clusters_ = len(cluster_centers_indices) assert_equal(n_clusters, n_clusters_) af = AffinityPropagation() labels = af.fit(S, p).labels cluster_centers_indices = af.fit(S, p).cluster_centers_indices n_clusters_ = len(cluster_centers_indices) assert_equal(np.unique(labels).size, n_clusters_) assert_equal(n_clusters, n_clusters_)
'HD' : 'Home Depot', 'GSK' : 'GlaxoSmithKline', 'PFE' : 'Pfizer', 'SNY' : 'Sanofi-Aventis', 'NVS' : 'Novartis', 'KMB' : 'Kimberly-Clark', 'R' : 'Ryder', 'GD' : 'General Dynamics', 'RTN' : 'Raytheon', 'CVS' : 'CVS', 'CAT' : 'Caterpillar', 'DD' : 'DuPont de Nemours', } symbols, names = np.array(symbol_dict.items()).T quotes = [finance.quotes_historical_yahoo(symbol, d1, d2, asobject=True) for symbol in symbols] #volumes = np.array([q.volume for q in quotes]).astype(np.float) open = np.array([q.open for q in quotes]).astype(np.float) close = np.array([q.close for q in quotes]).astype(np.float) variation = close - open correlations = np.corrcoef(variation) _, labels = clustering.affinity_propagation(correlations) for i in range(labels.max()+1): print 'Cluster %i: %s' % ((i+1), ', '.join(names[labels==i]))
X = np.empty((0, 2)) for i in range(n_clusters): X = np.r_[X, means[i] + std * np.random.randn(n_points_per_cluster, 2)] ################################################################################ # Compute similarities ################################################################################ X_norms = np.sum(X*X, axis=1) S = - X_norms[:,np.newaxis] - X_norms[np.newaxis,:] + 2 * np.dot(X, X.T) p = 10*np.median(S) ################################################################################ # Compute Affinity Propagation ################################################################################ cluster_centers_indices, labels = affinity_propagation(S, p) n_clusters_ = len(cluster_centers_indices) print 'Estimated number of clusters: %d' % n_clusters_ ################################################################################ # Plot result ################################################################################ import pylab as pl from itertools import cycle pl.close('all') pl.figure(1) pl.clf()