I have written a function to show elbow to select the optimal value of K of Kmeans.
from sklearn.cluster import KMeans
def show_elbow(df):
distance_list=[]
K = range(1,9)
for k in K:
kmeams=KMeans(n_cluster=k,max_iter=100)
kmeans=KMeans.fit(df)
distance_list.append(kmeans.inertia_)
plt.plot(K,distance_list,'bx-')
When I use this function, df[''] causes error while df[['']] doesn’t. I don’t know the reason.