Advertisement
897bhgy

Untitled

Jul 19th, 2023
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | Source Code | 0 0
  1. from sklearn.datasets import make_blobs
  2. from sklearn.cluster import KMeans
  3. import matplotlib.pyplot as plt
  4.  
  5. # Let's generate synthetic data
  6. X, y = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)
  7.  
  8. distortions = []
  9. K = range(1,10)  # Change range according to your needs
  10. for k in K:
  11.     kmeanModel = KMeans(n_clusters=k)
  12.     kmeanModel.fit(X)
  13.     distortions.append(kmeanModel.inertia_)
  14.  
  15. plt.figure(figsize=(16,8))
  16. plt.plot(K, distortions, 'bx-')
  17. plt.xlabel('k')
  18. plt.ylabel('Distortion')
  19. plt.title('The Elbow Method showing the optimal k')
  20. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement