Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

잡동사니 블로그

Kmedoids clustering 본문

공부용

Kmedoids clustering

코딩부대찌개 2024. 10. 16. 21:53

 

K-medoids의 목표는 각 데이터 포인트가 가장 가까운 대표점(medoids)에 할당되었을 때, 그 거리를 최소화하는 것이며, 일반적으로는 유클리드 거리(Euclidean distance)기반 그 합을 최소화 하는것이 목표.(Cosine similarity도 가능)

 

  • K-means: 군집의 중심을 각 군집에 속한 데이터 포인트의 평균(mean)으로 정의함. 즉, 군집의 중심은 데이터 공간의 임의의 점이 됨.
  • K-medoids: 군집의 중심을 데이터 포인트 중 하나로 선택 되기 때문에 그 중심은 반드시 실제로 존재하는 데이터이며, 이를 통해 K-medoids는 K-means보다 이상치(outlier)에 덜 민감해짐.

 

https://scikit-learn-extra.readthedocs.io/en/stable/auto_examples/cluster/plot_kmedoids_digits.html#sphx-glr-auto-examples-cluster-plot-kmedoids-digits-py

 

 

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn_extra.cluster import KMedoids
X = np.random.rand(10, 2)
# K-means 
kmeans = KMeans(n_clusters=1, random_state=0)
kmeans_labels = kmeans.fit_predict(X)
# K-medoids 
kmedoids = KMedoids(n_clusters=1, random_state=0)
kmedoids_labels = kmedoids.fit_predict(X)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
ax1.scatter(X[:, 0], X[:, 1], c=kmeans_labels, cmap='viridis', marker='o', edgecolor='k', s=50)
ax1.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, c='red', marker='x')
ax1.set_title('K-means Clustering')
ax2.scatter(X[:, 0], X[:, 1], c=kmedoids_labels, cmap='viridis', marker='o', edgecolor='k', s=50)
ax2.scatter(X[kmedoids.medoid_indices_, 0], X[kmedoids.medoid_indices_, 1], s=200, c='red', marker='x')
ax2.set_title('K-medoids Clustering')
plt.show()