0% found this document useful (0 votes)
30 views7 pages

Week 8. K-Means

The document describes the K-means clustering algorithm. It provides Python code to generate sample data and perform K-means clustering with different values of K (2, 4, 8, 16) on the data. For each K value, it prints the centroid coordinates calculated by the K-means model.

Uploaded by

revaldianggara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Week 8. K-Means

The document describes the K-means clustering algorithm. It provides Python code to generate sample data and perform K-means clustering with different values of K (2, 4, 8, 16) on the data. For each K value, it prints the centroid coordinates calculated by the K-means model.

Uploaded by

revaldianggara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

K-Means

Jasman Pardede
K-means python
• from pandas import DataFrame
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

def main():
print("Contoh program K-Means dengan python")

Data = {'x': [25,34,22,27,33,33,31,22,35,34,67,54,57,43,50,57,59,52,65,47,49,48,35,33,44,45,38,43,51,46],


'y': [79,51,53,78,59,74,73,57,69,75,51,32,40,47,53,36,35,58,59,50,25,20,14,12,20,5,29,27,8,7]
}

df = DataFrame(Data, columns=['x', 'y'])


print("Data frame adalah : ")
print(df)
"""fit data """

kmeans = KMeans(n_clusters=2).fit(df)

centroids = kmeans.cluster_centers_
print(centroids)
plt.scatter(df['x'], df['y'], c=kmeans.labels_.astype(float), s=50, alpha=0.5)

• [[38.75
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
61.625 ]
if __name__ == "__main__":
main() [47.07142857 22.14285714]]
• import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np

Plot K-
from sklearn.datasets.samples_generator import make_blobs
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist

means
def plot_kmeans(kmeans, X, n_clusters=4, rseed=0, ax=None):
labels = kmeans.fit_predict(X)

# plot the input data


ax = ax or plt.gca()
ax.axis('equal')
ax.scatter(X[:, 0], X[:, 1], c=labels, s=40, cmap='viridis', zorder=2)

# plot the representation of the KMeans model


centers = kmeans.cluster_centers_
radii = [cdist(X[labels == i], [center]).max()
for i, center in enumerate(centers)]
for c, r in zip(centers, radii):
ax.add_patch(plt.Circle(c, r, fc='#CCCCCC', lw=3, alpha=0.5, zorder=1))

def main():

X, y_true = make_blobs(n_samples=400, centers=4,


cluster_std=0.60, random_state=0)
X = X[:, ::-1] # flip axes for better plotting

rng = np.random.RandomState(13)
X_stretched = np.dot(X, rng.randn(2, 2))

print(X)
kmeans = KMeans(2, random_state=0)
labels = kmeans.fit(X).predict(X)
centroids = kmeans.cluster_centers_
print("Centroid")
print(centroids)
#plt.scatter(X[:, 0], X[:, 1], c=labels, s=40, cmap='viridis')
#plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
#plot_kmeans(kmeans, X)
plot_kmeans(kmeans, X_stretched)
plt.show()

if __name__ == "__main__":
main()
K-means (K=2)

• Centroid[[ 7.71129761 -1.24188271]


[ 2.66686538 0.43326118]]
K-means
(K=4)

• Centroid[[ 2.84849883 -1.61366997]


[ 7.75608144 -1.2689694 ]
[ 0.83945671 1.95662677]
[ 4.36874542 0.95041055]]
K-means
(K=8)

• Centroid[[ 0.88093864 1.58576742]


[ 4.80019291 0.92348466]
[ 7.24955258 -1.38159174]
[ 2.53097047 -1.97627957]
[ 0.77457472 2.53668882]
[ 3.77584258 0.96575591]
[ 8.22364653 -1.16501032]
[ 3.26144206 -1.18107376]]
K-means
(K=16)
• Centroid[[ 3.87736934 0.57313722]
[ 7.16112074 -1.18550695] [ 1.43408902
1.17407622] [ 2.40746487 -1.78461224]
[ 5.02526941 0.38700173] [ 3.24016293
-1.13702569] [ 8.513818 -1.39771063]
[ 3.78846888 1.4896326 ] [ 1.24719036
2.17295335] [ 3.00049495 -2.33050427]
[ 4.75649048 1.46802344] [ 7.54799
-1.98403464] [ 7.95073189 -0.77552836] [
0.38528185 1.63712053] [ 1.47673639
-1.29792363] [ 0.56778286 2.62904902]]

You might also like