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 | /**
|
---|
10 | * Resamples the data with WEKA {@link Resample} to have a uniform distribution among all classes.
|
---|
11 | * @author Steffen Herbold
|
---|
12 | */
|
---|
13 | public class Resampling implements IProcessesingStrategy,
|
---|
14 | ISetWiseProcessingStrategy {
|
---|
15 |
|
---|
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 | * (non-Javadoc)
|
---|
28 | * @see de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy#apply(weka.core.Instances, org.apache.commons.collections4.list.SetUniqueList)
|
---|
29 | */
|
---|
30 | @Override
|
---|
31 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
32 | for( Instances traindata : traindataSet ) {
|
---|
33 | apply(testdata, traindata);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * (non-Javadoc)
|
---|
39 | * @see de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy#apply(weka.core.Instances, weka.core.Instances)
|
---|
40 | */
|
---|
41 | @Override
|
---|
42 | public void apply(Instances testdata, Instances traindata) {
|
---|
43 | Resample resample = new Resample();
|
---|
44 | resample.setSampleSizePercent(100);
|
---|
45 | resample.setBiasToUniformClass(1.0);
|
---|
46 |
|
---|
47 | Instances traindataSample;
|
---|
48 | try {
|
---|
49 | resample.setInputFormat(traindata);
|
---|
50 | traindataSample = Filter.useFilter(traindata, resample);
|
---|
51 | } catch (Exception e) {
|
---|
52 | throw new RuntimeException(e);
|
---|
53 | }
|
---|
54 | traindata.clear();
|
---|
55 | for( int i=0 ; i<traindataSample.size() ; i++ ) {
|
---|
56 | traindata.add(traindataSample.get(i));
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | }
|
---|