/
datafilter.py
402 lines (301 loc) · 12.3 KB
/
datafilter.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
Data filter module.
"""
__docformat__ = "restructuredtext en"
## Copyright (c) 2009 Emmanuel Goossaert
##
## This file is part of npy.
##
## npy is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## npy is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with npy. If not, see <http://www.gnu.org/licenses/>.
import sys
from data import DataSet
from data import DataInstance
class Numerizer:
"""
Transforms all the ordinal and categorical attributes
of a given `DataSet` into numerical interval attributes.
:IVariables:
__attributes : dictionary
Dictionary of the non-numeric attributes of the data set
used to build this Numerizer. Each key is a dictionary itself,
and associates a number to each string value of a given attribute.
__label : dictionary
Dictionary of the non-numeric labels of the `DataSet`.
"""
def __init__(self, ds_source):
"""
Builds a `Numerizer` based on the data provided in ds_source.
:Parameters:
ds_source : `DataSet`
Data to use in order to build the `Numerizer`.
"""
self.attributes = {}
self.label = {}
data_instances = ds_source.get_data_instances()
for data_instance in data_instances:
# Process the attribute values
for index, value in enumerate(data_instance.get_attributes()):
try:
number = float(value)
except ValueError:
# Every time a non-float attribute value is met,
# it is added to the numerizer
self.add_value_for_attribute(value, index)
# Process the label value
label = data_instance.get_label_number()
try:
number = float(label)
except ValueError:
# Every time a non-float label value is met,
# it is added to the numerizer
self.__add_value_for_label(label)
def __add_value_for_attribute(self, value_attribute, index_attribute):
"""
Add a possible value for a given ordinal or categorical attribute.
:Parameters:
index_attribute : integer
Id of the attribute in the `DataInstance` sequence
value_attribute : string
Value of the attribute to store.
:Raises NpyIndexError:
If the given index is already in use.
"""
if not index_attribute in self.attributes:
self.attributes[index_attribute] = {}
else:
raise NpyIndexError, 'Index value already used'
values = self.attributes[index_attribute]
if not value_attribute in values:
values[value_attribute] = len(values) + 1
def attribute_string_to_number(self, value_attribute, index_attribute):
"""
Get the numeric value associated with the string value
for a given attribute.
:Parameters:
index_attribute : integer
Id of the attribute in the `DataInstance` sequence
value_attribute : string
Value of the attribute to convert to a number.
:Returns:
integer: the numeric value associated with a attribute string.
"""
if not index_attribute in self.attributes:
return None
values = self.attributes[index_attribute]
if not value_attribute in values:
return None
return values[value_attribute]
def __add_value_for_label(self, value_label):
"""
Add value for label.
"""
if not value_label in self.label:
self.label[value_label] = len(self.label) + 1
def label_string_to_number(self, label_string):
"""
Get the numeric value associated with the string value
for the label.
:Parameters:
label_string : string
Label string to be converted into a label number.
:Returns:
integer : the label number.
Returns None if the label string is not found.
"""
if not label_string in self.label:
return None
return self.label[label_string]
def label_number_to_string(self, label_number):
"""
Get the string value associated with the numeric value
for the label.
:Parameters:
label_number : integer
Label number to be converted into a label string.
:Returns:
string : the label string.
Returns None if the label index is not found.
"""
label_string = None
for string, number in self.label.iteritems():
if label_number == number:
label_string = string
break
return label_string
def numerize(self, ds_source):
"""
Apply the numerizing operation to a given `DataSet`.
:Parameters:
ds_source : `DataSet`
Data set to numerize.
:Returns:
`DataSet` : Numerized data set.
:Raises NpyDataTypeError:
If ds_source has already been numerized.
"""
if ds_source.is_numerized == True:
raise NpyDataTypeError, 'ds_source has already been numerized.'
ds_dest = DataSet()
ds_dest.set_name_attribute(ds_source.get_name_attribute())
data_instances = ds_source.get_data_instances()
for data_instance_old in data_instances:
attributes = []
# Process the attribute values
for index, value in enumerate(data_instance_old.get_attributes()):
try:
number = float(value)
except ValueError:
# Every time a non-float attribute value is met,
# it is added to the numerizer
number = self.attribute_string_to_number(value, index)
attributes.append(number)
# Process the label value
label_old = data_instance_old.get_label_number()
try:
label_new = float(label_old)
except ValueError:
# Every time a non-float label value is met,
# it is added to the numerizer
label_new = self.label_string_to_number(label_old)
ds_dest.add_data_instance(data_instance_old.get_index_number(), attributes, label_new)
ds_dest.is_numerized = True
return ds_dest
class Normalizer:
"""
Transforms all the numerical values of a given `Dataset` into values
strictly contained into a given interval.
:IVariables:
__lower_bound : float
Lower bound of the interval into which the values of the
`DataSet` have to be translated.
__upper_bound : float
Upper bound of the interval into which the values of the
`DataSet` have to be translated.
__min : sequence
Sequence of the smallest possible values for every attribute.
__max : sequence
Sequence of the highest possible values for every attribute.
"""
def __init__(self, ds_source, lower_bound=0, upper_bound=1):
"""
Builds a `Normalizer` based on the data provided in ds_source.
:Parameters:
ds_source : `DataSet`
`DataSet` used to build the normalizer.
:Raises NpyDataTypeError:
If the given `DataSet` has not been numerized.
"""
if ds_source.is_numerized == False:
raise NpyDataTypeError, 'ds_source must be numerized first.'
self.lower_bound = float(lower_bound)
self.upper_bound = float(upper_bound)
self.min = None
self.max = None
nb_attributes = ds_source.get_nb_attributes()
value_min = [ float( sys.maxint) for i in range(nb_attributes) ]
value_max = [ float(-sys.maxint) for i in range(nb_attributes) ]
data_instances = ds_source.get_data_instances()
for data_instance in data_instances:
# Process the attribute values
for index, value in enumerate(data_instance.get_attributes()):
if value < value_min[index]:
value_min[index] = float(value)
if value > value_max[index]:
value_max[index] = float(value)
self.__set_min(value_min)
self.__set_max(value_max)
def set_lower_bound(self, value):
self.lower_bound = float(value)
def set_upper_bound(self, value):
self.upper_bound = float(value)
def __set_min(self, value_min):
self.min = value_min
def __set_max(self, value_max):
self.max = value_max
def normalize(self, ds_source):
"""
Apply the normalizing operation to a given `DataSet`.
:Parameters:
ds_source : `DataSet`
Data set to normalize.
:Returns:
`DataSet` : Normalized data set.
:Raises NpyDataTypeError:
If the given `DataSet` has not been numerized.
"""
if ds_source.is_numerized == False:
raise NpyDataTypeError, 'ds_source must be numerized first.'
ds_dest = DataSet()
ds_dest.set_name_attribute(ds_source.get_name_attribute())
data_instances = ds_source.get_data_instances()
for data_instance_old in data_instances:
attributes_new = []
# Normalize each attribute
for index, value in enumerate(data_instance_old.get_attributes()):
value_new = (value - self.min[index]) * self.max[index] * (self.upper_bound - self.lower_bound) + self.lower_bound
attributes_new.append(value_new)
ds_dest.add_data_instance(data_instance_old.get_index_number(), attributes_new, data_instance_old.get_label_number())
ds_dest.is_numerized = True
return ds_dest
class Filter:
"""
Embeds a Numerizer and a Normalizer and allows to automatize the creation
of those two filters, and also of their use.
:IVariables:
__numerizer : `Numerizer`
`Numerizer` used by the filter.
__normalizer : `Normalizer`
`Normalizer` used by the filter.
"""
def __init__(self, ds_source, normalizer_lower_bound=None, normalizer_upper_bound=None):
"""
Initializer.
:Parameters:
ds_source : `DataSet`
`DataSet` used to create the filter.
normalizer_lower_bound : float
Lower bound used by the `Normalizer`.
normalizer_upper_bound : float
Upper bound used by the `Normalizer`.
"""
self.numerizer = Numerizer(ds_source)
ds_numerized = self.numerizer.numerize(ds_source)
if normalizer_lower_bound != None or normalizer_upper_bound != None:
self.normalizer = Normalizer(ds_numerized, normalizer_lower_bound, normalizer_upper_bound)
else:
self.normalizer = Normalizer(ds_numerized)
def filter(self, ds_source):
"""
Filter ds_source and produce and numerized and normalized
data set.
:Parameters:
ds_source : `DataSet`
`DataSet` to filter.
:Returns:
`DataSet` : data set filtered
"""
ds_numerized = self.numerizer.numerize(ds_source)
ds_normalized = self.normalizer.normalize(ds_numerized)
return ds_normalized
def label_number_to_string(self, number):
"""
Get the string value associated with the numeric value
for the label.
:Parameters:
number : integer
Label number to be converted into a label string.
:Returns:
string : the label string.
"""
return self.numerizer.label_number_to_string(number)