1 | package de.ugoe.cs.cpdp.dataprocessing;
|
---|
2 |
|
---|
3 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
4 |
|
---|
5 | import weka.core.Attribute;
|
---|
6 | import weka.core.Instance;
|
---|
7 | import weka.core.Instances;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Standardization procedure after Watanabe et al.: Adapting a Fault Prediction Model to Allow Inter Language Reuse.
|
---|
11 | * <br><br>
|
---|
12 | * In comparison to Watanabe et al., we transform training data instead of the test data. Otherwise, this approach would not be feasible with multiple projects.
|
---|
13 | * @author Steffen Herbold
|
---|
14 | */
|
---|
15 | public class AverageStandardization implements ISetWiseProcessingStrategy, IProcessesingStrategy {
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Does not have parameters. String is ignored.
|
---|
19 | * @param parameters ignored
|
---|
20 | */
|
---|
21 | @Override
|
---|
22 | public void setParameter(String parameters) {
|
---|
23 | // dummy
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances, org.apache.commons.collections4.list.SetUniqueList)
|
---|
28 | */
|
---|
29 | @Override
|
---|
30 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
31 | final Attribute classAttribute = testdata.classAttribute();
|
---|
32 |
|
---|
33 | final double[] meanTest = new double[testdata.numAttributes()];
|
---|
34 |
|
---|
35 | // get means of testdata
|
---|
36 | for( int j=0 ; j<testdata.numAttributes() ; j++ ) {
|
---|
37 | if( testdata.attribute(j)!=classAttribute ) {
|
---|
38 | meanTest[j] = testdata.meanOrMode(j);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | // preprocess training data
|
---|
43 | for( Instances traindata : traindataSet ) {
|
---|
44 | double[] meanTrain = new double[testdata.numAttributes()];
|
---|
45 | for( int j=0 ; j<testdata.numAttributes() ; j++ ) {
|
---|
46 | if( testdata.attribute(j)!=classAttribute ) {
|
---|
47 | meanTrain[j] = traindata.meanOrMode(j);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | for( int i=0 ; i<traindata.numInstances() ; i++ ) {
|
---|
52 | Instance instance = traindata.instance(i);
|
---|
53 | for( int j=0 ; j<testdata.numAttributes() ; j++ ) {
|
---|
54 | if( testdata.attribute(j)!=classAttribute ) {
|
---|
55 | instance.setValue(j, instance.value(j)*meanTest[j]/meanTrain[j]);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances, weka.core.Instances)
|
---|
64 | */
|
---|
65 | @Override
|
---|
66 | public void apply(Instances testdata, Instances traindata) {
|
---|
67 | final Attribute classAttribute = testdata.classAttribute();
|
---|
68 |
|
---|
69 | final double[] meanTest = new double[testdata.numAttributes()];
|
---|
70 |
|
---|
71 | // get means of testdata
|
---|
72 | for( int j=0 ; j<testdata.numAttributes() ; j++ ) {
|
---|
73 | if( testdata.attribute(j)!=classAttribute ) {
|
---|
74 | meanTest[j] = testdata.meanOrMode(j);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | // preprocess training data
|
---|
79 | final double[] meanTrain = new double[testdata.numAttributes()];
|
---|
80 | for( int j=0 ; j<testdata.numAttributes() ; j++ ) {
|
---|
81 | if( testdata.attribute(j)!=classAttribute ) {
|
---|
82 | meanTrain[j] = traindata.meanOrMode(j);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | for( int i=0 ; i<traindata.numInstances() ; i++ ) {
|
---|
87 | Instance instance = traindata.instance(i);
|
---|
88 | for( int j=0 ; j<testdata.numAttributes() ; j++ ) {
|
---|
89 | if( testdata.attribute(j)!=classAttribute ) {
|
---|
90 | instance.setValue(j, instance.value(j)*meanTest[j]/meanTrain[j]);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|