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
'인공지능 > Machine Learning' 카테고리의 다른 글
[ML] Split-Validation / Machine Learning Model Design (0) | 2022.10.17 |
---|---|
[ML] K-means clustering 실습 예제 2 (0) | 2022.10.14 |
[ML] PCA 실습 예제 (0) | 2022.10.14 |
Principal Component Analysis (PCA, 주성분분석) (0) | 2022.10.14 |
[ML] k-Means Clustering / Scree plot (0) | 2022.10.14 |
댓글