I am new to python and the optimal number of clustering. Right now I have a task to analyze two set of data and determine its optimal Kmean by using elbow and silhouette method.
X represents my original data before normalized.
I use elbow method to see the wcss value at different k values and the silhouette method to look at the silhouette score
from sklearn import preprocessing
from sklearn.metrics import silhouette_score
# normalize the data attributes
normalized = preprocessing.normalize(X)
#print("Normalized Data = ", normalized)
Sum_of_squared_distances = []
K = range(2,15)
for k in K:
km = KMeans(n_clusters=k)
km = km.fit(normalized)
Sum_of_squared_distances.append(km.inertia_)
plt.plot(K, Sum_of_squared_distances, 'bx-')
plt.xlabel('Number of clusters')
plt.ylabel('Sum_of_squared_distances')
plt.title('Elbow Method For Optimal k')
plt.show()
sil = []
for k in range(2, 15):
kmeans = KMeans(n_clusters = k).fit(normalized)
preds = kmeans.fit_predict(normalized)
sil.append(silhouette_score(normalized, preds, metric = 'euclidean'))
plt.plot(range(2, 15), sil, 'bx-')
plt.title('Silhouette Method For Optimal k')
plt.xlabel('Number of clusters')
plt.ylabel('Sil')
plt.show()
for i in range(len(sil)):
print(str(i+2) +":"+ str(sil[i]))
Could anybody suggest how can I pick the optimal Kmean? because from my understanding
