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 | import weka.experiment.Stats;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Normalizes each attribute of each data set separately.
|
---|
12 | * @author Steffen Herbold
|
---|
13 | */
|
---|
14 | public class Normalization implements ISetWiseProcessingStrategy, IProcessesingStrategy {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances, org.apache.commons.collections4.list.SetUniqueList)
|
---|
18 | */
|
---|
19 | @Override
|
---|
20 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
21 | final Attribute classAtt = testdata.classAttribute();
|
---|
22 |
|
---|
23 | for( int i=0 ; i<testdata.numAttributes(); i++) {
|
---|
24 | if( !testdata.attribute(i).equals(classAtt) ) {
|
---|
25 | Stats teststats = testdata.attributeStats(i).numericStats;
|
---|
26 |
|
---|
27 | double minVal = teststats.min;
|
---|
28 | double maxVal = teststats.max;
|
---|
29 |
|
---|
30 | for( Instances traindata : traindataSet ) {
|
---|
31 | Stats trainstats = traindata.attributeStats(i).numericStats;
|
---|
32 | if( minVal>trainstats.min ) {
|
---|
33 | minVal = trainstats.min;
|
---|
34 | }
|
---|
35 | if( maxVal<trainstats.max ) {
|
---|
36 | maxVal = trainstats.max;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | for( int j=0 ; j<testdata.numInstances() ; j++ ) {
|
---|
41 | Instance inst = testdata.instance(j);
|
---|
42 | double newValue = (inst.value(i)-minVal)/(maxVal-minVal);
|
---|
43 | inst.setValue(i, newValue);
|
---|
44 | }
|
---|
45 |
|
---|
46 | for( Instances traindata : traindataSet ) {
|
---|
47 | for( int j=0 ; j<traindata.numInstances() ; j++ ) {
|
---|
48 | Instance inst = traindata.instance(j);
|
---|
49 | double newValue = (inst.value(i)-minVal)/(maxVal-minVal);
|
---|
50 | inst.setValue(i, newValue);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances, weka.core.Instances)
|
---|
60 | */
|
---|
61 | @Override
|
---|
62 | public void apply(Instances testdata, Instances traindata) {
|
---|
63 | final Attribute classAtt = testdata.classAttribute();
|
---|
64 |
|
---|
65 | for( int i=0 ; i<testdata.numAttributes(); i++) {
|
---|
66 | if( !testdata.attribute(i).equals(classAtt) ) {
|
---|
67 | Stats teststats = testdata.attributeStats(i).numericStats;
|
---|
68 |
|
---|
69 | double minVal = teststats.min;
|
---|
70 | double maxVal = teststats.max;
|
---|
71 |
|
---|
72 | Stats trainstats = traindata.attributeStats(i).numericStats;
|
---|
73 | if( minVal>trainstats.min ) {
|
---|
74 | minVal = trainstats.min;
|
---|
75 | }
|
---|
76 | if( maxVal<trainstats.max ) {
|
---|
77 | maxVal = trainstats.max;
|
---|
78 | }
|
---|
79 |
|
---|
80 | for( int j=0 ; j<testdata.numInstances() ; j++ ) {
|
---|
81 | Instance inst = testdata.instance(j);
|
---|
82 | double newValue = (inst.value(i)-minVal)/(maxVal-minVal);
|
---|
83 | inst.setValue(i, newValue);
|
---|
84 | }
|
---|
85 |
|
---|
86 | for( int j=0 ; j<traindata.numInstances() ; j++ ) {
|
---|
87 | Instance inst = traindata.instance(j);
|
---|
88 | double newValue = (inst.value(i)-minVal)/(maxVal-minVal);
|
---|
89 | inst.setValue(i, newValue);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Does not have parameters. String is ignored.
|
---|
97 | * @param parameters ignored
|
---|
98 | */
|
---|
99 | @Override
|
---|
100 | public void setParameter(String parameters) {
|
---|
101 | // no parameters
|
---|
102 | }
|
---|
103 | }
|
---|