클러스터는 데이터를 유사한 특성을 가진 그룹으로 나누는 것
각 클러스터 안의 데이터는 서로 비슷하며, 다른 클러스터와는 차이가 나도록 설계
클러스터링은 비지도 학습(Unsupervised Learning) 알고리즘 중 하나로
데이터의 숨겨진 구조를 이해하거나 분류 작업에 도움을 줄 때 사용
# 기본 라이브러리 불러오기
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans
%matplotlib inline
# 보증금(만원),월세(만원) 단위 맞추기
train['보증금(만원)'] = train['보증금'] / 10000
train['월세(만원)'] = train['월세'] / 10000
# 보증금(만원), 월세(만원)만 새로운 데이터 만들기
security_monthly_rent = train[['보증금(만원)','월세(만원)']]
# 군집화 모델 설정 및 학습
kmeans = KMeans(n_clusters = 5) # 군집화 개수 5로 설정(임의의 값)
kmeans.fit(security_monthly_rent)
# 클러스터 레이블 추가
security_monthly_rent['cluster'] = kmeans.labels_ # 데이터프레임에 군집화 컬럼 추가
시각화
plt.scatter(security_monthly_rent['보증금(만원)'], security_monthly_rent['월세(만원)'], c= security_monthly_rent['cluster'], cmap='viridis', s= 100)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], c='red',marker='X', s=200,
label = '무게중심')
plt.title('보증금과 월세의 KMeans Clustering')
plt.xlabel('보증금(만원)')
plt.ylabel('월세(만원)')
plt.legend()
plt.show()
None
이건 블루베리 생크림 케이크 같다. 누가 생크림 왕창 발라놓은 듯한 케이크 너낌..
아니 근데 어쩜 아무리 인라인으로 만들었다 해도 이렇게까지 상관관계가 안보이게 나올 수 있지..?!?!?
뭔가 곰팡이 핀 케이크가 되어버렸다.. 이게 맞나..?
K-Means는 클러스터링을 수행하는 대표적인 알고리즘
데이터를 K개의 클러스터로 나누고 각 클러스터 중심(centroid)과 데이터 간의 거리를 최소화하는 방식으로 작동
1. K 개의 클러스터 중심 초기화
n_clusters
: 클러스터 개수 init
: 초기 중심값 설정 방법 (e.g., 'k-means++' 또는 'random')n_init
: 초기 중심값 선택 반복 횟수 (기본값은 10)max_iter
: 최대 반복 횟수random_state
: 결과 재현성을 위해 랜덤 시드 설정오늘치 끝!