-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzyTSModel_discriminator.py
1225 lines (735 loc) · 49.1 KB
/
fuzzyTSModel_discriminator.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
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 6 14:53:40 2021
@author: Admin
"""
import pandas as pd
import numpy as np
import skfuzzy as fuzz
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import StratifiedKFold
from scipy.optimize import least_squares, minimize, basinhopping, LinearConstraint, curve_fit
import matplotlib.pyplot as plt
from sklearn.metrics import f1_score, confusion_matrix
import scipy as sp
import os
from sklearn import metrics
import numpy.matlib
from sklearn.utils.class_weight import compute_class_weight
from scipy.interpolate import interp1d
from imblearn.over_sampling import RandomOverSampler
#This script corresponds to the parameter estimation of the TS fuzzy model
from random import randint
from sklearn.model_selection import StratifiedShuffleSplit
from scipy.signal import hilbert
from sklearn.metrics import roc_auc_score,roc_curve
from sklearn.metrics import average_precision_score, precision_recall_curve
from scipy.spatial.distance import cdist
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import FeatureUnion
from sklearn.compose import ColumnTransformer
from sklearn.decomposition import PCA
# Transformer method for this transformer
class CategoricalTransformer(BaseEstimator, TransformerMixin):
def __init__(self):
super().__init__()
# Return self nothing else to do here
def fit(self, X, y=None):
categoricalVariables = X.columns
#Iterate over the columns
dictVariables = {}
for colName in categoricalVariables:
uniqueVal, counts = np.unique( X[colName] , return_counts=True)
frequencyRelative = ( counts/ np.sum(counts) ) #+np.finfo(float).eps
entropyVariable = -1*np.dot(frequencyRelative, np.log2( frequencyRelative ) )
dictValues = {}
for iUnique in range( len( uniqueVal) ):
individualValue = ( counts[ iUnique ] / np.sum(counts) ) * -np.log2( counts[ iUnique ] / np.sum(counts) )
dictValues[ uniqueVal[ iUnique ] ] = individualValue/entropyVariable
dictVariables[colName] = dictValues
self.dictVariables = dictVariables
return self
# Transformer method for this transformer
def transform(self, X, y=None):
XsetModified = X.copy()
categoricalVariables = X.columns
for colName in categoricalVariables:
dictValues = self.dictVariables[colName]
XsetModified[colName] = 0
for keyValue in dictValues:
XsetModified.loc[ X[colName] == keyValue , colName] = dictValues[keyValue]
return XsetModified
class FuzzyModelDiscriminator(object):
#mCluster usually between 1.1 and 2
#Array exponentiation applied to the membership function u_old at each iteration, where
# a higher mCluster number will increase fuzziness, correspoding to smaller partining coefficients
# that means fewer cluster and more overlapping between the clusters...that is useful when the
#distorsion want to be measured .. how much a sample look similar to another class
mCluster = 2#
oversampling = 1
# Operation for finding membership values across variables
#minOperation 1, the min would be used...otherwise the np.prod is used
minOperation = 0
# Threshold for simplifying fuzzy rules
thrSimplyRules = 1.95
thrSimplyUniverse = 2
typeMF = 1# 1: gaussian; 2:exponential
def getConvexEnvelope( self, xin, mfin, norm=1, nc=1000 ):
# Calculates the convex membership function that envelopes a given set of
# data points and their corresponding membership values.
# Input:
# Xin: N x 1 input domain (column vector)
# MFin: N x 1correspoding membership values
# nc: number of alpha cut values to consider (default=101)
# norm: optional normalization flag (0: do not normalize, 1 : normalize,
# default=1)
#
# Output:
# mf: membership values of convex function
# x: output domain values
# Normalize the membership values (if requested)
if norm == 1:
maxVal = np.max(mfin)
if maxVal==0:
maxVal=np.finfo(float).eps
mfin = np.divide(mfin, maxVal)
# Initialize auxilary variables
acut = np.linspace(0,np.max(mfin),nc)
mf= np.full(2*nc, np.nan)
x=np.full(2*nc, np.nan)
if np.any(mfin>0):
x[0] = np.min(xin[mfin>0])
x[nc]=np.max(xin[mfin>0])
mf[0]=0
mf[nc] = 0
# Determine the elements in the alpha cuts
for i in range(0,nc):
if np.any(mfin>acut[i]):
x[i]=np.min(xin[mfin>acut[i]])
x[i+nc]=np.max(xin[mfin>acut[i]])
mf[i]=acut[i]
mf[i+nc]=acut[i]
#Delete NaNs
idx=np.isnan(x)
x=x[idx==False]
mf=mf[idx==False]
# Sort vectors based on membership value (descending order)
indmf=mf.argsort(axis=0)
indmf=np.flipud(indmf)
mf=mf[indmf]
x=x[indmf]
# Find duplicate values for x and onlykeep the ones with the highest membership value
_, ind = np.unique(x, return_index=True, return_inverse=False, return_counts=False, axis=None)
mf=mf[ind]
x=x[ind]
# Sort vectors based on x value (ascending order)
indx=x.argsort(axis=0)
mf=mf[indx]
x=x[indx]
xval=np.linspace(np.min(x),np.max(x),nc)
mf=np.interp(xval, x, mf, left=None, right=None, period=None)
x=xval;
return mf, x
def findParametersExponential(self, x, y ):
#Find the convex envelope
mf, xMf = self.getConvexEnvelope( x, y, norm=1, nc=1000 )
# The input is the envelop of the projections
# Smoothing the envelop using bins
# Taking the mean of the values inside the bin
xMfSet = []
mfSet = []
medianPeriod = np.median( np.diff( np.sort(xMf) ) )
for idx in range( len(xMf) ):
startWind = xMf[idx] - ( medianPeriod/2 )
endWind = xMf[idx] + ( medianPeriod/2 )
if idx == len(xMf)-1:
countPoints = np.sum( (x >= startWind) & (x <= endWind) )
else:
countPoints = np.sum( (x >= startWind) & (x < endWind) )
xMfSet += [ xMf[idx] ]*countPoints
mfSet += [ mf[idx] ]*countPoints
xMfSet = np.asarray( xMfSet )
mfSet = np.asarray( mfSet )
if self.typeMF == 1:
mu = sum(xMfSet * mfSet) / sum(mfSet)
mfSet[mfSet==0] = np.finfo(np.float64).eps
sig = np.mean(np.sqrt(-((xMfSet-mu)**2)/(2*np.log(mfSet))))
if np.min(xMfSet ) == np.max(xMfSet ):
param, _ = curve_fit(self._gaussmf, xMfSet, mfSet, p0 = [ np.min(xMfSet ), np.finfo(float).eps ],
bounds=( (-np.inf, np.finfo(float).eps ),
(np.inf, np.inf ) ), maxfev = 10000, jac = self.funJacExpontentialCurveOneSide)
else:
param, _ = curve_fit(self._gaussmf, xMfSet, mfSet, p0 = [mu, sig],
bounds=( (np.min(xMfSet ), np.finfo(float).eps ),
(np.max(xMfSet ) , np.max([ np.max(xMfSet )-np.min(xMfSet ), sig ]) ) ), maxfev = 10000,
jac = self.funJacExpontentialCurveOneSide)
popt = np.zeros(4)
popt[:2]= param
popt[2:]= param
else:
mu1 = xMf[mf>=0.97][0]
mu2 = xMf[mf>=0.97][-1]
xmf = xMf
sig1 = (mu1 - xmf[0])/(np.sqrt(-2*np.log(mf[0])))
sig2 = (xmf[-1]-mu2)/(np.sqrt(-2*np.log(mf[-1])))
if sig1==0.0:
sig1=0.1
if sig2==0.0:
sig2=0.1
try:
popt, pcov = curve_fit(self.evalulateExponentialFunction, xMfSet , mfSet, p0 = [mu1, sig1, mu2, sig2],
bounds=((-np.inf, np.finfo(float).eps,-np.inf, np.finfo(float).eps ),
(np.inf, np.inf, np.inf, np.inf),),
maxfev=1000,
jac = self.funJacExpontentialCurve )
except:
popt, pcov = curve_fit(self.evalulateExponentialFunction, xMfSet , mfSet, p0 = [mu1, sig1, mu2, sig2],
bounds=((-np.inf, np.finfo(float).eps,-np.inf, np.finfo(float).eps ),
(np.inf, np.inf, np.inf, np.inf) ),
maxfev=100000,)
return popt
def _gaussmf(self,x, mu, sigma, a=1):
# x: (1D array)
# mu: Center of the bell curve (float)
# sigma: Width of the bell curve (float)
# a: normalizes the bell curve, for normal fuzzy set a=1 (float)
return a * np.exp(-(x - mu)**2 / (2 * sigma**2))
def evalulateExponentialFunction( self, x, cL, wL, cR, wR):
if wR==0:
wR = np.finfo(float).eps
if wL==0:
wL = np.finfo(float).eps
yPred = np.ones( len(x) )
if cR - cL >= -1e-10 :
yPred = np.ones( len(x) )
idxLowerCl = np.argwhere( x < cL )
yPred[ idxLowerCl ] = fuzz.gaussmf( x[ idxLowerCl] , cL, wL)
idxLowerCr = np.argwhere(x> cR )
yPred[ idxLowerCr] = fuzz.gaussmf( x[ idxLowerCr] , cR, wR)
else:
yPred = yPred*100
return yPred
def funJacExpontentialCurve( self, x, cL, wL, cR, wR):
if wR==0:
wR = np.finfo(float).eps
if wL==0:
wL = np.finfo(float).eps
yPred = self.evalulateExponentialFunction( x, cL, wL, cR, wR)
yJac = np.zeros( [ len(yPred), 4] )
for idx in range( x.shape[0] ):
currentX = x[idx]
if currentX < cL:
yJac[idx, 0] = yPred[idx] * ( ( currentX- cL ) / wL**2 )
yJac[idx, 1] = yPred[idx] * ( ( currentX- cL )**2/ wL**3 )
if currentX > cR:
yJac[idx, 2] = yPred[idx] * ( ( currentX- cR )/ wR**2 )
yJac[idx, 3] = yPred[idx] * ( ( currentX- cR )**2/ wR**3)
return yJac
def funJacExpontentialCurveOneSide( self, x, c, w):
if w==0:
w = np.finfo(float).eps
yPred = self._gaussmf(x, c, w)
yJac = np.zeros( [ len(yPred), 2] )
for idx in range( x.shape[0] ):
currentX = x[idx]
yJac[idx, 0] = yPred[idx] * ( ( currentX- c ) / w**2 )
yJac[idx, 1] = yPred[idx] * ( ( currentX- c )**2/ w**3 )
return yJac
def findingTotalClusters( self, Xtrain, yTrain, numerical_columns, categorical_columns ):
#First step is to estimate how many logic rules would be in the model
#1st normalized the input
Xnormalized = self.normalizingInputData( Xtrain, numerical_columns, categorical_columns )
Z = np.hstack( (Xnormalized, yTrain[:,np.newaxis] ) )
#Normalizing data
scalerZ = MinMaxScaler() #for the response
if self.oversampling == 1:
#after normalizing, resample
ros = RandomOverSampler()
#At this point y is not normalized
X_sampled, y_sampled = ros .fit_resample(Z[ :, :-1], yTrain)
#normalizing Y to have same scale in the clustering
y_sampled_normalized = scalerZ.fit_transform( y_sampled[:,np.newaxis] )
Z = np.hstack( ( X_sampled , y_sampled_normalized ) )
alldata = Z.T
else:
alldata = Z.T
fpcs = []
totalCentersToCheck = 5
for ncenters in range(2, totalCentersToCheck+1 ):
#Applying the c-means using different centers
#m Array exponentiation applied to the membership function u_old at each iteration, where U_new = u_old ** m.
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(
alldata, ncenters, m= self.mCluster, error=1e-6, maxiter=10000, init=None)
fpcs.append(fpc)
#print('fpcs', fpcs)
#Finding which center achieve the best metric
centers = range(2, totalCentersToCheck+1 )
bestCentersIdx = np.argmax( fpcs )
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans( alldata,
centers[bestCentersIdx],
m=self.mCluster, error=1e-6, maxiter=10000, init=None)
if self.oversampling == 1:
u = u[ :,:Xtrain.shape[0] ]
print( 'number of clusters (rules) ', centers[bestCentersIdx] )
#Returning number of clusters
return centers[bestCentersIdx], u
def trainFuzzyModel( self, Xtrain, yTrain, clusters=None, plotFun=0):
if clusters == None:
#If there is not a number of clusted defined, it is determined
#on the training set.
clusters, u = self.findingTotalClusters( Xtrain, yTrain)
else:
Z = np.hstack( (Xtrain, yTrain[:,np.newaxis] ) )
#Normalizing data
scalerZ = MinMaxScaler()
normaliedData = scalerZ.fit_transform( Z )
if self.oversampling == 1:
#after normalizing, resample
ros = RandomOverSampler()
X_sampled, y_sampled = ros .fit_resample(normaliedData[ :, :-1], yTrain)
y_sampled_normalized = scalerZ.fit_transform( y_sampled[:,np.newaxis] )
Z = np.hstack( ( X_sampled , y_sampled_normalized ) )
alldata = Z.T
else:
alldata = normaliedData.T
_, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans( alldata, clusters,
m=self.mCluster, error=1e-6, maxiter=10000, init=None)
if self.oversampling == 1:
u = u[ :,:Xtrain.shape[0] ]
scalerTrain = MinMaxScaler()
normalizedData = scalerTrain.fit_transform( Xtrain )
parametersCurves = self.findingMembershipFunctions( normalizedData, yTrain, u, plotFun )
parametersCurves_simplify = self.simplifyMembershipFunctions( parametersCurves.copy() , .95, .99 )
consequentRules = self.calculatingConsecuents( u, yTrain, 0 )
return parametersCurves_simplify, consequentRules, scalerTrain
def trainFuzzyModelClasses( self, Xtrain, yTrain, selectedFeatures, numericalVariables, categoricalVariables, clusters=None, plotFun=0):
if clusters == None:
#If there is not a number of clusted defined, it is determined
#on the training set.
clusters, _ = self.findingTotalClusters( Xtrain, yTrain)
Xnormalized = self.normalizingInputData( Xtrain, numericalVariables, categoricalVariables )
Z = np.hstack( (Xnormalized, yTrain[:,np.newaxis] ) )
#Normalizing data
if self.oversampling == 1:
#after normalizing, resample
ros = RandomOverSampler()
#Resampling using original labels
X_sampled, y_sampled = ros.fit_resample(Z[ :, :-1], yTrain )
scalerY = MinMaxScaler()
y_sampled_normalized = scalerY.fit_transform( y_sampled[:,np.newaxis] )
Z = np.hstack( ( X_sampled , y_sampled_normalized ) )
alldata = Z.T
else:
alldata = Z.T
centroids, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans( alldata, clusters,
m=self.mCluster, error=1e-6, maxiter=10000, init=None)
#Denormalizing centroids
scalerZ = MinMaxScaler()
scalerZ.fit_transform( Xtrain[numericalVariables] )
#only for numerical variables
centroidsDeNorm = np.zeros( centroids.shape)
centroidsDeNorm[ :, :len( numericalVariables) ] = scalerZ.inverse_transform( centroids[:, : len( numericalVariables) ] )
#minValueFeatures = scalerZ.data_min_
if self.oversampling == 1:
u = u[ :,:Xtrain.shape[0] ]
# print(u)
#converting dataframe to numpy
#Xtrain = Xtrain[numericalVariables+categoricalVariables].to_numpy()
Xtrain = Xtrain.to_numpy()
numericalIndexes = np.arange( len(numericalVariables) )
parametersCurves, dicCategoricalVar = self.findingMembershipFunctions( Xtrain, u, centroidsDeNorm, numericalIndexes )
xMin = np.min(Xtrain, axis=0 )
xMax = np.max( Xtrain, axis=0)
parametersCurves_simplify = self.simplifyMembershipFunctions( parametersCurves.copy() , xMin,
xMax, numericalIndexes, self.thrSimplyRules, self.thrSimplyUniverse )
idxClass0 = np.where( yTrain==0 )[0]
idxClass1 = np.where( yTrain==1 )[0]
Xtrain0 = Xtrain[idxClass0, 5]
Xtrain1 = Xtrain[idxClass1, 5]
uClass0 = u[:, idxClass0]
uClass1 = u[:, idxClass1]
consequentRules = self.calculatingConsecuents( u, yTrain, 0 )
_, _, activationPerClassGroup = self.predict( Xtrain, yTrain, selectedFeatures, parametersCurves_simplify, dicCategoricalVar,
numericalVariables, categoricalVariables, numericalIndexes, consequentRules )
fpr, tpr, thresholds = roc_curve(yTrain, activationPerClassGroup[:,1])
# calculate the g-mean for each threshold
J = tpr - fpr
ix_J = np.argmax(J)
gMeans = np.sqrt(tpr*(1 - fpr))
ix = np.argmax(gMeans)
return parametersCurves_simplify, dicCategoricalVar, consequentRules, thresholds[ix]
def predictMembershipValue( self, Xtrain, parametersCurves, dictCategorical, idxNumericalVariables, featuresIdx):
totalRules, totalFeatures, totalParameters = parametersCurves.shape
MembershipMatrix = np.empty([ totalRules, Xtrain.shape[0], len(featuresIdx) ])
Betas = np.empty([ totalRules, Xtrain.shape[0]] )
for iRule in range( totalRules ):
idxFeature = 0
for iVariable in featuresIdx:
if iVariable in idxNumericalVariables:
#a,b,c = parametersCurves[ iRule, iVariable, : ]
#, width = parametersCurves[ iRule, iVariable, : ]
cL, wL, cR, wR = parametersCurves[ iRule, iVariable, : ]
MembershipMatrix[iRule, :, idxFeature] = self.evalulateExponentialFunction( Xtrain[:, iVariable], cL, wL, cR, wR)
else:
#This is a categorical variable
for idX in range(Xtrain.shape[0]):
if Xtrain[idX,iVariable] in dictCategorical[ iVariable][iRule] :
#Getting the value for each sample
MembershipMatrix[iRule, idX, idxFeature] = dictCategorical[ iVariable][iRule][ Xtrain[idX,iVariable] ]
else:
MembershipMatrix[iRule, idX, idxFeature] = np.finfo(float).eps
idxFeature+=1
Betas[iRule,:] = np.prod( MembershipMatrix[iRule, :, :], axis=1)
return Betas
def predict( self, Xtest, yTest, selectedFeatures, parametersCurves, dicCategoricalVar, numericalVariables, categoricalVariables, numericalIndexes, consequentRules, thresholdClass=.5 ):
if isinstance(Xtest, pd.DataFrame ) :
Xtest = Xtest.to_numpy()
membership = self.predictMembershipValue(Xtest, parametersCurves, dicCategoricalVar, numericalIndexes, selectedFeatures)
membership = membership/( np.finfo(float).eps+ np.sum(membership, axis=0) )
activationPerClass = np.dot( membership.T, consequentRules )
yHat = np.zeros( len(yTest) )
yHat[ activationPerClass[:,1] >= thresholdClass ] = 1
accuracy = metrics.f1_score( yTest, yHat, average='weighted')
return accuracy, yHat, activationPerClass
def trainFuzzyModelClassesCategorical( self, Xtrain, yTrain, dictionaryCategorical, categoricalVariables ):
numericalVariables = [ iVar for iVar in range(Xtrain.shape[1]) if iVar not in categoricalVariables ]
parameterModels = {}
for key in dictionaryCategorical:
if key == 'all':
Xgroup = Xtrain[:, numericalVariables] #Only the numeric variables
yGroup = yTrain
selectedFeatures = dictionaryCategorical[key][0]
clusters = dictionaryCategorical[key][1]
parametersCurves_simplify, consequentRules, scalerTrain, thresholdClass = self.trainFuzzyModelClasses( Xgroup, yGroup, selectedFeatures, clusters )
# _, yHatGroup, activationPerClassGroup = self.predict( Xgroup, yGroup, selectedFeatures, parametersCurves_simplify, consequentRules )
# #rocVal = roc_auc_score(y_train, activationPerClassGroup[:,1], average='weight')
# fpr, tpr, thresholds = roc_curve(yGroup, activationPerClassGroup[:,1])
# # plt.plot( fpr, tpr, '.-')
# # plt.show()
# # calculate the g-mean for each threshold
# gmeans = np.sqrt(tpr * (1-fpr))
# ix = np.argmax(gmeans)
parameterModels[key] = [ parametersCurves_simplify, consequentRules, scalerTrain, thresholdClass ]
else:
splitValues = key.split('_')
idxVar = 0
idxSel = np.zeros([Xtrain.shape[0], len(categoricalVariables) ] )
for iCatVar in categoricalVariables:
idxSel[ int(splitValues[ idxVar]) == Xtrain[ :, iCatVar ], idxVar ] = 1
idxVar+=1
idxSelGroup = np.prod(idxSel, axis=1)
Xgroup = Xtrain[ idxSelGroup == 1, : ]
Xgroup = Xgroup[:, numericalVariables] #Only the numeric variables
yGroup = yTrain[ idxSelGroup == 1 ]
selectedFeatures = dictionaryCategorical[key][0]
clusters = dictionaryCategorical[key][1]
parametersCurves_simplify, consequentRules, scalerTrain,thresholdClass = self.trainFuzzyModelClasses( Xgroup, yGroup, selectedFeatures, clusters )
parameterModels[key] = [ parametersCurves_simplify, consequentRules, scalerTrain, thresholdClass ]
return parameterModels
def findingMembershipFunctions( self, Xtrain, u, centroids, idxNumericalVariables, plotFun=0 ):
# number of features
totalFeatures = Xtrain.shape[1]
totalRules = u.shape[0]
#Normalized feature set
normalizedData = Xtrain# scalerZ.fit_transform( Xtrain )
#To store parameters of the membership fucntions
numberParamters = 4
parametersCurves = np.empty( [ totalRules, totalFeatures, numberParamters] )
dictCategoricalVar = {}
clusterPerPoint = np.argmax( u , axis= 0 )
# Each cluster has a membership function for each feature
for iRule in range( totalRules ):
#Membership functions of the rules are determined
#Using the only the points of the cluster
dataCluster = normalizedData[ clusterPerPoint==iRule, : ]
uCluster = u[ :, clusterPerPoint==iRule ]
for iVariable in range( totalFeatures ):
uniqueValues = np.unique( dataCluster[ :, iVariable ] )
projectionValue = np.empty( len( uniqueValues ) )
if iVariable in idxNumericalVariables:
if self.typeMF == 1:
iqr = (np.quantile( dataCluster[:, iVariable], .75) - np.quantile( dataCluster[:, iVariable], .25) )
if iqr!=0:
fstOut = np.quantile( dataCluster[:, iVariable], .25) - (iqr*3)
trdOut = np.quantile( dataCluster[:, iVariable], .75) + (iqr*3)
idxThrSamples = np.where( (dataCluster[:,iVariable] >= fstOut) & (dataCluster[:,iVariable] <= trdOut) )[0]
else:
lowerOut = np.quantile( dataCluster[:, iVariable], .01, interpolation='lower') #- np.finfo(float).eps
upperOut = np.quantile( dataCluster[:, iVariable], .99, interpolation='higher') #+ np.finfo(float).eps
idxThrSamples = np.where( (dataCluster[:,iVariable] >= lowerOut) & (dataCluster[:,iVariable] <= upperOut) )[0]
expParameters2 = self.findParametersExponential( dataCluster[idxThrSamples, iVariable], uCluster[iRule,idxThrSamples] )
parametersCurves[ iRule, iVariable,: ] = expParameters2
else:
expParameters = self.findParametersExponential( dataCluster[:, iVariable], uCluster[iRule,:] )
parametersCurves[ iRule, iVariable,: ] = expParameters
else:
#This is for categorical variables
uniqueValues = np.unique( dataCluster[:, iVariable] )
clusterPerValue = np.zeros( len( uniqueValues) )
for idxUniVal in range( len( uniqueValues ) ):
clusterPerValue[ idxUniVal ] = np.median( uCluster[ iRule, dataCluster[:, iVariable] == uniqueValues[idxUniVal] ] )
totalU = np.sum( clusterPerValue )
clusterPerValuePer = {}
for idxUniVal in range( len( uniqueValues ) ):
clusterPerValuePer[ uniqueValues[idxUniVal] ] = clusterPerValue[ idxUniVal ] / totalU
#check that variable is not in the dic
if not iVariable in dictCategoricalVar:
#A dic for each rule
dictCategoricalVar[ iVariable] = {}
dictCategoricalVar[ iVariable][iRule] = clusterPerValuePer
else:
dictCategoricalVar[ iVariable][iRule] = clusterPerValuePer
return parametersCurves , dictCategoricalVar #parameterCurves for numerical variables and dict for categorical variables
def calculatingConsecuents( self, Umatrix, yTrain, method=0 ):
uniqueClasses = np.unique( yTrain )
consequentPerRule = np.zeros( [Umatrix.shape[0] , len(uniqueClasses ) ] )
weightClass = compute_class_weight('balanced', classes= np.unique(yTrain), y=yTrain)
for iRule in range( Umatrix.shape[0] ):
consequentInter = np.zeros( len(uniqueClasses ) )
samplesPerClass = np.zeros( len(uniqueClasses ) )
medianPerClass = np.zeros( len(uniqueClasses ) )
stdPerClass = np.zeros( len(uniqueClasses ) )
for iClass in range( len(uniqueClasses ) ):
consequentInter[ iClass] = np.sum( Umatrix[ iRule, yTrain == uniqueClasses[ iClass ] ] )
samplesPerClass[ iClass] = np.sum( yTrain == uniqueClasses[ iClass ] )
medianPerClass[ iClass ] = np.median( Umatrix[ iRule, yTrain == uniqueClasses[ iClass ] ] )
stdPerClass[ iClass ] = np.std( Umatrix[ iRule, yTrain == uniqueClasses[ iClass ] ] )
consequentInterMedian = medianPerClass#np.multiply( consequentInter, 1/samplesPerClass )
consequentInter = np.multiply( consequentInter, weightClass )
maxClassIdx = np.argmax( consequentInter )
method = 2
if method ==0:
consequentPerRule[iRule, maxClassIdx ] = 1
elif method == 1:
consequentPerRule[iRule, maxClassIdx ] = consequentInter[ maxClassIdx ]
else:
consequentPerRule[iRule, : ] = consequentInter/ np.sum( consequentInter )
return consequentPerRule
def simplifyMembershipFunctions( self, parametersCurves, minX,maxX, idxNumericalVariables, thrMemb, thrU ):
totalRules, totalFeatures, totalParameters =parametersCurves.shape
#Evaluate pair of membership functions of the same variable
cons = [{'type': 'ineq', 'fun': lambda x: x[2] - x[0] } ] #For the optimziation
for iFeature in range(totalFeatures):
if iFeature in idxNumericalVariables:
xCombined = np.linspace( minX[iFeature], maxX[iFeature] , 100 )
for iRule in range(totalRules-1):
cL_1, wL_1, cR_1, wR_1 = parametersCurves[ iRule, iFeature,: ]
for jRule in range(iRule+1, totalRules):
cL_2, wL_2, cR_2, wR_2 = parametersCurves[ jRule, iFeature,: ]
yPred1 = self.evalulateExponentialFunction(xCombined, cL_1, wL_1, cR_1, wR_1)
yPred2 = self.evalulateExponentialFunction(xCombined, cL_2, wL_2, cR_2, wR_2)
#calculate similarity
combinedPred = np.vstack( ( yPred1,yPred2) )
similarity = np.sum( np.min(combinedPred, axis=0) )/ np.sum( np.max(combinedPred, axis=0) )
if similarity > thrMemb:
expParameters = self.findParametersExponential(xCombined, np.max(combinedPred, axis=0) )
parametersCurves[ iRule, iFeature, :] = expParameters
parametersCurves[ jRule, iFeature, :] = expParameters
#Removing those rules that are like the universe
for iFeature in range(totalFeatures):
if iFeature in idxNumericalVariables:
xPoints1 = np.linspace(minX[iFeature],maxX[iFeature],100)
for iRule in range(totalRules):
cL_1, wL_1, cR_1, wR_1 = parametersCurves[ iRule, iFeature,: ]
yPred1 = self.evalulateExponentialFunction(xPoints1, cL_1, wL_1, cR_1, wR_1)
#calculate similarity with the universe
similarity = np.sum( yPred1 )/ len( xPoints1)
if similarity > thrU:
parametersCurves[ iRule, iFeature, :] = [ np.nan, np.nan, np.nan, np.nan]
return parametersCurves
def normalizingInputData( self, Xinput, numerical_columns, categorical_columns ):
preprocessor = ColumnTransformer([ ('standarizer', MinMaxScaler(),numerical_columns ),
('catTrans', CategoricalTransformer(), categorical_columns )])
return preprocessor.fit_transform( Xinput )
def findingBestFeatures_val_aux( self, Xtrain, yTrain, numericalVariables, categoricalVariables ):
# Finding the number of clusters (rules) using all the training set
clusters, u = self.findingTotalClusters( Xtrain, yTrain, numericalVariables, categoricalVariables )
#Split data in three folds
folds = 3
skf = StratifiedKFold(n_splits=folds, shuffle=True)
foldIdx = 0
selectedFeaturesFolds = np.zeros( [folds,Xtrain.shape[1] ] )
relevanceFeaturesFolds = np.ones( [folds,Xtrain.shape[1] ] )*Xtrain.shape[1] # initially this with the max num of features
for train_index, test_index in skf.split(Xtrain, yTrain):
X_A, X_B = Xtrain.iloc[train_index], Xtrain.iloc[test_index]
y_A, y_B = yTrain[train_index], yTrain[test_index]
#Data plus labels
Xnormalized = self.normalizingInputData( X_A, numericalVariables, categoricalVariables )
Z = np.hstack( (Xnormalized, y_A[:,np.newaxis] ) )
#Normalizing data
if self.oversampling == 1:
#after normalizing, resample
ros = RandomOverSampler()
#Resampling using original labels
X_sampled, y_sampled = ros.fit_resample(Z[ :, :-1], y_A )
scalerY = MinMaxScaler()
y_sampled_normalized = scalerY.fit_transform( y_sampled[:,np.newaxis] )
Z = np.hstack( ( X_sampled , y_sampled_normalized ) )
alldata = Z.T
else:
alldata = Z.T
#Finding centroids
centroidsA, uA, u0, d, jm, p, fpc = fuzz.cluster.cmeans( alldata, clusters,
m=self.mCluster, error=1e-6, maxiter=10000, init=None)
#Denormalizing centroids
scalerZ = MinMaxScaler()
scalerZ.fit_transform( X_A[numericalVariables] )
#only for numerical variables
centroidsA_deNorm = np.zeros( centroidsA.shape)
centroidsA_deNorm[ :, :len( numericalVariables) ] = scalerZ.inverse_transform( centroidsA[:, : len( numericalVariables) ] )
#minValueFeatures = scalerZ.data_min_
maxValueFeatures= scalerZ.data_max_
#Taking back the original samples
if self.oversampling == 1:
uA = uA[:, :X_A.shape[0] ]
# converting X_A and X_B to numpy
# The columns should be sorted such as numerical columns are first and the categroical
X_A = X_A.to_numpy()
X_B = X_B.to_numpy()
idxNumericalVariables = np.arange( len( numericalVariables) )
idxCategoricallVariables = np.arange( len( numericalVariables), len( numericalVariables)+len( categoricalVariables) )
#Min and Max for each feature
minX = np.min( X_A, axis=0)
maxX = np.max( X_A, axis=0)
#Finding the parameter for the membership matrix
parametersCurvesA, dicCategoricalA = self.findingMembershipFunctions( X_A, uA , centroidsA_deNorm, idxNumericalVariables )
#Simplify membership functions
parametersCurvesA_simplify = self.simplifyMembershipFunctions( parametersCurvesA.copy() , minX, maxX , idxNumericalVariables, self.thrSimplyRules, self.thrSimplyUniverse )
#self.plotMembershipFunctions( parametersCurvesA, minX, maxX )
#self.plotMembershipFunctions( parametersCurvesA_simplify, minX, maxX )
consequentRulesA = self.calculatingConsecuents( uA, y_A, 0 )
# idx of the features to evaluate at each iteration
candidates = [ iC for iC in range( Xtrain.shape[1] ) ]
selected = [ ]