source: trunk/CrossPare/src/de/ugoe/cs/cpdp/dataprocessing/TCAPlusNormalization.java @ 54

Last change on this file since 54 was 52, checked in by sherbold, 9 years ago
  • added copyright statement to TCAPlusNormalization
  • Property svn:mime-type set to text/plain
File size: 4.8 KB
Line 
1// Copyright 2015 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.cpdp.dataprocessing;
16
17import org.apache.commons.math3.ml.distance.EuclideanDistance;
18
19import weka.core.Instances;
20
21// normalization selected according to TCA+ rules (TCA has to be applied separately
22public class TCAPlusNormalization implements IProcessesingStrategy {
23
24    private class DistChar {
25        private final double mean;
26        private final double std;
27        private final double min;
28        private final double max;
29        private int num;
30        private DistChar(double mean, double std, double min, double max, int num) {
31            this.mean = mean;
32            this.std = std;
33            this.min = min;
34            this.max = max;
35            this.num = num;
36        }
37    }
38   
39    /**
40     * Does not have parameters. String is ignored.
41     *
42     * @param parameters
43     *            ignored
44     */
45    @Override
46    public void setParameter(String parameters) {
47        // TODO Auto-generated method stub
48       
49    }
50
51    @Override
52    public void apply(Instances testdata, Instances traindata) {
53        applyTCAPlus(testdata, traindata);
54    }
55   
56    private void applyTCAPlus(Instances testdata, Instances traindata) {
57        DistChar dcTest = datasetDistance(testdata);
58        DistChar dcTrain = datasetDistance(traindata);
59       
60        // RULE 1:
61        if( 0.9*dcTrain.mean<=dcTest.mean && 1.1*dcTrain.mean>=dcTest.mean &&
62            0.9*dcTrain.std<=dcTest.std && 1.1*dcTrain.std>=dcTest.std) {
63            // do nothing
64        }
65        // RULE 2:
66        else if((0.4*dcTrain.min>dcTest.min || 1.6*dcTrain.min<dcTest.min) &&
67                (0.4*dcTrain.max>dcTest.max || 1.6*dcTrain.min<dcTest.max) &&
68                (0.4*dcTrain.min>dcTest.num || 1.6*dcTrain.min<dcTest.num)) {
69            NormalizationUtil.minMax(testdata);
70            NormalizationUtil.minMax(traindata);
71        }
72        // RULE 3:
73        else if((0.4*dcTrain.std>dcTest.std && dcTrain.num<dcTest.num) ||
74                (1.6*dcTrain.std<dcTest.std)&& dcTrain.num>dcTest.num) {
75            NormalizationUtil.zScoreTraining(testdata, traindata);
76        }
77        // RULE 4:
78        else if((0.4*dcTrain.std>dcTest.std && dcTrain.num>dcTest.num) ||
79                (1.6*dcTrain.std<dcTest.std)&& dcTrain.num<dcTest.num) {
80            NormalizationUtil.zScoreTarget(testdata, traindata);
81        }
82        //RULE 5:
83        else {
84            NormalizationUtil.zScore(testdata);
85            NormalizationUtil.zScore(traindata);
86        }
87    }
88   
89    private DistChar datasetDistance(Instances data) {
90        double distance;
91        double sumAll = 0.0;
92        double sumAllQ = 0.0;
93        double min = Double.MAX_VALUE;
94        double max = Double.MIN_VALUE;
95        int numCmp = 0;
96        int l = 0;
97        double[] inst1 = new double[data.numAttributes()-1];
98        double[] inst2 = new double[data.numAttributes()-1];
99        EuclideanDistance euclideanDistance = new EuclideanDistance();
100        for( int i=0; i<data.numInstances(); i++ ) {
101            l=0;
102            for( int k=0; k<data.numAttributes(); k++ ) {
103                if( k!=data.classIndex() ) {
104                    inst1[l] = data.instance(i).value(k);
105                }
106            }
107            for( int j=0; j<data.numInstances(); j++ ) {
108                l=0;
109                for( int k=0; k<data.numAttributes(); k++ ) {
110                    if( k!=data.classIndex() ) {
111                        inst2[l] = data.instance(j).value(k);
112                    }
113                }
114                distance = euclideanDistance.compute(inst1, inst2);
115                sumAll += distance;
116                sumAllQ += distance*distance;
117                numCmp++;
118                if( distance < min ) {
119                    min = distance;
120                }
121                if( distance > max ) {
122                    max = distance;
123                }
124            }
125        }
126        double mean = sumAll / numCmp;
127        double std = Math.sqrt((sumAllQ-(sumAll*sumAll)/numCmp) *
128                                  (1.0d / (numCmp - 1)));
129        return new DistChar(mean, std, min, max, data.numInstances());
130    }
131
132}
Note: See TracBrowser for help on using the repository browser.