def _meanshift(dataset, *bandwidths): if not bandwidths: bandwidths = [0.1, 0.3, 0.5, 0.7, 0.9] instances = [] for bw in bandwidths: instances.append(MeanShift(estimate_bandwidth(dataset, bw))) return instances
"""-------Select pcar1 as the principal component------- """ x_pc=pcar1 ######################################################################## """-------Implement KMeans------- """ (res1,idx1,plot_id1)=calc_kmeans.getplotid(z,2) (res2,idx2,plot_id2)=calc_kmeans.getplotid(z,3) (res3,idx3,plot_id3)=calc_kmeans.getplotid(z,4) ######################################################################## """------- Implement Mean Shift Clustering Algorithm: ------- """ bw=estimate_bandwidth(z,quantile=0.3) xms=MeanShift(bandwidth=bw) xms.fit(z) labs=xms.labels_ centr=xms.cluster_centers_# Find the Cluster centers labs_uniq=np.unique(labs) # Number of unique labels nclusts=len(labs_uniq) # Number of Clusters print 'Implementing Mean-Shift Clustering' ######################################################################## plotdat.Plotdata(z,z1,1) # --- Plots the given data plotall=analysis_plt.PlotAll(2) #-- Plots all analysis plotall.plt_dat(x,y,z,xg,xp,con,rcon,nclusts,labs,centr,idx1,idx2,idx3,res1,res2,res3,plot_id1,plot_id2,plot_id3) sltn.giv_ans(3,x,y,z,z_h,plot_id2,res2,nclusts,labs,centr)# plots final results
np.random.seed(0) n_points_per_cluster = 250 n_clusters = 3 n_points = n_points_per_cluster*n_clusters means = np.array([[1,1],[-1,-1],[1,-1]]) std = .6 clustMed = [] 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 clustering with MeanShift bandwidth = estimate_bandwidth(X, quantile=0.3) ms = MeanShift(bandwidth=bandwidth) ms.fit(X) labels = ms.labels_ cluster_centers = ms.cluster_centers_ labels_unique = np.unique(labels) n_clusters_ = len(labels_unique) print "number of estimated clusters : %d" % n_clusters_ ################################################################################ # Plot result import pylab as pl from itertools import cycle
np.random.seed(0) n_points_per_cluster = 250 n_clusters = 3 n_points = n_points_per_cluster * n_clusters means = np.array([[1, 1], [-1, -1], [1, -1]]) std = .6 clustMed = [] 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 clustering with MeanShift bandwidth = estimate_bandwidth(X, quantile=0.3) ms = MeanShift(bandwidth=bandwidth) ms.fit(X) labels = ms.labels_ cluster_centers = ms.cluster_centers_ labels_unique = np.unique(labels) n_clusters_ = len(labels_unique) print "number of estimated clusters : %d" % n_clusters_ ################################################################################ # Plot result import pylab as pl from itertools import cycle