-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKmeans.py
230 lines (171 loc) · 5.55 KB
/
Kmeans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from __future__ import division
import numpy as np
import pandas as pd
import scipy
# from copy import deepcopy
from tqdm import tqdm
# from itertools import product
# import pickle as p
import random
from random import randint
from sklearn.cluster import KMeans
from scipy.stats import multivariate_normal
df = pd.read_csv("leaf.data",header=None)
X = np.asarray(df[df.columns[1:]])
Y = np.asarray(df[df.columns[0]]).reshape(-1,1)
##preprocess
X = (X - np.mean(X,axis=0))/np.std(X,axis=0)
print X
print(X.shape)
print(Y.shape)
def kmpp(k,X):
m,n = X.shape
c = np.zeros([k,n])
c[0,:] = X[random.randint(0,m-1),:]
D = np.zeros([m,k])
for j in range(k-1):
z= X - c[j,:].reshape(1,-1)
D[:,j] = np.sum(z**2,axis=1)
D[:,j] = D[:,j]/np.sum(D[:,j],axis = 0)
probs = np.amin(D,axis =1)/sum(np.amin(D,axis =1))
c[j+1,:] = X[np.random.choice(range(0,m),p = probs.flatten()),:]
return c
def kmeans(k,X,init = "rand"):
m,n = X.shape
# c = 6*np.random.uniform(size=[k,n])-3
if init=="rand":
c = 6*np.random.uniform(-3,3,size=[k,n])
else:
c = kmpp(k,X)
Y = np.zeros([m,1])
Y_prev=Y+1
max_iter=2000
while(np.array_equal(Y_prev,Y)==False and max_iter>0):
max_iter -=1
Y_prev=Y.copy()
D = np.zeros([m,k])
for i in range(c.shape[0]):
z= X - c[i,:].reshape(1,-1)
# print z.shape
# print np.sum(z**2,axis=1).shape
D[:,i]=np.sum(z**2,axis=1)#.reshape(-1,1)
# print D
Y = np.argmin(D,axis=1).reshape(-1,1)
# print c
# labels=np.unique(Y)
# for label in labels:
# c[label,:] = np.average(X[Y.flatten()==label,:],axis=0)
for i in range(k):
# c[i,:] = np.average(X[Y.flatten()==i,:],axis=0)
# if len(np.where(Y.flatten()==i)[0])==0:
if i not in Y.flatten():
c[i,:]=0
else:
# print np.average(X[np.where(Y.flatten()==i),:],axis=1).shape
c[i,:] = np.average(X[np.where(Y.flatten()==i)[0],:],axis=0)
# print c[i,:].shape
return Y
def kmeans_obj(k,X,Y):
m,n = X.shape
labels=np.unique(Y)
# c = np.zeros([len(labels),n])
out=0
# for label in labels:
for i in range(k):
if i in labels:
# center = np.average(X[Y.flatten()==label,:],axis=0)
center = np.average(X[np.where(Y.flatten()==i)[0],:],axis=0)
z= X[Y.flatten()==i,:] - center.reshape(1,-1)
out+= np.sum(z**2)
return out
Y_= kmeans(12,X)
print kmeans_obj(12,X,Y_)
Y1 = KMeans(n_clusters=12,init='random').fit_predict(X)
print kmeans_obj(12,X,Y1)
ks = [12,18,24,36,42]
for k in ks:
outs=[]
for i in range(20):
outs.append(kmeans_obj(k,X,kmeans(k,X)))
# outs.append(kmeans_obj(X,KMeans(n_clusters=12,init='random').fit_predict(X)))
# print outs
print k, np.mean(outs),np.var(outs)
###Gaussian clustering
def GMM(k,X,init="rand"):
m,n = X.shape
if init=="rand":
mu = np.random.uniform(-3,3,size=[k,n])
else:
mu = kmpp(k,X)
cov = np.asarray([np.identity(n)]*k)
q = np.zeros([m,k])+1
# lambdas = np.zeros(k)+1
lambdas = np.asarray([1/k]*k)
ll_old = -np.inf
converged=False
while converged==False:
##estep
for i in range(m):
den=0
for j in range(k):
pd = multivariate_normal(mu[j,:],cov[j,:,:],allow_singular=True)
num = lambdas[j] * pd.pdf(X[i,:])
den += num
q[i,j] = num
q[i,:] = q[i,:]/den
##mstep
for j in range(k):
mu[j,:] = np.sum(X*q[:,j].reshape(-1,1),axis=0)/np.sum(q[:,j])
# print np.diag(np.dot((X-mu[j,:]),(X-mu[j,:]).T))
# print (np.dot((X-mu[j,:]),(X-mu[j,:]).T)*q[:,j].reshape(-1,1)).shape
# cov[j,:,:] = np.sum(q[:,j].reshape(-1,1)*np.dot((X-mu[j,:]),(X-mu[j,:]).T),axis=0)/np.sum(q[:,j])
w_cov = np.zeros([n,n])
for i in range(m):
w_cov +=q[i,j]*np.dot((X[i,:]-mu[j,:]).reshape(-1,1),(X[i,:]-mu[j,:]).reshape(1,-1))
cov[j,:,:] = w_cov/np.sum(q[:,j])
lambdas[j] = np.sum(q[:,j])/m
cov = cov + np.asarray([np.identity(n)]*k)*0.00001
###likelihood function
ll_new=0
for i in range(m):
num=0
for j in range(k):
pd = multivariate_normal(mu[j,:],cov[j,:,:],allow_singular=True)
num += lambdas[j] * pd.pdf(X[i,:])
ll_new+= np.log(num)
if abs(ll_new-ll_old) < 1e-2:
converged=True
return q, ll_new
else:
ll_old = ll_new
# print ll_new
ks = [12,18,24,36,42]
res=[]
for k in tqdm(ks):
outs=[]
for i in tqdm(range(20)):
_,loss = GMM(k,X)
outs.append(loss)
res.append([k, np.mean(outs),np.var(outs)])
print k, np.mean(outs),np.var(outs)
print res
# ## kmeans++
ks = [12,18,24,36,42]
for k in ks:
outs=[]
for i in range(20):
outs.append(kmeans_obj(k,X,kmeans(k,X,init="kpp")))
# outs.append(kmeans_obj(X,KMeans(n_clusters=12,init='random').fit_predict(X)))
# print outs
print k, np.mean(outs),np.var(outs)
# In[59]:
ks = [12,18,24,36,42]
res=[]
for k in tqdm(ks):
outs=[]
for i in tqdm(range(20)):
_,loss = GMM(k,X,init="kpp")
outs.append(loss)
res.append([k, np.mean(outs),np.var(outs)])
print k, np.mean(outs),np.var(outs)
print res