-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFair_OS.py
More file actions
810 lines (641 loc) · 25.3 KB
/
Fair_OS.py
File metadata and controls
810 lines (641 loc) · 25.3 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# -*- coding: utf-8 -*-
#code adapted from https://github.com/analyticalmindsltd/smote_variants
import numpy as np
import time
import logging
import itertools
from sklearn.neighbors import NearestNeighbors
# setting the _logger format
_logger = logging.getLogger('smote_variants')
_logger.setLevel(logging.DEBUG)
_logger_ch = logging.StreamHandler()
_logger_ch.setFormatter(logging.Formatter(
"%(asctime)s:%(levelname)s:%(message)s"))
_logger.addHandler(_logger_ch)
def mode(data):
values, counts = np.unique(data, return_counts=True)
return values[np.where(counts == max(counts))[0][0]]
class StatisticsMixin:
"""
Mixin to compute class statistics and determine minority/majority labels
"""
def class_label_statistics(self, X, y):
"""
determines class sizes and minority and majority labels
Args:
X (np.array): features
y (np.array): target labels
"""
unique, counts = np.unique(y, return_counts=True)
self.class_stats = dict(zip(unique, counts))
self.min_label = unique[0] if counts[0] < counts[1] else unique[1]
self.maj_label = unique[1] if counts[0] < counts[1] else unique[0]
# shorthands
self.min_label = self.min_label
self.maj_label = self.maj_label
def check_enough_min_samples_for_sampling(self, threshold=2):
if self.class_stats[self.min_label] < threshold:
m = ("The number of minority samples (%d) is not enough "
"for sampling")
m = m % self.class_stats[self.min_label]
_logger.warning(self.__class__.__name__ + ": " + m)
return False
return True
class RandomStateMixin:
"""
Mixin to set random state
"""
def set_random_state(self, random_state):
"""
sets the random_state member of the object
Args:
random_state (int/np.random.RandomState/None): the random state
initializer
"""
self._random_state_init = random_state
if random_state is None:
self.random_state = np.random
elif isinstance(random_state, int):
self.random_state = np.random.RandomState(random_state)
elif isinstance(random_state, np.random.RandomState):
self.random_state = random_state
elif random_state is np.random:
self.random_state = random_state
else:
raise ValueError(
"random state cannot be initialized by " + str(random_state))
class ParameterCheckingMixin:
"""
Mixin to check if parameters come from a valid range
"""
def check_in_range(self, x, name, r):
"""
Check if parameter is in range
Args:
x (numeric): the parameter value
name (str): the parameter name
r (list-like(2)): the lower and upper bound of a range
Throws:
ValueError
"""
if x < r[0] or x > r[1]:
m = ("Value for parameter %s outside the range [%f,%f] not"
" allowed: %f")
m = m % (name, r[0], r[1], x)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_out_range(self, x, name, r):
"""
Check if parameter is outside of range
Args:
x (numeric): the parameter value
name (str): the parameter name
r (list-like(2)): the lower and upper bound of a range
Throws:
ValueError
"""
if x >= r[0] and x <= r[1]:
m = "Value for parameter %s in the range [%f,%f] not allowed: %f"
m = m % (name, r[0], r[1], x)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_less_or_equal(self, x, name, val):
"""
Check if parameter is less than or equal to value
Args:
x (numeric): the parameter value
name (str): the parameter name
val (numeric): value to compare to
Throws:
ValueError
"""
if x > val:
m = "Value for parameter %s greater than %f not allowed: %f > %f"
m = m % (name, val, x, val)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_less_or_equal_par(self, x, name_x, y, name_y):
"""
Check if parameter is less than or equal to another parameter
Args:
x (numeric): the parameter value
name_x (str): the parameter name
y (numeric): the other parameter value
name_y (str): the other parameter name
Throws:
ValueError
"""
if x > y:
m = ("Value for parameter %s greater than parameter %s not"
" allowed: %f > %f")
m = m % (name_x, name_y, x, y)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_less(self, x, name, val):
"""
Check if parameter is less than value
Args:
x (numeric): the parameter value
name (str): the parameter name
val (numeric): value to compare to
Throws:
ValueError
"""
if x >= val:
m = ("Value for parameter %s greater than or equal to %f"
" not allowed: %f >= %f")
m = m % (name, val, x, val)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_less_par(self, x, name_x, y, name_y):
"""
Check if parameter is less than another parameter
Args:
x (numeric): the parameter value
name_x (str): the parameter name
y (numeric): the other parameter value
name_y (str): the other parameter name
Throws:
ValueError
"""
if x >= y:
m = ("Value for parameter %s greater than or equal to parameter"
" %s not allowed: %f >= %f")
m = m % (name_x, name_y, x, y)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_greater_or_equal(self, x, name, val):
"""
Check if parameter is greater than or equal to value
Args:
x (numeric): the parameter value
name (str): the parameter name
val (numeric): value to compare to
Throws:
ValueError
"""
if x < val:
m = "Value for parameter %s less than %f is not allowed: %f < %f"
m = m % (name, val, x, val)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_greater_or_equal_par(self, x, name_x, y, name_y):
"""
Check if parameter is less than or equal to another parameter
Args:
x (numeric): the parameter value
name_x (str): the parameter name
y (numeric): the other parameter value
name_y (str): the other parameter name
Throws:
ValueError
"""
if x < y:
m = ("Value for parameter %s less than parameter %s is not"
" allowed: %f < %f")
m = m % (name_x, name_y, x, y)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_greater(self, x, name, val):
"""
Check if parameter is greater than value
Args:
x (numeric): the parameter value
name (str): the parameter name
val (numeric): value to compare to
Throws:
ValueError
"""
if x <= val:
m = ("Value for parameter %s less than or equal to %f not allowed"
" %f < %f")
m = m % (name, val, x, val)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_greater_par(self, x, name_x, y, name_y):
"""
Check if parameter is greater than or equal to another parameter
Args:
x (numeric): the parameter value
name_x (str): the parameter name
y (numeric): the other parameter value
name_y (str): the other parameter name
Throws:
ValueError
"""
if x <= y:
m = ("Value for parameter %s less than or equal to parameter %s"
" not allowed: %f <= %f")
m = m % (name_x, name_y, x, y)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_equal(self, x, name, val):
"""
Check if parameter is equal to value
Args:
x (numeric): the parameter value
name (str): the parameter name
val (numeric): value to compare to
Throws:
ValueError
"""
if x == val:
m = ("Value for parameter %s equal to parameter %f is not allowed:"
" %f == %f")
m = m % (name, val, x, val)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_equal_par(self, x, name_x, y, name_y):
"""
Check if parameter is equal to another parameter
Args:
x (numeric): the parameter value
name_x (str): the parameter name
y (numeric): the other parameter value
name_y (str): the other parameter name
Throws:
ValueError
"""
if x == y:
m = ("Value for parameter %s equal to parameter %s is not "
"allowed: %f == %f")
m = m % (name_x, name_y, x, y)
raise ValueError(self.__class__.__name__ + ": " + m)
def check_isin(self, x, name, li):
"""
Check if parameter is in list
Args:
x (numeric): the parameter value
name (str): the parameter name
li (list): list to check if parameter is in it
Throws:
ValueError
"""
if x not in li:
m = "Value for parameter %s not in list %s is not allowed: %s"
m = m % (name, str(li), str(x))
raise ValueError(self.__class__.__name__ + ": " + m)
def check_n_jobs(self, x, name):
"""
Check n_jobs parameter
Args:
x (int/None): number of jobs
name (str): the parameter name
Throws:
ValueError
"""
if not ((x is None)
or (x is not None and isinstance(x, int) and not x == 0)):
m = "Value for parameter n_jobs is not allowed: %s" % str(x)
raise ValueError(self.__class__.__name__ + ": " + m)
class ParameterCombinationsMixin:
"""
Mixin to generate parameter combinations
"""
@classmethod
def generate_parameter_combinations(cls, dictionary, raw):
"""
Generates reasonable paramter combinations
Args:
dictionary (dict): dictionary of paramter ranges
num (int): maximum number of combinations to generate
"""
if raw:
return dictionary
keys = sorted(list(dictionary.keys()))
values = [dictionary[k] for k in keys]
combinations = [dict(zip(keys, p))
for p in list(itertools.product(*values))]
return combinations
class NoiseFilter(StatisticsMixin,
ParameterCheckingMixin,
ParameterCombinationsMixin):
"""
Parent class of noise filtering methods
"""
def __init__(self):
"""
Constructor
"""
pass
def remove_noise(self, X, y):
"""
Removes noise
Args:
X (np.array): features
y (np.array): target labels
"""
pass
def get_params(self, deep=False):
"""
Return parameters
Returns:
dict: dictionary of parameters
"""
return {}
def set_params(self, **params):
"""
Set parameters
Args:
params (dict): dictionary of parameters
"""
for key, value in params.items():
setattr(self, key, value)
return self
class OverSampling(StatisticsMixin,
ParameterCheckingMixin,
ParameterCombinationsMixin,
RandomStateMixin):
"""
Base class of oversampling methods
"""
categories = []
cat_noise_removal = 'NR'
cat_dim_reduction = 'DR'
cat_uses_classifier = 'Clas'
cat_sample_componentwise = 'SCmp'
cat_sample_ordinary = 'SO'
cat_sample_copy = 'SCpy'
cat_memetic = 'M'
cat_density_estimation = 'DE'
cat_density_based = 'DB'
cat_extensive = 'Ex'
cat_changes_majority = 'CM'
cat_uses_clustering = 'Clus'
cat_borderline = 'BL'
cat_application = 'A'
def __init__(self):
pass
def det_n_to_sample(self, strategy, n_maj, n_min):
"""
Determines the number of samples to generate
Args:
strategy (str/float): if float, the fraction of the difference
of the minority and majority numbers to
generate, like 0.1 means that 10% of the
difference will be generated if str,
like 'min2maj', the minority class will
be upsampled to match the cardinality
of the majority class
"""
if isinstance(strategy, float) or isinstance(strategy, int):
return max([0, int((n_maj - n_min)*strategy)])
else:
m = "Value %s for parameter strategy is not supported" % strategy
raise ValueError(self.__class__.__name__ + ": " + m)
def sample_between_points(self, x, y):
"""
Sample randomly along the line between two points.
Args:
x (np.array): point 1
y (np.array): point 2
Returns:
np.array: the new sample
"""
return x + (y - x)*self.random_state.random_sample()
def sample_between_points_componentwise(self, x, y, mask=None):
"""
Sample each dimension separately between the two points.
Args:
x (np.array): point 1
y (np.array): point 2
mask (np.array): array of 0,1s - specifies which dimensions
to sample
Returns:
np.array: the new sample being generated
"""
if mask is None:
return x + (y - x)*self.random_state.random_sample()
else:
return x + (y - x)*self.random_state.random_sample()*mask
def sample_by_jittering(self, x, std):
"""
Sample by jittering.
Args:
x (np.array): base point
std (float): standard deviation
Returns:
np.array: the new sample
"""
return x + (self.random_state.random_sample() - 0.5)*2.0*std
def sample_by_jittering_componentwise(self, x, std):
"""
Sample by jittering componentwise.
Args:
x (np.array): base point
std (np.array): standard deviation
Returns:
np.array: the new sample
"""
return x + (self.random_state.random_sample(len(x))-0.5)*2.0 * std
def sample_by_gaussian_jittering(self, x, std):
"""
Sample by Gaussian jittering
Args:
x (np.array): base point
std (np.array): standard deviation
Returns:
np.array: the new sample
"""
return self.random_state.normal(x, std)
def sample(self, X, y):
"""
The samplig function reimplemented in child classes
Args:
X (np.matrix): features
y (np.array): labels
Returns:
np.matrix, np.array: sampled X and y
"""
return X, y
def fit_resample(self, X, y):
"""
Alias of the function "sample" for compatibility with imbalanced-learn
pipelines
"""
return self.sample(X, y)
def sample_with_timing(self, X, y):
begin = time.time()
X_samp, y_samp = self.sample(X, y)
_logger.info(self.__class__.__name__ + ": " +
("runtime: %f" % (time.time() - begin)))
return X_samp, y_samp
def preprocessing_transform(self, X):
"""
Transforms new data according to the possible transformation
implemented by the function "sample".
Args:
X (np.matrix): features
Returns:
np.matrix: transformed features
"""
return X
def get_params(self, deep=False):
"""
Returns the parameters of the object as a dictionary.
Returns:
dict: the parameters of the object
"""
pass
def set_params(self, **params):
"""
Set parameters
Args:
params (dict): dictionary of parameters
"""
for key, value in params.items():
setattr(self, key, value)
return self
def descriptor(self):
"""
Returns:
str: JSON description of the current sampling object
"""
return str((self.__class__.__name__, str(self.get_params())))
def __str__(self):
return self.descriptor()
class FOS_1(OverSampling): #F4_SMOTE(OverSampling):
categories = [OverSampling.cat_sample_ordinary,
OverSampling.cat_extensive]
def __init__(self,
proportion=1.0,
n_neighbors=5,
n_jobs=1,
random_state=None):
super().__init__()
self.check_greater_or_equal(proportion, "proportion", 0)
self.check_greater_or_equal(n_neighbors, "n_neighbors", 1)
self.check_n_jobs(n_jobs, 'n_jobs')
self.proportion = proportion
self.n_neighbors = n_neighbors
self.n_jobs = n_jobs
self.set_random_state(random_state)
@classmethod
def parameter_combinations(cls, raw=False):
parameter_combinations = {'proportion': [0.1, 0.25, 0.5, 0.75,
1.0, 1.1,1.5, 2.0],
'n_neighbors': [3, 5, 7]}
return cls.generate_parameter_combinations(parameter_combinations, raw)
def sample(self, X, y,prot_idx, pv_mid_pt, prot_grp, maj_min, nsamp,
pv_max,pv_min):
_logger.info(self.__class__.__name__ + ": " +
"Running sampling via %s" % self.descriptor())
self.class_label_statistics(X, y)
if not self.check_enough_min_samples_for_sampling():
return X.copy(), y.copy()
y = np.squeeze(y)
n_to_sample = nsamp
if maj_min == 0:
X_min = X[y == self.min_label]
y_min = y[y == self.min_label]
prot = X_min[:,prot_idx]
if prot_grp == 0:
X_min = X_min[prot==prot_grp]
y_min = y_min[prot==prot_grp]
if prot_grp == 1:
X_min = X_min[prot==prot_grp]
y_min = y_min[prot==prot_grp]
if maj_min == 1:
X_min = X[y == self.maj_label]
y_min = y[y == self.maj_label]
prot = X_min[:,prot_idx]
if prot_grp == 0:
X_min = X_min[prot==prot_grp]
y_min = y_min[prot==prot_grp]
if prot_grp == 1:
X_min = X_min[prot==prot_grp]
y_min = y_min[prot==prot_grp]
self.min_label = np.copy(self.maj_label)
if n_to_sample == 0:
return X.copy(), y.copy()
# fitting the model
n_neigh = min([len(X_min), self.n_neighbors+1])
nn = NearestNeighbors(n_neighbors=n_neigh, n_jobs=self.n_jobs)
nn.fit(X_min)
dist, ind = nn.kneighbors(X_min)
if n_to_sample == 0:
return X.copy(), y.copy()
# generating samples
#np.random.seed(seed=1)
base_indices = self.random_state.choice(list(range(len(X_min))),
n_to_sample)
neighbor_indices = self.random_state.choice(list(range(1, n_neigh)),
n_to_sample)
X_base = X_min[base_indices]
X_neighbor = X_min[ind[base_indices, neighbor_indices]]
samples = X_base + np.multiply(self.random_state.rand(n_to_sample,
1),
X_neighbor - X_base)
return (np.vstack([X, samples]),
np.hstack([y, np.hstack([self.min_label]*n_to_sample)]))
def get_params(self, deep=False):
return {'proportion': self.proportion,
'n_neighbors': self.n_neighbors,
'n_jobs': self.n_jobs,
'random_state': self._random_state_init}
class FOS_2(OverSampling): #F3a_SMOTE(OverSampling):
categories = [OverSampling.cat_sample_ordinary,
OverSampling.cat_extensive]
def __init__(self,
proportion=1.0,
n_neighbors=5,
n_jobs=1,
random_state=None):
super().__init__()
self.check_greater_or_equal(proportion, "proportion", 0)
self.check_greater_or_equal(n_neighbors, "n_neighbors", 1)
self.check_n_jobs(n_jobs, 'n_jobs')
self.proportion = proportion
self.n_neighbors = n_neighbors
self.n_jobs = n_jobs
self.set_random_state(random_state)
@classmethod
def parameter_combinations(cls, raw=False):
parameter_combinations = {'proportion': [0.1, 0.25, 0.5, 0.75,
1.0, 1.1,1.5, 2.0],
'n_neighbors': [3, 5, 7]}
return cls.generate_parameter_combinations(parameter_combinations, raw)
def sample(self, X, y,prot_idx, pv_mid_pt, prot_grp, maj_min, nsamp):
_logger.info(self.__class__.__name__ + ": " +
"Running sampling via %s" % self.descriptor())
self.class_label_statistics(X, y)
if not self.check_enough_min_samples_for_sampling():
return X.copy(), y.copy()
n_to_sample = nsamp
if maj_min == 0:
X_min = X[y == self.min_label]
y_min = y[y == self.min_label]
prot = X_min[:,prot_idx]
if prot_grp == 0:
X_min1 = X_min[prot<pv_mid_pt]
y_min1 = y_min[prot<pv_mid_pt]
if prot_grp == 1:
X_min1 = X_min[prot>pv_mid_pt]
y_min1 = y_min[prot>pv_mid_pt]
if maj_min == 1:
X_min = X[y == self.maj_label]
y_min = y[y == self.maj_label]
prot = X_min[:,prot_idx]
if prot_grp == 0:
X_min1 = X_min[prot<pv_mid_pt]
y_min1 = y_min[prot<pv_mid_pt]
if prot_grp == 1:
X_min1 = X_min[prot>pv_mid_pt]
y_min1 = y_min[prot>pv_mid_pt]
self.min_label = np.copy(self.maj_label)
if n_to_sample == 0:
return X.copy(), y.copy()
# fitting the model
n_neigh = min([len(X_min), self.n_neighbors+1])
nn = NearestNeighbors(n_neighbors=n_neigh, n_jobs=self.n_jobs)
nn.fit(X_min)
dist, ind = nn.kneighbors(X_min1)
if n_to_sample == 0:
return X.copy(), y.copy()
# generating samples
np.random.seed(seed=1)
base_indices = self.random_state.choice(list(range(len(X_min1))),
n_to_sample)
neighbor_indices = self.random_state.choice(list(range(1, n_neigh)),
n_to_sample)
X_base = X_min1[base_indices]
X_neighbor = X_min[ind[base_indices, neighbor_indices]]
samples = X_base + np.multiply(self.random_state.rand(n_to_sample,
1),
X_neighbor - X_base)
return (np.vstack([X, samples]),
np.hstack([y, np.hstack([self.min_label]*n_to_sample)]))
def get_params(self, deep=False):
return {'proportion': self.proportion,
'n_neighbors': self.n_neighbors,
'n_jobs': self.n_jobs,
'random_state': self._random_state_init}