Skip to content

Instantly share code, notes, and snippets.

@akiniwa
Last active November 21, 2016 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akiniwa/ba95bd888a78ee7b1f77 to your computer and use it in GitHub Desktop.
Save akiniwa/ba95bd888a78ee7b1f77 to your computer and use it in GitHub Desktop.
# http://stackoverflow.com/questions/6645895/calculating-the-percentage-of-variance-measure-for-k-means
cents = [km.kmeans.cluster_centers_ for km in kms]
D_k = [cdist(rid_brand_pca, cent, 'euclidean') for cent in cents]
# 最も近い中心との距離
dist = [np.min(D,axis=1) for D in D_k]
avgWithinSS = [sum(d)/rid_brand_pca.shape[0] for d in dist]
# elbow curve
K = range(1, 16)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(K, avgWithinSS, 'b*-')
plt.grid(True)
plt.xlabel('Number of clusters')
plt.ylim((1.6, 2.0))
plt.ylabel('Average within-cluster sum of squares')
plt.title('Elbow for KMeans clustering')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment