Python 中的叢集分析
Shaumik Daityari
Business Analyst
kmeans(obs, k_or_guess, iter, thresh, check_finite)
obs: 標準化後的觀測值k_or_guess: 叢集數iter: 迭代次數(預設:20)thres: 閾值(預設:1e-05)check_finite: 是否檢查觀測值是否全為有限數(預設:True)回傳兩個物件:叢集中心、distortion

vq(obs, code_book, check_finite=True)
obs: 標準化後的觀測值code_book: 叢集中心check_finite: 是否檢查觀測值是否全為有限數(預設:True)回傳兩個物件:叢集標籤清單、distortion 清單
kmeans 回傳單一的 distortion 值vq 回傳一組 distortion 清單。# Import kmeans and vq functions
from scipy.cluster.vq import kmeans, vq
# Generate cluster centers and labels
cluster_centers, _ = kmeans(df[['scaled_x', 'scaled_y']], 3)
df['cluster_labels'], _ = vq(df[['scaled_x', 'scaled_y']], cluster_centers)
# Plot clusters
sns.scatterplot(x='scaled_x', y='scaled_y', hue='cluster_labels', data=df)
plt.show()

Python 中的叢集分析