1 | package de.ugoe.cs.cpdp.dataprocessing;
|
---|
2 |
|
---|
3 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
4 |
|
---|
5 | import weka.core.Instances;
|
---|
6 | import weka.filters.Filter;
|
---|
7 | import weka.filters.supervised.instance.Resample;
|
---|
8 |
|
---|
9 | // TODO comment
|
---|
10 | public class Undersampling implements IProcessesingStrategy,
|
---|
11 | ISetWiseProcessingStrategy {
|
---|
12 |
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Does not have parameters. String is ignored.
|
---|
16 | * @param parameters ignored
|
---|
17 | */
|
---|
18 | @Override
|
---|
19 | public void setParameter(String parameters) {
|
---|
20 | // dummy
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
26 | for( Instances traindata : traindataSet ) {
|
---|
27 | apply(testdata, traindata);
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public void apply(Instances testdata, Instances traindata) {
|
---|
33 |
|
---|
34 | final int[] counts = traindata.attributeStats(traindata.classIndex()).nominalCounts;
|
---|
35 |
|
---|
36 | if( counts[1]<counts[0] ) {
|
---|
37 | Instances negatives = new Instances(traindata);
|
---|
38 | Instances positives = new Instances(traindata);
|
---|
39 |
|
---|
40 | for( int i=traindata.size()-1 ; i>=0 ; i-- ) {
|
---|
41 | if( Double.compare(1.0, negatives.get(i).classValue())==0 ) {
|
---|
42 | negatives.remove(i);
|
---|
43 | }
|
---|
44 | if( Double.compare(0.0, positives.get(i).classValue())==0 ) {
|
---|
45 | positives.remove(i);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | Resample resample = new Resample();
|
---|
50 | resample.setSampleSizePercent((100.0* counts[1])/counts[0]);
|
---|
51 | try {
|
---|
52 | resample.setInputFormat(traindata);
|
---|
53 | negatives = Filter.useFilter(negatives, resample);
|
---|
54 | } catch (Exception e) {
|
---|
55 | throw new RuntimeException(e);
|
---|
56 | }
|
---|
57 | traindata.clear();
|
---|
58 | for( int i=0 ; i<negatives.size() ; i++ ) {
|
---|
59 | traindata.add(negatives.get(i));
|
---|
60 | }
|
---|
61 | for( int i=0 ; i<positives.size() ; i++ ) {
|
---|
62 | traindata.add(positives.get(i));
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|