-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssign8.py
190 lines (138 loc) · 4.71 KB
/
Assign8.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
import sys
import numpy as np
import scipy.spatial as spa
import pandas as pd
def degree_matrix(num_nodes, similarity_matrix):
matrix = np.zeros((num_nodes, num_nodes))
for i in range(num_nodes):
matrix[i][i] = sum(similarity_matrix[i])
return matrix
def sortDistances(distances):
distances.sort(key=lambda x: x[1])
return distances
def knn_adj_matrix(num_nodes, mutual_knn_edgelist, **kwargs):
distances = kwargs.get('distances', None)
if(distances == None):
distances = np.ones((num_nodes, num_nodes))
matrix = np.zeros((num_nodes, num_nodes))
i = 0
j = 0
for node in mutual_knn_edgelist:
for neighbor in mutual_knn_edgelist[node]:
# default adj matrix is defined with either 1 or 0 in mutual node locations, however the adj matrix can be given edge weights through @param distance
matrix[node][neighbor] = distances[i][j]
j += 1
j = 0
i += 1
return matrix
def knn_edgelist(knn_graph):
mutual_knn = {}
for node in knn_graph:
mutual = []
for node_neighbor in knn_graph[node]:
if node in knn_graph[node_neighbor]:
mutual.append(node_neighbor)
mutual_knn[node] = mutual
return mutual_knn
def k_nearest_neighbor(points, k):
k_nearest_neighbors = []
dist_edge_weights = []
for i in range(len(points)):
distances = []
dew = []
for j in range(len(points)):
dist = spa.distance.euclidean(points[i], points[j])
dew.append(dist)
if(i == j):
continue
else:
distances.append(
(j, dist))
neighbors = sortDistances(distances)
dist_edge_weights.append(dew)
k_nearest_neighbors.append(neighbors[0:k])
knn_graph = {}
for i in range(len(k_nearest_neighbors)):
knn_graph[i] = []
for neighbors in k_nearest_neighbors[i]:
knn_graph[i].append(neighbors[0])
return [knn_graph, dist_edge_weights]
def markov_matrix(knn_adj_mat, deg_mat):
return np.dot(np.linalg.pinv(deg_mat), knn_adj_mat)
def laplacian_matrix(A, D):
return D - A
def asym_norm_laplacian_matrix(D, L):
return np.dot(np.linalg.pinv(D), L)
def sym_norm_laplacian_matrix(D, L):
neg_sqrt_deg = np.linalg.pinv(np.sqrt(D))
return np.dot(np.dot(neg_sqrt_deg, L), neg_sqrt_deg)
def normalize_U(U):
Y = []
for row in U:
denom = sum(row)**2
numer = row
Y.append(numer/denom)
return Y
def spectral_clustering(points, k, cut):
# compute similiarity matrix via knn
knn, node_distances = k_nearest_neighbor(points, k)
# edge list format of adj graph
mutual_knn = knn_edgelist(knn)
# similarity matrix
A = knn_adj_matrix(len(points), mutual_knn)
# degree matrix
D = degree_matrix(len(points), A)
#normalized_adj_matrix = markov_matrix(A, D)
L = laplacian_matrix(A, D)
B = None
if(cut == 'ratio'):
B = L
elif(cut == 'asymmetric'):
B = asym_norm_laplacian_matrix(D, L)
elif(cut == 'symmetric'):
B = sym_norm_laplacian_matrix(D, L)
# solve for eigenvalues and eigenvectors
evals, evecs = np.linalg.eigh(B)
# take k smallest eigen values and their correspond eigen vectors
U = []
for i in range(len(evecs) - 1, len(evecs) - k - 1, -1):
U.append(evecs[i])
U = np.array(U)
# Y normalize rows of U using 16.23 eq
U_t = np.transpose(U)
Y = normalize_U(U_t)
# run k-means on Y to get clusterings c1...ck
return
def getTrueClusterLabels(applianceEnergyAttributeData):
clusterLabels = []
for val in applianceEnergyAttributeData:
if(val <= 40):
clusterLabels.append(0)
elif(val <= 60):
clusterLabels.append(1)
elif(val <= 100):
clusterLabels.append(2)
else:
clusterLabels.append(3)
return clusterLabels
def parse(filename, num_points):
headers = list(pd.read_csv(filename, nrows=0))
headers.pop(28)
headers.pop(0)
data = pd.read_csv(filename, usecols=headers, nrows=num_points).to_numpy()
dataT = list(np.transpose(data))
# get appliance energy labels for performance assessment
applianceEnergyAttr = dataT[0]
trueClusterLabels = getTrueClusterLabels(applianceEnergyAttr)
dataT.pop(0)
dataT = np.array(dataT)
return [np.transpose(dataT), dataT, headers]
if __name__ == "__main__":
argv = sys.argv
filename = argv[1]
k = int(argv[2])
n = int(argv[3])
spread = float(argv[4])
clustering_objective = argv[5]
points, attr_view, headers = parse(filename, n)
spectral_clustering(points, k, clustering_objective)