Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn.cluster import KMeans
- k = 2 # Define the number of clusters in which we want to partion the data
- kmeans = KMeans(n_clusters = k) # Run the algorithm kmeans
- kmeans.fit(X);
- centroids = kmeans.cluster_centers_ # Get centroid's coordinates for each cluster
- labels = kmeans.labels_ # Get labels assigned to each data
- k = 2 # Define the number of clusters in which we want to partion the data
- kmeans = KMeans(n_clusters = k) # Run the algorithm kmeans
- kmeans.fit(X);
- centroids = kmeans.cluster_centers_ # Get centroid's coordinates for each cluster
- labels = kmeans.labels_ # Get labels assigned to each data
- colors = ['r.', 'g.'] # Define two colors for the plot below
- plt.figure()
- for i in range(len(X)):
- plt.plot(X[i,0], X[i,1], colors[labels[i]], markersize = 30)
- plt.scatter(centroids[:,0],centroids[:,1], marker = "x", s = 300, linewidths = 5) # Plot centroids
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement