Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k-means-unsupervised-clustering-scratch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions k-means-unsupervised-scratch
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

# coding: utf-8

# In[1]:


import numpy as np
from matplotlib import pyplot as plt
from sklearn.datasets import make_blobs


# In[7]:


X,y = make_blobs(n_samples=500, centers = 5)
print X.shape,y.shape


# In[8]:


plt.figure(0)
plt.grid("on")
plt.scatter(X[:,0], X[:,1])
plt.show()


# In[40]:


k = 5

colors = ['green','red','blue','yellow','orange','pink']

clusters = {}

for kx in range(k):
centre = 10.0*(2*np.random.random((X.shape[1],)) -1)
points = []
cluster = {

'centre' : centre,
'points' : points,
'color' : colors[kx]
}
clusters[kx] = cluster

print clusters


# In[14]:


def distance(v1,v2):
return np.sqrt(np.sum((v1-v2)**2))


# In[43]:


for ix in range(X.shape[0]):
dist = []
curr_x = X[ix]

for kx in range(k):
d = distance(curr_x, clusters[kx]['centre'])
dist.append(d)

current_cluster = np.argmin(dist)

clusters[current_cluster]['points'].append(curr_x)

for kx in range(k):
pts = np.array(clusters[kx]['points'])

clusters[kx]['coords'] = pts

plt.figure(0)
plt.grid("on")

for kx in range(k):
pts = clusters[kx]['coords']

try:
plt.scatter(pts[:,0],pts[:,1], color = clusters[kx]['color'])
except:
pass

center = clusters[kx]['centre']
plt.scatter(center[0],center[1], color='black',s=100,marker="*")

for kx in range(k):
if clusters[kx]['coords'].shape[0] > 0:
new_center = clusters[kx]['coords'].mean(axis=0)
else:
new_center = clusters[kx]['centre']

clusters[kx]['centre'] = new_center
clusters[kx]['points'] = []

plt.figure(1)
plt.grid("on")

for kx in range(k):
pts = clusters[kx]['coords']

try:
plt.scatter(pts[:,0],pts[:,1], color = clusters[kx]['color'])
except:
pass

center = clusters[kx]['centre']
plt.scatter(center[0],center[1], color='black',s=100,marker="*")