-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCEA_DNFEA_functions.py
724 lines (579 loc) · 26.8 KB
/
CCEA_DNFEA_functions.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
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
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 16 13:29:23 2019
@author: jmatt
"""
import pandas as pd
import numpy as np
import scipy.special as sps
class parameter_container:
"""
An empty object used to pass variables and data between functions
"""
def __init__(self):
breakhere=1
def keys(self,prnt=True):
"""
Return a list of tuples of the variable names and types
INPUTS
prnt - (default = True) print the keys and types. False return
as a list of key/type tuples
OUTPUTS
keylist - a list of key/type tuples
"""
if prnt:
get_keys(self,prnt)
else:
return get_keys(self,prnt)
"""
END parameter_containiner class
"""
class CC_clause:
"""
An object to contain a single conjunctive clause
"""
def __init__(self,data,param,source_input_vector,clause_order):
"""
Initialize the clause based on the selected source_feature. Force the
target ranges for each variable to be such that the clause will match
the source feature
INPUTS
data - pandas dataframe of the input features and the outcome variable.
Missing values must be denoted with NaN
y - the name of the column in the pandas dataframe containing the
outcome variable
param - an object containing control parameters
source_input_vector - index of the feature used as a template for the CC
order - the order of the CC
VARIABLES
order - the number of features in the clause
target_class - the output target class the clause is trying to match
source_input_vector - the input vector used to build the clause
features - the features that are included in the clause
criteria - dataframe containing the matching criteria fro each feature
matches - dataframe containing:
1. boolean matches of each feature,
2. what input feature vectors are matched (the clause output)
3. Which values of the clause output match the data output
"""
#
# """
# remove next lines
# """
# source_input_vector = 6
#extract the data associated with the feature
self.order = clause_order
self.target_class = data.loc[source_input_vector,param.y]
self.input_vector_data = data.loc[source_input_vector,param.data_features]
#Find the candidates with valid values
candidate_features = list(self.input_vector_data[~self.input_vector_data.isna()].index)
#Randomly select clause_order features without replacement and store
#in self
self.source_input_vector = source_input_vector
self.features = list(np.random.choice(candidate_features,size=clause_order,replace=False))
#decide ranges
self.criteria = pd.DataFrame(dtype=object)
for feature in self.features:
#Extract the feature value for the source target input vector
feature_val = self.input_vector_data[feature]
criteria = self.generate_CC_criteria(data,param,feature,feature_val)
self.criteria[feature] = criteria[feature]
def generate_CC_criteria(self,data,param,feature,actual_feature_value):
"""
creates criteria for a newly added feature
pass actual_feature_value = np.nan if the actual feature value is
not available
"""
#Extract the max and min values of the feature
max_val = param.var_ranges.loc['max',feature]
min_val = param.var_ranges.loc['min',feature]
feature_type = param.var_ranges.loc['type',feature]
#create a blank dataframe to store the criteria
criteria = pd.DataFrame(data={feature:[np.nan,np.nan,np.nan]},index=['lb','ub','target'])
if feature_type == 'integer':
lb,ub = range_calc_integer(max_val,min_val,actual_feature_value)
criteria.loc['lb',feature] = lb
criteria.loc['ub',feature] = ub
elif feature_type == 'continuous':
#check if there is a feature value to match or not
missing_feature_val = pd.isnull(actual_feature_value)
if missing_feature_val:
feature_val = max_val
else:
feature_val = actual_feature_value
#select a lower bound between the min value and the feature
#value
lb = np.random.rand()*(feature_val-min_val)+min_val
#if the feature value is nan, set the lower range to the
#lower bound previously determined
if missing_feature_val:
feature_val = lb
#select an upper bound between the feature value and the max
#value
ub = np.random.rand()*(max_val-feature_val)+feature_val
criteria.loc['lb',feature] = lb
criteria.loc['ub',feature] = ub
elif feature_type == 'binary':
#check if there is a feature value to match or not
missing_feature_val = pd.isnull(actual_feature_value)
if missing_feature_val:
feature_val = np.random.choice(list(param.var_ranges.loc['set',feature]))
else:
feature_val = actual_feature_value
#If a valid feature value is input, there is only one choice for
#binary to ensure clause matches input feature vector
criteria.loc['lb',feature] = np.NaN
criteria = criteria.astype(object)
criteria.at['target',feature] = feature_val
elif feature_type == 'categorical':
#check if there is a feature value to match or not
missing_feature_val = pd.isnull(actual_feature_value)
if missing_feature_val:
target = set()
#Offset to reflect the fact that there is no actual value to
#include in the set
offset = 0
else:
#Force the value of the target input feature to be in the
#rule set
target = set([actual_feature_value])
#Offset to reflect the fact that the actual feature value
#is already in the set
offset = 1
#Store the set of all values
all_feature_values = param.var_ranges.loc['set',feature]
#The maximum number of values the rule can match is one less
#than the total number of values (or it would match everything
#and would not provide any useful info)
max_elements = len(all_feature_values)-offset
#Determine the number of elements from the value set to include
num_to_select = np.random.randint(max_elements)
#select the values to include in the rule set. NOTE: This may
#or may not include the value of the target input feature
selected_values = np.random.choice(list(all_feature_values),size=num_to_select,replace=False)
#Add the previously selected values to the rule set
target = target.union(selected_values)
#Store in self.criteria
criteria.loc['lb',feature] = np.NaN
criteria = criteria.astype(object)
criteria.at['target',feature] = target
else:
print('ERROR: unknown feature type ({}) for feature {}'.format(feature_type,feature))
return criteria
def identify_matches(self,data,param,features_to_update = 'all'):
"""
Identifies the input feature vectors that the clause matches
"""
#if all, update all features
if features_to_update == 'all':
features_to_update = self.features
#Check if dictionary of matches exists, if it doesn't, create
try: self.matches
except: self.matches = pd.DataFrame()
#For all features requiring match testing
for feature in features_to_update:
feature_type = param.var_ranges.loc['type',feature]
if (feature_type == 'integer')|(feature_type == 'continuous'):
#The value is greater than or equal to the lower bound
lb_matches = data[feature]>=self.criteria.loc['lb',feature]
#The value is less than or equal to the upper bound
ub_matches = data[feature]<=self.criteria.loc['ub',feature]
#the value is in [lb ub]
self.matches[feature] = lb_matches&ub_matches
elif feature_type == 'binary':
self.matches[feature] = data[feature]==self.criteria.loc['target',feature]
elif feature_type == 'categorical':
self.matches[feature] = data[feature].isin(self.criteria.loc['target',feature])
else:
print('ERROR: unknown feature type ({}) for feature {}'.format(feature_type,feature))
np.zer
#True for input feature vectors where all features are matched
self.matches['clause_match'] = self.matches[self.features].all(axis=1)
#The predicted output class for clause matches, np.nan for non-matches
self.matches.loc[self.matches['clause_match'],'output_class']=self.target_class
#boolean match of output class
self.matches['match_data_output'] = self.matches['output_class'] == data[param.y]
def update_Nk(self,data,param):
"""
Updates the number of input feature vectors with no missing data for the
given clause for each output class.
INPUTS
data - pandas dataframe of the input features and the outcome variable.
Missing values must be denoted with NaN
param
.y - the name of the column in the pandas dataframe containing the
outcome variable
"""
#matches segregated by output class
self.Nk_all = calc_Nk(data,param,self.features)
#Number of input feature vectors with no missing data for the features
#of the clause and with which matches the target output class of the
#clause
self.Nk = self.Nk_all[self.target_class]
#total number of input feature vectors with no missing data for the
#features of the clause regardless of output class
self.Ntot = sum(self.Nk_all.values())
def update_Nmatchk(self,data,param):
"""
Finds the number of matches for the target output class and the total
number of clause matches
"""
#Number of clause matches that correctly predict the output class
self.Nmatch_k = sum(self.matches['match_data_output'])
#total number of input feature vectors that the clause matches
self.Nmatch = sum(self.matches['clause_match'])
def ratio_test(self):
"""
docstring
"""
#Correct matches divided by all matches
fraction_correct = self.Nmatch_k/self.Nmatch
#number of input feature vectors for a certain class divided by the
#total number of input feature vectors. In both cases, use the number
#of input feature vectors with no missing data
class_fraction = self.Nk/self.Ntot
if fraction_correct<class_fraction:
self.pass_ratio_test = False
else:
self.pass_ratio_test = True
def calc_fitness(self):
"""
Calculates the fitness of the CC using the hypergeometric probability
distribution
"""
#NOTE: "valid" input feature vectors = the input feature vectors
#without any missing values for the features present in the clause
#number of ways the correct (true positive) clause matches could be
#selected from the number of valid input feature vectors for class k
part1 = sps.binom(self.Nk,self.Nmatch_k)
#the number of ways the number of incorrect (false positive) clause
#matches could be selected from the number of valid input feature
#vectors that have a class other than k
part2 = sps.binom((self.Ntot-self.Nk),(self.Nmatch-self.Nmatch_k))
#The number of ways the clause matches (regardless of class) could be
#selected from the number of valid input feature vectors
part3 = sps.binom(self.Ntot,self.Nmatch)
#Check that part3 is non-zero
if part3==0:
self.fitness = 10
print('\n\nWARNING: invalid fitness denominator')
print(' Ntot = {}, Nmatch = {}'.format(self.Ntot,self.Nmatch))
print(self.Ntot)
print(self.criteria)
else:
#The probability that the observed association between the clause and
#the target class k is due to chance
self.fitness = part1*part2/part3
def update_fitness(self,data,param,features_to_update = 'all'):
"""
"""
self.identify_matches(data,param,features_to_update)
self.update_Nk(data,param)
self.update_Nmatchk(data,param)
self.ratio_test()
self.calc_fitness()
def drop_feature(self,data,param,features_to_drop):
"""
removes a feature or a list of features from the clause
"""
#Drop the values from the criteria
self.criteria.drop(columns=features_to_drop,inplace=True)
#Drop the values from the matches dataframe
self.matches.drop(columns=features_to_drop,inplace=True)
#Update the features list
self.features = [val for val in self.features if not val in features_to_drop]
#Do not update the matches for any features, but update the
#global clause-matching variable, the output class variable,
#and test for matching the output class
self.identify_matches(data,param,[])
#Update the number of input feature vectors with no missing data
#for the features in the clause
self.update_Nk(data,param)
#Update the mach counts
self.update_Nmatchk(data,param)
#recalculate the fitness
self.calc_fitness()
#Update the clause order
self.order = len(self.features)
def keys(self,prnt=True):
"""
Return a list of tuples of the variable names and types
INPUTS
prnt - (default = True) print the keys and types. False return
as a list of key/type tuples
OUTPUTS
keylist - a list of key/type tuples
"""
if prnt:
get_keys(self,prnt)
else:
return get_keys(self,prnt)
"""
END OF CC_Clause object
"""
def range_calc_integer(max_val,min_val,actual_feature_val):
"""
Generates a range with integer start & end points that:
1. does not contain both the minimum and the maximum value in the
data, and
2. contains the feature value
INPUTS
max_val - the maximum value in the data
min_val - the minimum value in the data
feature_val - the value of the current feature for the source vector
OUTPUTS
The lower and upper bounds of the range
"""
#check if there is a feature value to match or not
missing_feature_val = pd.isnull(actual_feature_val)
if missing_feature_val:
feature_val = max_val
else:
feature_val = actual_feature_val
#Define a continuous range beginning and ending on integers
#Select a lower bound on the integers in [min_val,feature_val]
lb = np.random.randint(min_val,high=feature_val+1)
#If the feature value is missing, set the lower limit to the previously
#determined lb
if missing_feature_val:
feature_val = lb
#Select an upper bound on the integers in [lb,max_val]
ub = np.random.randint(feature_val,high=max_val+1)
#If the selected bounds contain the entire range (which would
#indicate that the variable would have no contribution towards
#discriminating the output), randomly select either the upper
#or lower bound to shift.
if (lb==min_val)&(ub == max_val):
#determine which bound to adjust
if feature_val == min_val:
decrease_ub = True
elif feature_val == max_val:
decrease_ub = False
elif np.random.rand()>0.5:
decrease_ub = True
else:
decrease_ub = False
#Adjust the bound
if decrease_ub:
#Calculate the adjustment as a random integer between 1 and
#the difference between the min and value
adjustment = np.random.randint(max_val-feature_val)+1
ub-=adjustment
else:
#Calculate the adjustment as a random integer between
#the difference between the min and value
adjustment = np.random.randint(feature_val-min_val)+1
lb+=adjustment
return lb,ub
def find_ranges(data,param):
"""
Finds the ranges of each input variable and returns a dataframe of the min
and max values
INPUTS
data - pandas dataframe of the input features and the outcome variable.
Missing values must be denoted with NaN
param
.y - the name of the column in the pandas dataframe containing the
outcome variable
OUTPUTS
var_ranges - pandas dataframe containing the variable ranges
"""
#Find the variable column names
data_cols = [x for x in data.keys() if not x==param.y]
#Determine if the variable is binary, integer, or continuous as a way
#to reduce the search space
var_ranges = pd.DataFrame()
var_to_drop = []
for var in data_cols:
#Get the set of unique values for the current variable
unique_vals = set(data.loc[~data[var].isna(),var])
#If there are less than 2 unique values, warn user of useless variable
#and add variable to the droplist
if len(unique_vals)<2:
print('WARNING:')
print(' Variable {} contains {} unique, non-NaN values and contains no useful information'.format(var,len(unique_vals)))
print(' Dropping Variable {} from the dataset'.format(var))
var_to_drop.append(var)
#If there are two unique values, the variable is binary
elif len(unique_vals)==2:
values = list(unique_vals)
var_ranges.loc['max',var] = values[0]
var_ranges.loc['min',var] = values[1]
var_ranges.loc['type',var] = 'binary'
var_ranges.at['set',var] = unique_vals
else:
#test to see if values are numeric
try:
t = np.array(list(unique_vals))+2
is_numeric = True
except:
is_numeric = False
#If the values are all numeric, find the min and max, and categorize
#as binary, integer, or continuous
if is_numeric:
var_ranges.loc['max',var] = max(unique_vals)
var_ranges.loc['min',var] = min(unique_vals)
#Convert the unique values to integers; if the integer values are
#equal to the un-converted values, then the variable is contains
#only integers. Otherwise, call the variable a continuous variable
unique_int = [int(x) for x in unique_vals]
zipped = list(zip(unique_vals,unique_int))
if all([x==y for x,y in zipped]):
var_ranges.loc['type',var] = 'integer'
var_ranges.at['set',var] = unique_vals
else:
var_ranges.loc['type',var] = 'continuous'
else:
var_ranges.loc['max',var] = np.NaN
var_ranges.loc['min',var] = np.NaN
var_ranges.loc['type',var] = 'categorical'
var_ranges.at['set',var] = unique_vals
print('WARNING: \n Variable {} contains non-numeric data.'.format(var))
data.drop(columns=var_to_drop,inplace=True)
return var_ranges
def gen_CC_clause_pop(data,param,new_pop,CC_stats):
"""
Generates a random conjunctive clause given a set of variable ranges
INPUTS
var_ranges - pandas dataframe of variable ranges. column names are
variable names and the min/max rows are labled as 'min' and 'max
respectively
OUTPUTS
clause - a randomly generated clause
"""
new_pop_list = []
for i in range(new_pop):
clause_order = np.random.randint(param.num_features)+1
candidate_mask = param.feature_order>=clause_order
cand_match_counts = CC_stats.matched_input_vectors[candidate_mask]
source_input_vector = sel_input_vector(cand_match_counts)
new_pop_list.append(CC_clause(data,param,source_input_vector,clause_order))
new_pop_list[-1].update_fitness(data,param)
return new_pop_list
def sel_input_vector(cand_match_counts):
"""
Selects an input feature as the 'template'. The probability of a feature
being selected is inversely proportional to the number of clauses it is
matched by
INPUTS
CC_stats
.matched_input_vectors - a pandas series containing the match
counts
candidates - the input feature vectors that have enough non-NaN values
to build a clause of the desired order
OUTPUTS
feature - the index of the selected feature
"""
#Find the number of times input feature vectors are matched
num_matched = sum(cand_match_counts)
#If none are matched, use a uniform probability of selection
if num_matched == 0:
selection_PMF = (1-cand_match_counts)/sum(1-cand_match_counts)
else:
#The probability mass function that a feature is matched
match_PMF = cand_match_counts/sum(cand_match_counts)
#The probability mass function that a feature will be selected
selection_PMF = (1-match_PMF)/sum(1-match_PMF)
#The cumulative distribution function for selection
selection_CDF = selection_PMF.cumsum()
#Randomly select a feature
rand = np.random.rand()
input_vector = list(selection_CDF[selection_CDF>=rand].index)[0]
return input_vector
def find_max_input_feature_order(data,param):
"""
Finds the number of non-missing data in each of the input feature vectors
INPUTS
data - pandas dataframe of the input features and the outcome variable.
Missing values must be denoted with NaN
param
.y - the name of the column in the pandas dataframe containing the
outcome variable
OUTPUTS
feat_order - pandas series containing the order of each feature vector
"""
#Find the variable column names
data_cols = [x for x in data.keys() if not x==param.y]
feat_order = len(data_cols) - data[data_cols].isna().sum(axis=1)
return feat_order
def calc_Nk(data,param,clause_features):
"""
Finds the number of input feature vectors with no missing data for the
given clause for each output class.
INPUTS
data - pandas dataframe of the input features and the outcome variable.
Missing values must be denoted with NaN
param
.y - the name of the column in the pandas dataframe containing the
outcome variable
clause_features - List of variables included in the current conjunctive clause
(for a CCEA) or disjunctive normal form clause (for a DNFEA)
OUTPUTS
Nk - dict containg the number of complete input feature vectors for
each output class
"""
#find all NaN values
is_missing = data[clause_features].isna()
#Mask all rows missing at one or more value
is_missing = is_missing.any(axis=1)
#Extract the non-missing output values
nm = data.loc[~is_missing,param.y]
#Find the number of complete input feature vectors for each class
class_counts = nm.value_counts()
#Find the set of classes in the full dataset (required in case there is a
#class for which all input feature vectors are missing at least one value)
classes = set(data[param.y])
Nk = {}
#For each class, try to add the count to the dictionary, otherwise set the
#count to zero
for k in classes:
try: Nk[k] = class_counts[k]
except: Nk[k] = 0
return Nk
def calc_Nmatchk(data,param,clause):
"""
Finds the number of input feature vectors with no missing data for the
given clause for each output class.
INPUTS
data - pandas dataframe of the input features and the outcome variable.
Missing values must be denoted with NaN
param
.y - the name of the column in the pandas dataframe containing the
outcome variable
clause_features - List of variables included in the current conjunctive clause
(for a CCEA) or disjunctive normal form clause (for a DNFEA)
OUTPUTS
Nk - dict containg the number of complete input feature vectors for
each output class
"""
t=1
def get_keys(dct,prnt):
"""
Return a list of tuples of the variable names and types
INPUTS
prnt - (default = True) print the keys and types. False return
as a list of key/type tuples
OUTPUTS
keylist - a list of key/type tuples
"""
keylist = sorted([(x,type(dct.__dict__[x])) for x in dct.__dict__.keys()])
if prnt:
#print column width
CW = 40
#print header
print('{: ^{CW}}|{: ^{CW}}'.format(
'Variable',
'Type',
CW=CW))
print('{:-^{CW}}|{:-^{CW}}'.format(
'-',
'-',
CW=CW))
#Print sorted keys
for name,v_type in keylist:
print('{: ^{CW}}| {: <{CW}}'.format(
name,
str(v_type),
CW=CW))
else:
return keylist