-
Notifications
You must be signed in to change notification settings - Fork 0
/
inout.py
executable file
·293 lines (275 loc) · 9.91 KB
/
inout.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
import math
import random
#import gmpy # combinatory analysis
def read_data(filename, skip_first_line=False, ignore_first_column=False):
'''
Loads data from a csv file and returns the corresponding list.
All data are expected to be floats, except in the first column.
@param filename: csv file name.
@param skip_first_line: if True, the first line is not read.
Default value: False.
@param ignore_first_column: if True, the first column is ignored.
Default value: False.
@return: a list of lists, each list being a row in the data file.
Rows are returned in the same order as in the file.
They contains floats, except for the 1st element which is a string
when the first column is not ignored.
'''
f = open(filename, 'r')
if skip_first_line:
f.readline()
data = []
for line in f:
line = line.split(",")
line[1:] = [ float(x) for x in line[1:] ]
if ignore_first_column:
line = line[1:]
data.append(line)
f.close()
return data
def write_data(data, filename, indexation=0):
'''
Writes data in a csv file.
@param data: a list of lists
@param filename: the path of the file in which data is written.
The file is created if necessary; if it exists, it is overwritten.
'''
# If you're curious, look at python's module csv instead, which offers
# more powerful means to write (and read!) csv files.
f = open(filename, 'w')
f.write("numero, attribut 1, attribut 2,... attribut n\n")
for item in data:
i = data.index(item) + 1
f.write("%i" % i)
f.write(",")
f.write(','.join([repr(x) for x in item]))
f.write('\n')
f.close()
def write_output(data, groups, filename):
'''
Writes output in a csv file.
@param data: a list of lists
@param filename: the path of the file in which data is written.
The file is created if necessary; if it exists, it is overwritten.
'''
# If you're curious, look at python's module csv instead, which offers
# more powerful means to write (and read!) csv files.
f = open(filename, 'w')
f.write("numero, attribut 1, attribut 2,... attribut n, cluster number\n")
for item in data:
i = data.index(item) + 1
f.write("%i" % i)
f.write(",")
f.write(','.join([repr(x) for x in item]))
f.write(",")
f.write("%i" % (groups[data.index(item)] + 1))
f.write('\n')
f.close()
def plot_matlab_2D(data, filename, group):
'''
Writes data in a .mat file.
@param data: a list of lists
@param filename: the path of the file in which data is written.
The file is created if necessary; if it exists, it is overwritten.
'''
# If you're curious, look at python's module csv instead, which offers
# more powerful means to write (and read!) csv files.
f = open(filename, 'w')
for item in data:
#item = item[0:2] ---> Activate to plot the sepales caracteristics for the IRIS data
#item = item[2:4] ---> Activate to plot the petales caracteristics for the IRIS data
f.write('plot(')
f.write(','.join([repr(x) for x in item]))
f.write(",'{}')".format(group))
f.write(';')
f.write('hold on')
f.write('\n')
f.close()
def plot_matlab_3D(data, x, y, z, filename, group):
'''
Writes data in a .mat file.
@param data: a list of lists
@param filename: the path of the file in which data is written.
The file is created if necessary; if it exists, it is overwritten.
'''
# If you're curious, look at python's module csv instead, which offers
# more powerful means to write (and read!) csv files.
f = open(filename, 'w')
for item in data:
f.write('plot3(')
f.write(','.join([repr(x) for x in item]))
f.write(",'{}')".format(group))
f.write(';')
f.write('hold on')
f.write('\n')
f.close()
def generate_random_data(nb_objs, nb_attrs, frand=random.random):
'''
Generates a matrix of random data.
@param frand: the fonction used to generate random values.
It defaults to random.random.
Example::
import random
generate_random_data(5, 6, lambda: random.gauss(0, 1))
@return: a matrix with nb_objs rows and nb_attrs+1 columns. The 1st
column is filled with line numbers (integers, from 1 to nb_objs).
'''
data = []
for i in range(nb_objs):
#line = [i+1]
#for j in range(numAtt):
#line.append(frand())
i = i + 1
line = map(lambda x: frand(), range(nb_attrs))
data.append(line)
return(data)
def calculate_euclidian_distance(vector_1, vector_2):
'''
Generate the Euclidian distance between two vectors
@param param: the two vectors
@return: the distance between the two input vectors
'''
if len(vector_1) == len(vector_2):
s = 0
for i in range(len(vector_1)):
s = s + math.pow(float(vector_1[i]) - float(vector_2[i]), 2)
return float(math.sqrt(s))
def calculate_manhattan_distance(vector_1, vector_2):
'''
Generate the distance of Manhattan between two vectors
@param param: the two vectors
@return: the distance between the two input vectors
'''
if len(vector_1) == len(vector_2):
s = 0
for i in range(len(vector_1)):
s = s + math.fabs(float(vector_1[i]) - float(vector_2[i]))
return float(s)
def points_center(L, ancient_centers):
'''
@return: The new center of the group L
@param L: A list of vectors
@param ancient_centers: the list of centers during the precedent iteration
'''
C = [] #empty group problem
for x in range(len(L)):
c = []
for y in range(len(ancient_centers[0])):
s = 0.0000
t = 0
for z in range(len(L[x])):
s = s + float(L[x][z][y])
t = t + 1
if len(L[x]) != 0:
c = c + [s / len(L[x])]
else:
c = ancient_centers[x] #same center, doesn't change
C = C + [c]
return C
def dunn_index(U, dist):
'''
@return: Dunn index of quality
@param U: a list of lists (containing observations ordered in clusters)
@param dist: the distance methode
'''
D = []
K = []
for i in range(len(U) - 1):#compare elements one time
for j in range(i + 1, len(U)):# //
for k in range(len(U[i])):#calculate distance
for l in range(len(U[j])):
if dist == 1:
d = calculate_euclidian_distance(U[i][k], U[j][l])
if dist == 2:
d = calculate_manhattan_distance(U[i][k], U[j][l])
D = D + [d]
K = K + [min(D)]
#Same thing here
DD = []
KK = []
for i in range(len(U)):
for j in range(len(U[i])):
for k in range(len(U[i])):
if dist == 1:
d = calculate_euclidian_distance(U[i][j], U[i][k])
if dist == 2:
d = calculate_manhattan_distance(U[i][j], U[i][k])
DD = DD + [d]
m = max(DD)
if m :
KK = KK + [m]
return float(min(K) / max(KK))
else :
return 0
def DB_index(dist, centers, U, G):
'''
@return: DB index of quality
@param U: a list of lists (containing distances of all elements to all centers at the end of the algorithm)
@param dist: the distance methode
@param G: listing groups of the i_th element of the observations list
@param centers: list of centers of clusters at the end of the algorithm
'''
K = []
for i in range(len(U[0])):
K = K + [0.0] # Creating a list of length(u(i)) float elements
for i in range(len(U)):
k = G[i] - 1 # to get the index
K[k] = K[k] + U[i][k]
K = [t / len(centers) for t in K]
#Sum of all distances of intra cluster elements to the center i
s = 0
S = []
for i in range(len(centers)):
for j in range(len(centers)):
if i == j:
continue
if calculate_euclidian_distance(centers[i], centers[j]) == 0.:
continue
if calculate_manhattan_distance(centers[i], centers[j]) == 0. :
continue
if dist == 1 :
d = (K[i] + K[j]) / calculate_euclidian_distance(centers[i], centers[j])
if dist == 2:
d = (K[i] + K[j]) / calculate_manhattan_distance(centers[i], centers[j])
S = S + [d]
m = max (S)
s = s + m
S = []
return s / len(centers)
def intra_cluster_distance(T, centers, dist):
'''
@return: Calcule la somme de toutes les distances intra-clusters
@param T: vecteur de list de veteur des elements classer
@param centers: list des vecteurs centres
@param dist: distance calculation methode
'''
s = 0.
for t in T:
for element in t:
if dist == 1:
d = calculate_euclidian_distance(element, centers[T.index(t)])
if dist == 2:
d = calculate_manhattan_distance(element, centers[T.index(t)])
s = s + d
return s
def inter_cluster_distance(centers,observations, dist):
'''
@return: Calcule la somme de toutes les distances inter-clusters
@param observations: vecteur de list de veteur des elements a classer
@param centers: list des vecteurs centres
@param dist: distance calculation methode
'''
s=0.
g = []
# Calcule du centre de gravite
for i in range(len(observations[0])):
for observation in observations:
s = s + observation[i]
g = g + [s/len(observations[0])]
for center in centers:
if dist==1:
d=calculate_euclidian_distance(center, g)
if dist==2:
d=calculate_manhattan_distance(center, g)
s =s+d
return s