-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTLF.java
More file actions
1333 lines (1265 loc) · 46.5 KB
/
Copy pathTLF.java
File metadata and controls
1333 lines (1265 loc) · 46.5 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
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
/*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* TLF.java
* A Framework for Supervised Heterogeneous Transfer Learning
* using Dynamic Distribution Adaptation and Manifold Regularization
*
* @author Md Geaur Rahman and Md Zahidul Islam
* School of Computing, Mathematics & Engineering
* Charles Sturt University, Bathurst, NSW, Australia
*/
package transferlearning;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;
import transferlearning.RulesToForest.Node;
import weka.classifiers.AbstractClassifier;
import weka.classifiers.Classifier;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.matrix.Matrix;
/**
*
* @author Md Geaur Rahman <gea.bau.edu.bd>
*/
public class TLF extends AbstractClassifier {
/**
* For serialization.
*/
private static final long serialVersionUID = -7891225050957072995L;
/**
* The final forest.
*/
private Classifier forest;
/**
* Source Dataset from the knowledge is being transferred.
*/
private Instances sourceDataset=null;
/**
* Target dataset.
*/
private Instances targetDataset=null;
/**
* projected dataset.
*/
private Instances projectedDataset=null;
/**
* The number of trees will be built for a forest. (default 2)
*/
private int numTrees = 2;
/**
* Used to regularize Ridge regression. (default 0.5)
*/
private double sigma_ridge = 0.001;
/**
* Used to regularize MMD. (default 10)
*/
private double lambda_mmd = 10.00;
/**
* Used to regularize manifold. (default 0.1)
*/
private double gama_manifold = 0.100;
boolean isMeanStd=true; //true-> mean+std, false->only mean
boolean isDistinct=true; //true-> remove identical class dist i.e. Opt 1, false->all, Opt 2
boolean isOnlyContributoryAttribute=true; //true: only contributory attributes, false: all numerical attributes
RulesToForest classifier=null;
boolean printFinalClassifier=false;
boolean printMsg=false;
boolean useMatrixTrace=false;
String projApproach="ridge";
private int baseClassifier=0; //0: RF, 1:SysFor
private double similarityThreshold=0.8;//used to find similar class distribution
private String manifoldK_Range="";
/**
* A variable that is used to store the attribute domains of the passed
* dataset.
*/
/** the distance function used. */
final String levelPadding="| ";
/**
* Parses a given list of options. <br>
*
* <!-- options-start -->
* Valid options are: <br>
*
* <pre> -N <numTrees>
* The number of trees to be built for a forest. (default 2)
* </pre>
*
* <pre> -L <minSrcRecLeaf>
* The minimum number of records in a source leaf. Works as in C4.5. (default 10)
* </pre>
*
* <pre> -T <minTgtRecLeaf>
* The minimum number of records in a target leaf. Works as in C4.5. (default 10)</pre>
*
* <pre> -S <Rigde regularizer>
* The ridge regularizer to avoid overfitting to the training data. (default 0.5f)</pre>
*
* <pre> -G <Manifold regularizer>
* The Manifold regularizer to avoid overfitting to the training data. (default 0.1f)</pre>
*
* <pre> -M <MMD regularizer>
* The MMD regularizer to avoid overfitting to the training data. (default 10.0f)</pre>
*
* <!-- options-end -->
*
* Options after -- are passed to the designated classifier.<p>
*
* @param options the list of options as an array of strings
* @throws Exception if an option is not supported
*/
@Override
public void setOptions(String[] options) throws Exception {
String gsNumtrees = Utils.getOption('N', options);
if (gsNumtrees.length() != 0) {
setNumTrees(Integer.parseInt(gsNumtrees));
} else {
setNumTrees(2);
}
String gRidge = Utils.getOption('S', options);
if (gRidge.length() != 0) {
setRidgeRegularizer(Float.parseFloat(gRidge));
} else {
setRidgeRegularizer(0.5f);
}
String gManifold = Utils.getOption('G', options);
if (gManifold.length() != 0) {
setManifoldRegularizer(Float.parseFloat(gManifold));
} else {
setManifoldRegularizer(0.1f);
}
String gMMD = Utils.getOption('M', options);
if (gMMD.length() != 0) {
setMMDRegularizer(Float.parseFloat(gMMD));
} else {
setMMDRegularizer(10.0f);
}
super.setOptions(options);
}
/**
* Gets the current settings of the classifier.
*
* @return the current setting of the classifier
*/
@Override
public String[] getOptions() {
Vector<String> result = new Vector<String>();
result.add("-N");
result.add("" + getNumTrees());
result.add("-S");
result.add("" + getRidgeRegularizer());
result.add("-G");
result.add("" + getManifoldRegularizer());
result.add("-M");
result.add("" + getMMDRegularizer());
Collections.addAll(result, super.getOptions());
return result.toArray(new String[result.size()]);
}
/**
* Setter for Base classifier
*
* @param baseClassifier value 0: RF, 1: SysFor
*/
public void setBaseClassifier(int baseClassifier)
{
this.baseClassifier=baseClassifier;
}
/**
* Setter for ManifoldRegularizer
*
* @param gama_manifold value to set to
*/
public void setManifoldRegularizer(double gama_manifold) {
this.gama_manifold = gama_manifold;
}
/**
* Getter for ManifoldRegularizer
*
* @return gama_manifold
*/
public double getManifoldRegularizer() {
return this.gama_manifold;
}
/**
* Setter for MMDRegularizer
*
* @param lambda_mmd value to set to
*/
public void setMMDRegularizer(float lambda_mmd) {
this.lambda_mmd = lambda_mmd;
}
/**
* Getter for MMDRegularizer
*
* @return lambda_mmd
*/
public double getMMDRegularizer() {
return this.lambda_mmd;
}
/**
* Setter for RidgeRegularizer
*
* @param sigma_ridge value to set to
*/
public void setRidgeRegularizer(double sigma_ridge) {
this.sigma_ridge = sigma_ridge;
}
/**
* Getter for RidgeRegularizer
*
* @return sigma_ridge
*/
public double getRidgeRegularizer() {
return this.sigma_ridge;
}
/**
* Setter for forest size
*
* @param numTrees value to set to for forest size
*/
public void setNumTrees(int numTrees) {
this.numTrees = numTrees;
}
/**
* Getter for forest size
*
* @return numTrees
*/
public int getNumTrees() {
return this.numTrees;
}
public String getManifoldKRange()
{
return this.manifoldK_Range;
}
@Override
public void buildClassifier(Instances data) throws Exception {
getCapabilities().testWithFail(data);
data = new Instances(data);
targetDataset = data;
if(sourceDataset==null) //no transfer learning, just build on target dataset
{
forest=TLUtils.buildClassifier(targetDataset, numTrees, TLUtils.findMinLeafSize(targetDataset.numInstances()), baseClassifier);
String treeStr=TLUtils.preprocessTree(forest.toString());
classifier=new RulesToForest(treeStr,targetDataset);
}
else
{
projectedDataset=projectedSourceData();
if(projectedDataset==null) //no transfer learning, just build on target dataset
{
forest=TLUtils.buildClassifier(targetDataset, numTrees, TLUtils.findMinLeafSize(targetDataset.numInstances()), baseClassifier);
String treeStr=TLUtils.preprocessTree(forest.toString());
classifier=new RulesToForest(treeStr,targetDataset);
}
else{
forest=TLUtils.buildClassifier(projectedDataset, numTrees, TLUtils.findMinLeafSize(projectedDataset.numInstances()), baseClassifier);
String treeStr=TLUtils.preprocessTree(forest.toString());
classifier=new RulesToForest(treeStr,targetDataset);
}
}
if(printFinalClassifier)
System.out.println("\nTransfer learning classifier:\n"+forest.toString());
}
/**
* Classifies the given test instance. The instance has to belong to a
* dataset when it's being classified. Note that a classifier MUST
* implement either this or distributionForInstance().
*
* @param instance the instance to be classified
* @return the predicted most likely class for the instance or
* Utils.missingValue() if no prediction is made
* @exception Exception if an error occurred during the prediction
*/
@Override
public double classifyInstance(Instance instance) throws Exception{
double pred=0.0;
if(classifier==null)
{
throw new IllegalArgumentException(
"TLF: Model is not built yet!!!");
}
else {
pred=classifier.classifyInstance(instance);
}
return pred;
}
public void setSourceDataset(Instances data)throws Exception
{
getCapabilities().testWithFail(data);
data = new Instances(data);
sourceDataset = data;
}
public Instances getSourceDataset()
{
return sourceDataset;
}
public Instances getProjectedDataset()
{
return projectedDataset;
}
@Override
public String toString()
{
return forest.toString();
}
private Instances projectedSourceData()
{
Instances projectSourceTarget=null;
Forest srcForest=new Forest(sourceDataset, numTrees);
srcForest.buildForest();
if(printMsg){
TLUtils.display(srcForest.getClassDistribution(), "Source Class distribution");
TLUtils.display(srcForest.getAttrContribution(), "Source Attribute contribution");
TLUtils.display(srcForest.getLeafCentroids(), "Source Leaf Centroids");
}
Forest tgtForest=new Forest(targetDataset, numTrees);
tgtForest.buildForest();
if(printMsg){
TLUtils.display(tgtForest.getClassDistribution(), "Target Class distribution");
TLUtils.display(tgtForest.getAttrContribution(), "Target Attribute contribution");
TLUtils.display(tgtForest.getLeafCentroids(), "Target Leaf Centroids");
}
//find similar class distributions
double [][]JSD=TLUtils.calculateSimilarityMatrix(srcForest.getClassDistribution(),tgtForest.getClassDistribution(),
sourceDataset,targetDataset);
if(printMsg)TLUtils.display(JSD, "Similarity of class distributions");
int [][]pivots=TLUtils.findPivots(JSD.clone(),similarityThreshold);
if(pivots.length>0)
{
if(printMsg)TLUtils.display(pivots, "Pivots");
double [][]ws=TLUtils.findAttributeContribution(srcForest.getAttrContribution(), pivots, 0);
double [][]wt=TLUtils.findAttributeContribution(tgtForest.getAttrContribution(), pivots, 1);
if(printMsg){
TLUtils.display(ws, "source attribute contribution");
TLUtils.display(wt, "target attribute contribution");
}
//find shared attribute contribution
double [][]srcSAC=TLUtils.findAttributeContribution(srcForest.getLeafCentroids(), pivots, 0);
double [][]tgtSAC=TLUtils.findAttributeContribution(tgtForest.getLeafCentroids(), pivots, 1);
if(printMsg){
TLUtils.display(srcSAC, "source shared attr cont");
TLUtils.display(tgtSAC, "target shared attr cont");
}
//remove the class attribute
int scol=srcSAC[0].length-1;
double [][]X=new double[srcSAC.length][scol];
for(int r=0;r<srcSAC.length;r++)
{ System.arraycopy(srcSAC[r], 0, X[r], 0, scol);
}
int tcol=tgtSAC[0].length-1;
double [][]Y=new double[tgtSAC.length][tcol];
for(int r=0;r<tgtSAC.length;r++)
{ System.arraycopy(tgtSAC[r], 0, Y[r], 0, tcol);
}
////
int [][]comNumAttr=findCommonAttributesNumeric();
int [][]comAttr=findCommonAttributesAll();
if(printMsg){
TLUtils.display(comNumAttr, "CommonAttributesNumeric");
TLUtils.display(comAttr, "CommonAttributesAll");
}
Instances srcPivots=TLUtils.doubleArraysToInstances(sourceDataset, srcSAC);
Instances tgtPivots=TLUtils.doubleArraysToInstances(targetDataset, tgtSAC);
if(printMsg){
System.out.println("\nSource Pivots:\n"+srcPivots);
System.out.println("\nTarget Pivots:\n"+tgtPivots);
}
//find source and target datasets with only common attributes
Instances srcDatasetComAttr=TLUtils.removeAttributes(srcPivots, comAttr,0);
Instances tgtDatasetComAttr=TLUtils.removeAttributes(tgtPivots, comAttr,1);
if(printMsg){
System.out.println("\nSource dataset after removing uncommon attributes:\n"+srcDatasetComAttr);
System.out.println("\nTarget dataset after removing uncommon attributes:\n"+tgtDatasetComAttr);
}
srcDatasetComAttr.setClassIndex(-1);
tgtDatasetComAttr.setClassIndex(-1);
// find maximum mean discrepency matrix (MMD)
Matrix M=null;
if(this.lambda_mmd>0){
MMD mmd=new MMD(srcDatasetComAttr,tgtDatasetComAttr);
M=mmd.getM();
if(printMsg) System.out.println("\nMMD (M) matrix\n"+M);
}
ArrayList<Integer> records=srcForest.getTransferrableInstances(pivots, 0);
if(printMsg)System.out.println("\nTransferable source records\n"+records);
Instances srcTrs=findTrasferableInstances(records);
//merge and then normalize the source and target datasets with only common attributes
Instances datasetComAttr=TLUtils.mergeInstancesTL(sourceDataset,targetDataset);
datasetComAttr.setClassIndex(sourceDataset.classIndex());
// find Manifold Graph Laplacian (L) matrix
Matrix L=null;
if(this.gama_manifold>0){
double [][]k=TLUtils.calculateCosineSimilarity(datasetComAttr);
Manifold mfold=new Manifold(datasetComAttr,k);
L=mfold.getL();
this.manifoldK_Range=mfold.getkRange();
System.out.println("\nManifold: auto range of k values: "+this.manifoldK_Range+"\n");
if(printMsg) System.out.println("\nManifold Graph Laplacian (L) matrix\n"+L);
}
//find projection matrix using the MMD and Manifold
int nm=datasetComAttr.numInstances();
double [][]kk=TLUtils.kernel(datasetComAttr);
if(printMsg) TLUtils.display(kk, "Kernel matrix");
Matrix K=new Matrix(kk);
Matrix I=new Matrix(TLUtils.identityMatrix(nm));
Matrix I_sigma=I.times(this.sigma_ridge);
Matrix G=K.plus(I_sigma);
if(this.lambda_mmd>0){
Matrix M_lambda=M.times(this.lambda_mmd);
G=G.plus(M_lambda);
}
if(this.gama_manifold>0){
Matrix L_gamma=L.times(this.gama_manifold);
G=G.plus(L_gamma);
}
Matrix Qs=new Matrix(transformedAttrContribution(ws,nm,0));
Matrix Qt=new Matrix(transformedAttrContribution(wt,nm,1));
Matrix Qs_transpose=Qs.transpose();
Matrix QsG=Qs_transpose.times(G);
Matrix P=QsG.times(Qt);
double [][]Ps=P.getArray();
if(printMsg) TLUtils.displayProjection(Ps);
projectSourceTarget=projectSourceDataAndMergeTarget(Ps,srcTrs,comAttr);
}
if(printMsg) System.out.println("\nProjected source data\n"+projectSourceTarget);
return projectSourceTarget;
}
private Instances findTrasferableInstances(ArrayList<Integer> records)
{
int s=records.size();
Instances srcDS=new Instances(sourceDataset,0);
//src data
for(int i=0;i<s;i++)
{
int rno=records.get(i);
srcDS.add(sourceDataset.get(rno));
}
srcDS.setClassIndex(-1);
return srcDS;
}
private Instances projectSourceDataAndMergeTarget(double [][]Ps, Instances srcDS,int [][]comAttr)
{
String relation=targetDataset.relationName();
int numAttr=targetDataset.numAttributes();
ArrayList<Attribute> testAtts= new ArrayList<Attribute>();
ArrayList<String> []testVals=new ArrayList[numAttr];
for(int i=0;i<numAttr;i++)
{
if(targetDataset.attribute(i).isNominal())
{
testVals[i] = new ArrayList<String>();
int numv=targetDataset.attribute(i).numValues();
for(int k=0;k<numv;k++)
testVals[i].add(targetDataset.attribute(i).value(k));
}
}
int p=Ps.length;//first value is the intercept
int s=srcDS.numInstances();
int t=targetDataset.numInstances();
int total=s+t;
double [][]alldata=new double[total][numAttr];
//src data
for(int i=0;i<s;i++)
{
Instance inst=srcDS.get(i);
for(int j=0;j<numAttr;j++)
{
if(targetDataset.attribute(j).isNominal())
{
int sca=-1;
for(int m=0;m<comAttr.length;m++)
{
if(comAttr[m][1]==j)
{
sca=comAttr[m][0];
}
}
if(sca>=0){
double v=inst.value(sca);
String tv=srcDS.attribute(sca).value((int)v);
int ajIndex=testVals[j].indexOf(tv);
if(ajIndex>=0)
alldata[i][j]=ajIndex;
else
{
testVals[j].add(tv);
alldata[i][j]=testVals[j].indexOf(tv);
}
}
}
else
{
double tv=0;
for(int c=0;c<p;c++)
{
tv+=inst.value(c)*Ps[c][j];
}
alldata[i][j]=tv;
}
}
}
//merging data
for(int i=0,r=s;i<t && r<total;i++,r++)
{
Instance inst=targetDataset.get(i);
for(int j=0;j<numAttr;j++)
{
if(targetDataset.attribute(j).isNominal())
{
double v=inst.value(j);
String tv=targetDataset.attribute(j).value((int)v);
alldata[r][j]=testVals[j].indexOf(tv);
}
else
{
alldata[r][j]=inst.value(j);
}
}
}
for(int i=0;i<numAttr;i++)
{
if(targetDataset.attribute(i).isNominal())
{
testAtts.add(new Attribute(targetDataset.attribute(i).name(),testVals[i]));
}
else{
testAtts.add(new Attribute(targetDataset.attribute(i).name()));
}
}
Instances projectSourceTarget = new Instances(relation, testAtts, 0);
for(int i=0;i<total;i++)
{
projectSourceTarget.add(new DenseInstance(1.0, alldata[i]));
}
projectSourceTarget.setClassIndex(targetDataset.classIndex());
return projectSourceTarget;
}
private double[][] findBoundary()
{
int col=sourceDataset.numAttributes()-1;
double [][]boundary=new double[4][col];//0: max, 1: min; 2: range, 3:weight
double trange=0;
for(int i=0;i<col;i++)
{
if(sourceDataset.attribute(i).isNumeric())
{
boundary[0][i]=Math.max(sourceDataset.attributeStats(i).numericStats.max,
targetDataset.attributeStats(i).numericStats.max);
boundary[1][i]=Math.min(sourceDataset.attributeStats(i).numericStats.min,
targetDataset.attributeStats(i).numericStats.min);
boundary[2][i] = boundary[0][i]-boundary[1][i];
trange+=boundary[2][i];
}
}
if(trange>0){
for(int i=0;i<col;i++)
{
if(sourceDataset.attribute(i).isNumeric())
{
boundary[3][i]=boundary[2][i]/trange;
}
}
}
return boundary;
}
private double[][] transformedAttrContribution(double [][]w, int nm, int flag)
{
int n=w.length;
int noa=w[0].length;
double[][] Q=new double[nm][noa];
for(int i=0;i<nm;i++){
if((flag==0 && i<n)||(flag==1 && i>=n)){
int k=i;
if(flag==1)k=i-n;
for(int j=0;j<noa;j++){
Q[i][j]=w[k][j];
}
}
else{
Arrays.fill(Q[i], 0);
}
}
return Q;
}
private int[][] findCommonAttributesNumeric()
{
int sa=sourceDataset.numAttributes();
int ta=targetDataset.numAttributes();
ArrayList<Integer>source = new ArrayList<Integer>();
ArrayList<Integer>target = new ArrayList<Integer>();
for(int i=0;i<sa;i++)
{
if(sourceDataset.attribute(i).isNumeric()){
String aName=sourceDataset.attribute(i).name();
for(int j=0;j<ta;j++) {
if(targetDataset.attribute(j).isNumeric()){
String tName=targetDataset.attribute(j).name();
if(aName.equals(tName)){
source.add(i);
target.add(j);
break;
}
}
}
}
}
int ss=source.size();
int[][] comA=new int[ss][2];
if(ss==target.size())
{
for(int i=0;i<ss;i++)
{
comA[i][0]=source.get(i);
comA[i][1]=target.get(i);
}
}
return comA.clone();
}
private int[][] findCommonAttributesAll()
{
int sa=sourceDataset.numAttributes();
int ta=targetDataset.numAttributes();
ArrayList<Integer>source = new ArrayList<Integer>();
ArrayList<Integer>target = new ArrayList<Integer>();
for(int i=0;i<sa;i++)
{
String aName=sourceDataset.attribute(i).name();
for(int j=0;j<ta;j++){
String tName=targetDataset.attribute(j).name();
if(aName.equals(tName)){
source.add(i);
target.add(j);
break;
}
}
}
int ss=source.size();
int[][] comA=new int[ss][2];
if(ss==target.size())
{
for(int i=0;i<ss;i++)
{
comA[i][0]=source.get(i);
comA[i][1]=target.get(i);
}
}
return comA.clone();
}
private class Manifold{
Matrix L;
Instances data;
double[][] S;
int knnMin;
int knnMax;
public Manifold(Instances data, double[][]S)
{
this.data=new Instances(data);
this.S=S.clone();
knnMin=Integer.MAX_VALUE;
knnMax=Integer.MIN_VALUE;
calculateL();
}
public String getkRange()
{
return knnMin+"-"+knnMax;
}
public Matrix getL()
{
return L;
}
private void calculateL()
{
// if(printMsg) System.out.println("\nthe values of k are:\n");
double [][]ww=calculateW();
double [][]dd=digD(ww);
double [][]ii=TLUtils.identityMatrix(data.numInstances());
Matrix W=new Matrix(ww);
Matrix D=new Matrix(dd);
Matrix D_inverse=D.inverse();
Matrix I=new Matrix(ii);
Matrix tmp=D_inverse.times(W);
Matrix tmp1=tmp.times(D_inverse);
L=I.minus(tmp1);
}
private double[][] digD(double [][]W)
{
int nm=W.length;
double [][]D=new double[nm][nm];
for(int i=0;i<nm;i++)
{
double sum=0;
for(int j=0;j<nm;j++)
{
sum+=W[i][j];
D[i][j]=0;
}
D[i][i]=sum;
}
return D;
}
private double[][] calculateW()
{
int nm=data.numInstances();
double [][]W=new double[nm][nm];
for(int i=0;i<nm;i++)
{
for(int j=0;j<nm;j++)
{
W[i][j]=calculateGraphAffinity(i,j);
}
}
return W;
}
private double calculateGraphAffinity(int i,int j)
{
double w_ij=0;
if(i==j)
w_ij=S[i][j];
else{
ArrayList<Integer> knn_i=calculateGraphAffinity(i);
ArrayList<Integer> knn_j=calculateGraphAffinity(j);
if(knn_i.contains(new Integer(j))||knn_j.contains(new Integer(i)))
w_ij=S[i][j];
}
return w_ij;
}
private ArrayList<Integer> calculateGraphAffinity(int i)
{
ArrayList<Integer> knn=new ArrayList<Integer>();
int minK=4;
int maxK=64;
int nm=S.length;
double [][]array=new double[nm][2];
for(int k=0;k<nm;k++)
{
array[k][0]=S[k][i];
array[k][1]=k;
}
Arrays.sort(array, new Comparator<double[]>() {
public int compare(double[] a, double[] b) {
return Double.compare(a[0], b[0]);
}
});
double cv=data.get(i).value(data.classIndex());
for(int k=nm-1;k>=0;k--)
{
int recNo=(int)array[k][1];
if(recNo!=i)
{
if((knn.size()<minK || cv==data.get(recNo).value(data.classIndex()))
&& knn.size()<maxK )
knn.add(recNo);
}
}
// if(printMsg) System.out.print(knn.size()+", ");
if(knn.size()<knnMin)knnMin=knn.size();
if(knn.size()>knnMax)knnMax=knn.size();
return knn;
}
}
private class MMD{
Matrix M;
double[][] mmd;
Instances src;
Instances tgt;
double mu=0.5;
public MMD(Instances src, Instances tgt)
{
this.src=new Instances(src);
this.tgt=new Instances(tgt);
calculateM();
}
public double[][] getMMD()
{
return mmd.clone();
}
public Matrix getM()
{
return M;
}
private void calculateM()
{
int n=src.numInstances();
int m=tgt.numInstances();
int nm=n+m;
mmd=new double[nm][nm];
for(int i=0;i<nm;i++)
{
Arrays.fill(mmd[i], 0);
}
ArrayList<String> commonCV=findCommonCV();
int numc=commonCV.size();
if(numc>0)
{
ArrayList<Integer>[] srcCWR=findClassWiseRecords(src,commonCV,0);
ArrayList<Integer>[] tgtCWR=findClassWiseRecords(tgt,commonCV,n);
estimate_mu(srcCWR,tgtCWR,commonCV,n);
if(printMsg) System.out.println("\nMMD mu:"+mu);
//find marginal distribution
double[][] m_m=new double[nm][nm];
double sn=1.0/(n*n);
double tm=1.0/(m*m);
double stmn=-1.0/(n*m);
for(int i=0;i<nm;i++)
{
for(int j=0;j<nm;j++)
{
if(i<n && j<n)
m_m[i][j]=sn;
else if(i>=n && j>=n)
m_m[i][j]=tm;
else
m_m[i][j]=stmn;
}
}
double[][] m_c=new double[nm][nm];
for(int i=0;i<nm;i++)
{
Arrays.fill(m_c[i], 0);
}
for(int c=0;c<numc;c++)
{
int nc=srcCWR[c].size();
int mc=tgtCWR[c].size();
sn=1.0/(nc*nc);
tm=1.0/(mc*mc);
stmn=-1.0/(nc*mc);
for(int i=0;i<nm;i++)
{
for(int j=0;j<nm;j++)
{
if(srcCWR[c].contains(new Integer(i)) && srcCWR[c].contains(new Integer(j)))
m_c[i][j]+=sn;
else if(tgtCWR[c].contains(new Integer(i)) && tgtCWR[c].contains(new Integer(j)))
m_c[i][j]+=tm;
else if((srcCWR[c].contains(new Integer(i)) && tgtCWR[c].contains(new Integer(j)))
||(tgtCWR[c].contains(new Integer(i)) && srcCWR[c].contains(new Integer(j))))
m_c[i][j]+=stmn;
}
}
}
for(int i=0;i<nm;i++)
{
for(int j=0;j<nm;j++)
{
mmd[i][j]=(1-mu)*m_m[i][j]+mu*m_c[i][j];
}
}
}
M=new Matrix(mmd);
}
private void estimate_mu(ArrayList<Integer>[] srcCWR,ArrayList<Integer>[] tgtCWR,
ArrayList<String> commonCV, int start)
{
double adist_m = proxy_a_distance(src, tgt);
Instances srctmp;
Instances tgttmp;
int numc=commonCV.size();
double terror=0;
for (int i = 0; i < numc; i++) {
srctmp=new Instances(src,0);
int nr=srcCWR[i].size();
for(int j=0;j<nr;j++){
srctmp.add(src.instance(srcCWR[i].get(j)));
}
tgttmp=new Instances(tgt,0);
nr=tgtCWR[i].size();
for(int j=0;j<nr;j++){
tgttmp.add(tgt.instance((tgtCWR[i].get(j)-start)));
}
if(srctmp.numInstances()>0 && tgttmp.numInstances()>0){
double adist_tmp = proxy_a_distance(srctmp, tgttmp);
terror+=adist_tmp;
}
}
double adist_c=terror/numc;
if((adist_c+adist_m)<=0)
{
mu=0.5;
}
else
{
mu=adist_c/(adist_c+adist_m);
if(mu>1)
mu=1;
else if(mu<0)
mu=0;
}
}
private ArrayList<Integer>[] findClassWiseRecords(Instances data,ArrayList<String> commonCV, int start)
{
int numc=commonCV.size();
ArrayList<Integer>[]cvRecords=new ArrayList[numc];
for (int i = 0; i < numc; i++) {
cvRecords[i] = new ArrayList<Integer>();
}
int ci=data.classIndex();
int n=data.numInstances();
for (int i = 0; i < n; i++) {
Instance inst=data.get(i);
String v=data.attribute(ci).value((int)inst.value(ci));
int index=commonCV.indexOf(v);
if(index>=0)
{
cvRecords[index].add(i+start);
}
}
return cvRecords;
}
private ArrayList<String> findCommonCV()
{
int ci=src.classIndex();
ArrayList<Double> sv=new ArrayList<Double>();
int ns=src.numInstances();
for(int i=0;i<ns;i++)
{
double d=src.get(i).value(ci);
if(!sv.contains(d))
{
sv.add(d);
}
}
ArrayList<Double> tv=new ArrayList<Double>();
int ts=tgt.numInstances();
for(int i=0;i<ts;i++)
{
double d=tgt.get(i).value(ci);
if(!tv.contains(d))
{
tv.add(d);