Skip to content

Instantly share code, notes, and snippets.

@nekketsuuu
Created June 14, 2018 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekketsuuu/d2d4e565018cf44b3898c4d6208eece8 to your computer and use it in GitHub Desktop.
Save nekketsuuu/d2d4e565018cf44b3898c4d6208eece8 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
class GlobalKmeans(object):
def __init__(self, X, cluster):
self.X = X
self.num = X.shape[0]
self.label = np.zeros((self.num, 1))
self.cluster = cluster
self.k = 0
self.centroid = np.zeros((self.cluster, self.X.shape[1]))
def distance(self, x, y):
return np.linalg.norm(x-y)
def assign(self):
min_inertia = 0.0
new_centroids = np.zeros((self.k, self.X.shape[1]))
for i in xrange(self.num):
cs = np.concatenate((self.centroid, self.X[i].reshape((1, 2))), axis=0)
kmeans = KMeans(n_clusters=self.k, init=cs, n_init=1).fit(self.X)
if i == 0 or min_inertia > kmeans.inertia_:
min_inertia = kmeans.inertia_
new_centroids = kmeans.cluster_centers_
self.centroid = new_centroids
def give_label(self):
for i in xrange(self.num):
temp = self.distance(self.X[i], self.centroid[0])
labe = 0
for k in xrange(self.k):
if k == 0:
continue
d = self.distance(self.X[i], self.centroid[k])
if temp > d:
labe = k
temp = d
self.label[i] = labe
def fit(self):
self.k = 1
self.centroid = np.mean(self.X, axis = 0).reshape((1,2))
while(self.k < self.cluster):
self.k = self.k + 1
self.assign()
self.give_label()
def plot(self):
plt.figure(figsize=(10,10))
for j in xrange(self.cluster):
points = X[np.where(self.label == j)[0]]
plt.scatter(points[:,0], points[:,1])
plt.scatter(self.centroid[j,0], self.centroid[j,1], s = 300, marker = "x")
if __name__ == '__main__':
N = 30
mu1 = [-10, -15]
sigma1 = [[1, 0], [0, 1]]
mu2 = [0, 1]
sigma2 = [[1, 0], [0, 1]]
mu3 = [3, 5]
sigma3 = [[1, 0], [0, 1]]
x1 = 0.2 * np.random.multivariate_normal(mu1, sigma1, N)
x2 = 0.2 * np.random.multivariate_normal(mu2, sigma2, N)
x3 = 0.2 * np.random.multivariate_normal(mu3, sigma3, N)
X = np.concatenate([x1, x2, x3], axis=0)
g = GlobalKmeans(X, 3)
g.fit()
g.plot()
plt.show()
@kiwamizamurai
Copy link

Thank you very much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment