본문 바로가기
인공지능/Machine Learning

[ML] K-means clustering 실습 예제

by 유일리 2022. 10. 14.
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
%matplotlib inline

X,y = make_blobs(n_samples=300, n_features=2, centers=4, cluster_std=4, random_state=10)

plt.figure(figsize=(7,5))
plt.scatter(X[:,0],X[:,1])

model = KMeans(n_clusters=4)
model.fit(X)

model_predict = model.predict(X)
centroids = model.cluster_centers_
print(model.cluster_centers_)

plt.figure(figsize=(7,5))
plt.scatter(X[:,0],X[:,1], c=model_predict, s=50, cmap='rainbow')
plt.scatter(centroids[:,0], centroids[:,1], c='black',s=150, alpha=0.8)

#Using a for loop, iterate the values of k with a range of 1-10 and find the values of distortion for each k value.

distortions = [] 
K = range(1,10) 
for k in K: 
    model = KMeans(n_clusters=k)
    model.fit(X, y)
    distortions.append(model.inertia_)
    
#Generate plot with k on the x-axis and distortions on the y_axis using matplotlib 
plt.figure(figsize=(16,8))
plt.plot(K, distortions)
plt.xlabel('k')
plt.ylabel('Distortion')
plt.show()

 

https://github.com/erica00j/machinelearning/blob/main/k_clustering.ipynb

 

GitHub - erica00j/machinelearning

Contribute to erica00j/machinelearning development by creating an account on GitHub.

github.com

 

댓글