Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn.datasets import make_blobs
- from sklearn.cluster import KMeans
- import matplotlib.pyplot as plt
- # Let's generate synthetic data
- X, y = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)
- distortions = []
- K = range(1,10) # Change range according to your needs
- for k in K:
- kmeanModel = KMeans(n_clusters=k)
- kmeanModel.fit(X)
- distortions.append(kmeanModel.inertia_)
- plt.figure(figsize=(16,8))
- plt.plot(K, distortions, 'bx-')
- plt.xlabel('k')
- plt.ylabel('Distortion')
- plt.title('The Elbow Method showing the optimal k')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement