from scipy.spatial.distance import cdist
distortions = []
K = range(1,10)
for k in K:
kmeanModel = KMeans(n_clusters=k).fit(X_train)
distortions.append(sum(np.min(cdist(X_train, kmeanModel.cluster_centers_, 'euclidean'), axis=1)) / X_train.shape[0])
# Plot the elbow
plt.plot(K, distortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Distortion')
plt.title('The Elbow Method showing the optimal k')
plt.show()
'개발 > ML+ Data Science' 카테고리의 다른 글
Missing data 개수,비율,dtype 출력하는 template (0) | 2019.10.01 |
---|---|
notebook image 불러와서 표시하기 (0) | 2019.09.29 |
ROC curve 그리기 (0) | 2019.09.28 |
sklearn metric 설명들 (0) | 2019.09.28 |
Kmeans: '(slice(None, None, None), slice(None, None, None))' is an invalid key (0) | 2019.09.28 |