Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- import sklearn
- import math
- # Create Data and scatter them
- X = [7, 3, 1, 5, 1, 7, 8, 5]
- Y = [1, 4, 5, 8, 3, 8, 2, 9]
- labels = ["x" + str(i) for i in range(1, len(X)+1)]
- kdata = pd.DataFrame({"X": X, "Y": Y}, index=labels)
- plt.figure()
- plt.scatter(kdata.X, kdata.Y)
- for i in range(len(kdata.index)):
- plt.text(kdata.loc[labels[i], "X"], kdata.loc[labels[i], "Y"], '%s' % (str(labels[i])), size=15, zorder=1)
- plt.title("2-D points before clustering")
- plt.xlabel("X")
- plt.ylabel("Y")
- plt.show()
- # Create a kmeans model with initial centroids the 3 first points
- from sklearn.cluster import KMeans
- K = 3
- init_centroids = kdata.loc[labels[0:K], :]
- kmeans = KMeans(n_clusters=K, init=init_centroids)
- kmeans = kmeans.fit(kdata)
- print("Final Centroids = ")
- print(kmeans.cluster_centers_)
- print()
- print("Each point's label of centroid = ")
- print(kmeans.labels_)
- print()
- # Cohesion, Separation
- print("K = " + str(K))
- cohesion = kmeans.inertia_
- print("Cohesion = " + str(cohesion))
- separation = 0
- distance = lambda x1, x2: math.sqrt(((x1.X - x2.X) ** 2) + ((x1.Y - x2.Y) ** 2))
- m = kdata.mean()
- for i in list(set(kmeans.labels_)):
- mi = kdata.loc[kmeans.labels_ == i, :].mean()
- Ci = len(kdata.loc[kmeans.labels_ == i, :].index)
- separation += Ci * (distance(m, mi) ** 2)
- print("Separation = " + str(separation))
- print("CSS + BSS = " + str(cohesion + separation))
- # Draw centroids
- plt.figure()
- plt.scatter(kdata.X, kdata.Y, c=kmeans.labels_)
- for i in range(len(kdata.index)):
- plt.text(kdata.loc[labels[i], "X"], kdata.loc[labels[i], "Y"], '%s' % (str(labels[i])), size=15, zorder=1)
- plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], marker="+", s=169, c=range(K))
- plt.title("After clustering")
- plt.xlabel("X")
- plt.ylabel("Y")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement