-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram3_tr3.py
305 lines (236 loc) · 8.26 KB
/
program3_tr3.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
# coding: utf-8
# ### CMPE 255 Programming Assignment 3
# In[1]:
import numpy as np
import scipy as sp
import random
import math
from collections import defaultdict
from sklearn.cluster import KMeans
from sklearn.cluster import MiniBatchKMeans
from scipy.sparse import csr_matrix
from sklearn.cluster import DBSCAN
from sklearn.utils import shuffle
from matplotlib import pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import calinski_harabaz_score
import networkx as nx
# In[2]:
def csr_read(fname, ftype="csr", nidx=1):
r"""
Read CSR matrix from a text file.
\param fname File name for CSR/CLU matrix
\param ftype Input format. Acceptable formats are:
- csr - Compressed sparse row
- clu - Cluto format, i.e., CSR + header row with "nrows ncols nnz"
\param nidx Indexing type in CSR file. What does numbering of feature IDs start with?
"""
with open(fname) as f:
lines = f.readlines()
if ftype == "clu":
p = lines[0].split()
nrows = int(p[0])
ncols = int(p[1])
nnz = long(p[2])
lines = lines[1:]
assert(len(lines) == nrows)
elif ftype == "csr":
nrows = len(lines)
ncols = 0
nnz = 0
for i in range(nrows):
p = lines[i].split()
if len(p) % 2 != 0:
raise ValueError("Invalid CSR matrix. Row %d contains %d numbers." % (i, len(p)))
nnz += len(p)/2
for j in range(0, len(p), 2):
cid = int(p[j]) - nidx
if cid+1 > ncols:
ncols = cid+1
else:
raise ValueError("Invalid sparse matrix ftype '%s'." % ftype)
val = np.zeros(int(nnz), dtype=np.float)
ind = np.zeros(int(nnz), dtype=np.int)
ptr = np.zeros(nrows+1, dtype=np.long)
n = 0
for i in range(nrows):
p = lines[i].split()
for j in range(0, len(p), 2):
ind[n] = int(p[j]) - nidx
val[n] = float(p[j+1])
n += 1
ptr[i+1] = n
assert(n == nnz)
return csr_matrix((val, ind, ptr), shape=(nrows, ncols), dtype=np.float)
# In[3]:
def csr_idf(matrix, copy=False, **kargs):
r""" Scale a CSR matrix by idf.
Returns scaling factors as dict. If copy is True,
returns scaled matrix and scaling factors.
"""
if copy is True:
matrix = matrix.copy()
nrows = matrix.shape[0]
nnz = matrix.nnz
ind, val, ptr = matrix.indices, matrix.data, matrix.indptr
# document frequency
df = defaultdict(int)
for i in ind:
df[i] += 1
# inverse document frequency
for k,v in df.items():
df[k] = np.log(nrows / float(v)) ## df turns to idf - reusing memory
# scale by idf
for i in range(0, nnz):
val[i] *= df[ind[i]]
return df if copy is False else matrix
# In[4]:
def csr_l2normalize(matrix, copy=False, **kargs):
r""" Normalize the rows of a CSR matrix by their L-2 norm.
If copy is True, returns a copy of the normalized matrix.
"""
if copy is True:
matrix = matrix.copy()
nrows = matrix.shape[0]
nnz = matrix.nnz
ind, val, ptr = matrix.indices, matrix.data, matrix.indptr
# normalize
for i in range(nrows):
rsum = 0.0
for j in range(ptr[i], ptr[i+1]):
rsum += val[j]**2
if rsum == 0.0:
continue # do not normalize empty rows
rsum = float(1.0/np.sqrt(rsum))
for j in range(ptr[i], ptr[i+1]):
val[j] *= rsum
if copy is True:
return matrix
# In[5]:
#Read CSR matrix from the input file
csrMatrix = csr_read('train.dat', ftype="csr", nidx=1)
#Scale the CSR matrix by idf (Inverse Document Frequency)
csrIDF = csr_idf(csrMatrix, copy=True)
#Normalize the rows of a CSR matrix by their L-2 norm.
csrL2Normalized = csr_l2normalize(csrIDF, copy=True)
#Obtain a dense ndarray representation of the CSR matrix.
denseMatrix = csrL2Normalized.toarray()
# In[6]:
print(csrL2Normalized)
csrL2Normalized.shape
# In[7]:
print(denseMatrix.shape)
# In[8]:
kmeans = MiniBatchKMeans(n_clusters=200,random_state = 0)
kmeans.fit(csrL2Normalized)
# In[9]:
label = kmeans.labels_
points =centroids= centers = kmeans.cluster_centers_
# In[10]:
centers.shape, label.shape
indices = np.asarray(list(range(0,8580)))
lab = np.column_stack([indices,label])
# In[11]:
def MyDBSCAN(points, eps,minPts):
neighborhoods = []
core = []
border = []
noise = []
for i in range(len(points)):
neighbors = []
for p in range(0, len(points)):
# If the distance is below eps, p is a neighbor
if sp.spatial.distance.cosine(points[i] ,points[p]) <= eps:
neighbors.append(p)
neighborhoods.append(neighbors)
# If neighborhood has at least minPts, i is a core point
if len(neighbors) >= minPts :
core.append(i)
# Find border points
for i in range(len(points)):
neighbors = neighborhoods[i]
# Look at points that are not core points
if len(neighbors) < minPts:
for j in range(len(neighbors)):
# If one of its neighbors is a core, it is also in the core point's neighborhood,
# thus it is a border point rather than a noise point
if neighbors[j] in core:
border.append(i)
# Need at least one core point...
break
# Find noise points
for i in range(len(points)):
if i not in core and i not in border:
noise.append(i)
# # Invoke graph instance to visualize the cluster
G = nx.Graph()
nodes = core
G.add_nodes_from(nodes)
# Create neighborhood
for i in range(len(nodes)):
for p in range(len(nodes)):
# If the distance is below the threshold, add a link in the graph.
if p != i and sp.spatial.distance.cosine(points[nodes[i]] ,points[nodes[p]]) <= eps:
G.add_edges_from([(nodes[i], nodes[p])])
# List the connected components / clusters
clusters = list(nx.connected_components(G))
print("# clusters:", len(clusters))
print("clusters: ", clusters)
centers = []
for cluster in clusters:
coords = []
for point in list(cluster):
coords.append(points[point])
center = np.mean(coords,axis =0)
centers.append(center)
expanded_clusters = clusters
for pt in border:
distances = {}
for i, center in enumerate(centers):
# print("point = ", pt, " center = ", i)
# print(scipy.spatial.distance.cosine(points[pt],center))
distances[i] = sp.spatial.distance.cosine(points[pt],center)
# distances =
# print("closest cluster for point %d = %d " %(pt, min(distances, key=distances.get)))
closest_cluster = min(distances, key=distances.get)
expanded_clusters[closest_cluster].add(pt)
# print(clusters[closest_cluster])
label , centroids, expanded_clusters
centroid_labels = [len(clusters)+1]* len(centroids)
for index, clstr in enumerate(expanded_clusters):
for n in clstr:
centroid_labels[n]= index
print(np.unique(centroid_labels))
final_labels = [0]*len(label)
for i,l in enumerate(label):
final_labels[i] = centroid_labels[l]
np.unique(final_labels)
return final_labels
# In[13]:
kValues = list()
scores = list()
eps =0.5
minPts = 1
d = MyDBSCAN(points, eps,minPts)
with open("submission1.txt", "w") as f:
for l in d:
f.write("%s\n"%l)
# for k in range(3,22,2):
# clustering = DBSCAN(eps= eps, min_samples=k,metric="euclidean").fit(lab)
# labels = clustering.labels_
# score = calinski_harabaz_score(lab, labels)
# kValues.append(k)
# scores.append(score)
# print ("For K= %d Calinski Harabaz Score is %f" %(k, score))
# In[14]:
# %matplotlib inline
# import matplotlib.pyplot as plt
# plt.plot(kValues, scores)
# plt.xticks(kValues, kValues)
# plt.xlabel('Number of Clusters k')
# plt.ylabel('Calinski and Harabaz Score')
# plt.title('Trend of Average Distance to Centroid/Diameter')
# plt.grid(linestyle='dotted')
# plt.savefig('plot.png')
# plt.show()