fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn import datasets
  4. from sklearn.cluster import KMeans
  5.  
  6. # Load the Iris dataset
  7. iris = datasets.load_iris()
  8. X = iris.data[:, :2] # we will only use the first two features for visualization purposes
  9.  
  10. # Implementing K-means clustering algorithm
  11. kmeans = KMeans(n_clusters=3, random_state=42)
  12. kmeans.fit(X)
  13. centroids = kmeans.cluster_centers_
  14. labels = kmeans.labels_
  15.  
  16. # Visualizing the clusters
  17. plt.figure(figsize=(8, 6))
  18.  
  19. # Plotting data points
  20. plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=50, alpha=0.7, edgecolors='k', label='Data Points')
  21.  
  22. # Plotting centroids
  23. plt.scatter(centroids[:, 0], centroids[:, 1], marker='*', s=200, c='red', label='Centroids')
  24.  
  25. plt.xlabel('Sepal Length')
  26. plt.ylabel('Sepal Width')
  27. plt.title('K-means Clustering')
  28. plt.legend()
  29. plt.grid(True)
  30. plt.show()
  31. # your code goes here
Success #stdin #stdout #stderr 0.26s 40648KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "import numpy"
Execution halted