forked from Evo-ML/EvoCluster
-
Notifications
You must be signed in to change notification settings - Fork 2
/
measures.py
228 lines (159 loc) · 6.62 KB
/
measures.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
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 31 21:22:53 2019
@author: Raneem
"""
from sklearn import metrics
from sklearn.metrics.pairwise import euclidean_distances
import statistics
import math
import numpy
import sys
import math
def HS(labelsTrue, labelsPred):
return float("%0.2f"%metrics.homogeneity_score(labelsTrue,labelsPred))
def CS(labelsTrue, labelsPred):
return float("%0.2f"%metrics.completeness_score(labelsTrue,labelsPred))
def VM(labelsTrue, labelsPred):
return float("%0.2f"%metrics.v_measure_score(labelsTrue,labelsPred))
def AMI(labelsTrue, labelsPred):
return float("%0.2f"%metrics.adjusted_mutual_info_score(labelsTrue,labelsPred))
def ARI(labelsTrue, labelsPred):
return float("%0.2f"%metrics.adjusted_rand_score(labelsTrue,labelsPred))
def Fmeasure(labelsTrue, labelsPred):
return float("%0.2f"%metrics.f1_score(labelsTrue, labelsPred, average='macro'))
def SC(points, labelsPred):#Silhouette Coefficient
if numpy.unique(labelsPred).size == 1:
fitness = sys.float_info.max
else:
silhouette= float("%0.2f"%metrics.silhouette_score(points, labelsPred, metric='euclidean'))
silhouette = (silhouette + 1) / 2
fitness = 1 - silhouette
return fitness
def accuracy(labelsTrue, labelsPred):#Silhouette Coefficient
#silhouette = metrics.accuracy_score(labelsTrue, labelsPred, normalize=False)
return ARI(labelsTrue, labelsPred)
def delta_fast(ck, cl, distances):
values = distances[numpy.where(ck)][:, numpy.where(cl)]
values = values[numpy.nonzero(values)]
return numpy.min(values)
def big_delta_fast(ci, distances):
values = distances[numpy.where(ci)][:, numpy.where(ci)]
#values = values[numpy.nonzero(values)]
return numpy.max(values)
def dunn_fast(points, labels):
""" Dunn index - FAST (using sklearn pairwise euclidean_distance function)
Parameters
----------
points : numpy.array
numpy.array([N, p]) of all points
labels: numpy.array
numpy.array([N]) labels of all points
"""
distances = euclidean_distances(points)
ks = numpy.sort(numpy.unique(labels))
deltas = numpy.ones([len(ks), len(ks)])*1000000
big_deltas = numpy.zeros([len(ks), 1])
l_range = list(range(0, len(ks)))
for k in l_range:
for l in (l_range[0:k]+l_range[k+1:]):
deltas[k, l] = delta_fast((labels == ks[k]), (labels == ks[l]), distances)
big_deltas[k] = big_delta_fast((labels == ks[k]), distances)
di = numpy.min(deltas)/numpy.max(big_deltas)
return di
def DI(points, labelsPred):#dunn index
dunn = float("%0.2f"%dunn_fast(points, labelsPred))
if(dunn < 0):
dunn = 0
fitness = 1 - dunn
return fitness
def DB(points, labelsPred):
return float("%0.2f"%metrics.davies_bouldin_score(points, labelsPred))
def stdev(individual, labelsPred, k, points):
std = 0
distances = []
f = (int)(len(individual) / k)
startpts = numpy.reshape(individual, (k,f))
for i in range(k):
index_list = numpy.where(labelsPred == i)
distances = numpy.append(distances, numpy.linalg.norm(points[index_list]-startpts[i], axis = 1))
std = numpy.std(distances)
#stdev = math.sqrt(std)/ k
#print("stdev:",stdev)
return std
'''
def SSE(individual, k, points):
f = (int)(len(individual) / k)
startpts = numpy.reshape(individual, (k,f))
labelsPred = [-1] * len(points)
sse = 0
for i in range(len(points)):
distances = numpy.linalg.norm(points[i]-startpts, axis = 1)
sse = sse + numpy.min(distances)
clust = numpy.argmin(distances)
labelsPred[i] = clust
if numpy.unique(labelsPred).size < k:
sse = sys.float_info.max
print("SSE:",sse)
return sse
'''
def SSE(individual, labelsPred, k, points):
f = (int)(len(individual) / k)
startpts = numpy.reshape(individual, (k,f))
fitness = 0
centroidsForPoints = startpts[labelsPred]
fitnessValues = numpy.linalg.norm(points-centroidsForPoints, axis = 1)**2
fitness = sum(fitnessValues)
return fitness
def TWCV(individual, labelsPred, k, points):
sumAllFeatures = sum(sum(numpy.power(points,2)))
sumAllPairPointsCluster = 0
for clusterId in range(k):
indices = numpy.where(numpy.array(labelsPred) == clusterId)[0]
pointsInCluster = points[numpy.array(indices)]
sumPairPointsCluster = sum(pointsInCluster)
sumPairPointsCluster = numpy.power(sumPairPointsCluster,2)
sumPairPointsCluster = sum(sumPairPointsCluster)
sumPairPointsCluster = sumPairPointsCluster/len(pointsInCluster)
sumAllPairPointsCluster += sumPairPointsCluster
fitness = (sumAllFeatures - sumAllPairPointsCluster)
return fitness
def purity(labelsTrue,labelsPred):
# get the set of unique cluster ids
labelsTrue=numpy.asarray(labelsTrue).astype(int)
labelsPred=numpy.asarray(labelsPred).astype(int)
k=(max(labelsTrue)+1).astype(int)
totalSum = 0;
for i in range(0,k):
max_freq=0
t1=numpy.where(labelsPred == i)
for j in range(0,k):
t2=numpy.where(labelsTrue == j)
z=numpy.intersect1d(t1,t2);
e=numpy.shape(z)[0]
if (e >= max_freq):
max_freq=e
totalSum=totalSum + max_freq
purity=totalSum/numpy.shape(labelsTrue)[0]
#print("purity:",purity)
return purity
def entropy(labelsTrue,labelsPred):
# get the set of unique cluster ids
labelsTrue=numpy.asarray(labelsTrue).astype(int)
labelsPred=numpy.asarray(labelsPred).astype(int)
k=(max(labelsTrue)+1).astype(int)
entropy=0
for i in range(0,k):
t1=numpy.where(labelsPred == i)
entropyI=0
for j in range(0,k):
t2=numpy.where(labelsTrue == j)
z=numpy.intersect1d(t1,t2);
e=numpy.shape(z)[0]
if (e!=0):
entropyI=entropyI+(e/numpy.shape(t1)[1])*math.log(e/numpy.shape(t1)[1])
a=numpy.shape(t1)[1]
b=numpy.shape(labelsTrue)[0]
entropy=entropy+(( a / b )*((-1 / math.log(k))*entropyI))
#print("entropy:",entropy)
return entropy