Skip to content

Instantly share code, notes, and snippets.

@nithyadurai87
Last active April 1, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nithyadurai87/10b5b273151c80be97579d684279cd84 to your computer and use it in GitHub Desktop.
Save nithyadurai87/10b5b273151c80be97579d684279cd84 to your computer and use it in GitHub Desktop.
from sklearn.cluster import KMeans
from sklearn import metrics
from scipy.spatial.distance import cdist
import numpy as np
import matplotlib.pyplot as plt
x1 = [15, 19, 15, 5, 13, 17, 15, 12, 8, 6, 9, 13]
x2 = [13, 16, 17, 6, 17, 14, 15, 13, 7, 6, 10, 12]
X = np.array(list(zip(x1, x2)))
distortions = []
K = range(1,8)
for i in K:
model = KMeans(n_clusters=i)
model.fit(X)
distortions.append(sum(np.min(cdist(X, model.cluster_centers_, 'euclidean'), axis=1)) / X.shape[0])
plt.plot()
plt.plot(K, distortions, 'bx-')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment