Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

7.

kmeans - Jupyter Notebook 11/03/20, 2(16 AM

In [1]: 1 import numpy as np


2 import matplotlib.pyplot as plt
3 from sklearn.datasets import make_blobs

In [2]: 1 X,Y = make_blobs(n_samples=500,centers=5,random_state=3,n_features

In [3]: 1 plt.scatter(X[:,0],X[:,1],c=Y)

Out[3]: <matplotlib.collections.PathCollection at 0x1a1eef5ad0>

In [4]: 1 k=5
2
3 colors = ["green","yellow","red","blue","orange"]
4 clusters = {}
5
6 for i in range(k):
7 center = 10*(2*np.random.random((X.shape[1],))-1)
8 points = []
9
10 cluster = {
11 'center':center,
12 'points':points,
13 'color':colors[i]
14 }
15
16 clusters[i] = cluster

http://localhost:8888/notebooks/Desktop/FOOD/NSIT/Pattern%20Recognition/7.kmeans.ipynb Page 1 of 3
7.kmeans - Jupyter Notebook 11/03/20, 2(16 AM

In [5]: 1 clusters

Out[5]: {0: {'center': array([-6.64359202, -6.42507785]),


'points': [],
'color': 'green'},
1: {'center': array([-8.49484246, -0.20337736]),
'points': [],
'color': 'yellow'},
2: {'center': array([ 0.44878223, -6.30271839]),
'points': [],
'color': 'red'},
3: {'center': array([3.22463233, 4.87300261]), 'points': [], 'color
': 'blue'},
4: {'center': array([ 2.63478937, -2.29404665]),
'points': [],
'color': 'orange'}}

In [6]: 1 def distance(v1,v2):


2 d = np.sqrt(np.sum((v1-v2)**2))
3 return d

In [7]: 1 def assignclusters(X,clusters):


2 for ix in range(X.shape[0]):
3 curr_x = X[ix]
4 dist = []
5
6 for i in range(k):
7 d = distance(curr_x,clusters[i]['center'])
8 dist.append(d)
9
10 current_cluster = np.argmin(dist)
11
12 clusters[current_cluster]['points'].append(curr_x)
13
14 def updateClusters(X,clusters):
15 for i in range(k):
16 pts = np.array(clusters[i]['points'])
17
18 if pts.shape[0]>0:
19 newu = pts.mean(axis=0)
20 clusters[i]['center'] = newu
21 clusters[i]['points'] = []

In [8]: 1 maxEpochs = 20
2 for i in range(maxEpochs):
3 assignclusters(X,clusters)
4 updateClusters(X,clusters)

http://localhost:8888/notebooks/Desktop/FOOD/NSIT/Pattern%20Recognition/7.kmeans.ipynb Page 2 of 3
7.kmeans - Jupyter Notebook 11/03/20, 2(16 AM

In [9]: 1 def plotclusters(X,clusters):


2 for i in range(k):
3 pts = np.array(clusters[i]['points'])
4 print(pts.shape)
5 #print(clusters[ik])
6
7 try:
8 plt.scatter(pts[:,0],pts[:,1],color=clusters[i]['color'
9 except:
10 pass
11
12 uk = clusters[i]['center']
13 plt.scatter(uk[0],uk[1],color="black",marker="*")

In [10]: 1 assignclusters(X,clusters)
2 plotclusters(X,clusters)

(99, 2)
(103, 2)
(98, 2)
(100, 2)
(100, 2)

In [ ]: 1

http://localhost:8888/notebooks/Desktop/FOOD/NSIT/Pattern%20Recognition/7.kmeans.ipynb Page 3 of 3

You might also like