Skip to content

Instantly share code, notes, and snippets.

@rkdgusrn1212
Created June 8, 2022 01:06
Show Gist options
  • Save rkdgusrn1212/c4a007fa06f99efb4b9c03e48b06c337 to your computer and use it in GitHub Desktop.
Save rkdgusrn1212/c4a007fa06f99efb4b9c03e48b06c337 to your computer and use it in GitHub Desktop.
k-means clustering
from sklearn.cluster import KMeans
#features 컬럼들을 평균 0, 표준편차 1로 표준화함.
X_scaled = X.loc[:, features]#[:]전채 행에서 features열만 가지고 군집화함.
X_scaled = (X_scaled - X_scaled.mean(axis=0)) / X_scaled.std(axis=0)#각 열에서의 행들의 평균, 행들의 표준편차
#n_cluster는 군집 개수, n_init은 다른 랜덤 centeroid를 가지고 알고리즘을 수행될 횟수로 그 중 가장 군집화가 잘된 결과를 반환한다.
kmeans = KMeans(n_clusters=10, n_init=10)
X["Cluster"] = kmeans.fit_predict(X_scaled)#군집화 정보를 다시 학습 데이터에 넣어준다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment