source: trunk/CrossPare/src/de/ugoe/cs/cpdp/dataprocessing/Resampling.java @ 74

Last change on this file since 74 was 41, checked in by sherbold, 9 years ago
  • formatted code and added copyrights
  • Property svn:mime-type set to text/plain
File size: 2.5 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.collections4.list.SetUniqueList;
18
19import weka.core.Instances;
20import weka.filters.Filter;
21import weka.filters.supervised.instance.Resample;
22
23/**
24 * Resamples the data with WEKA {@link Resample} to have a uniform distribution among all classes.
25 *
26 * @author Steffen Herbold
27 */
28public class Resampling implements IProcessesingStrategy, ISetWiseProcessingStrategy {
29
30    /**
31     * Does not have parameters. String is ignored.
32     *
33     * @param parameters
34     *            ignored
35     */
36    @Override
37    public void setParameter(String parameters) {
38        // dummy
39    }
40
41    /*
42     * (non-Javadoc)
43     *
44     * @see de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy#apply(weka.core.Instances,
45     * org.apache.commons.collections4.list.SetUniqueList)
46     */
47    @Override
48    public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
49        for (Instances traindata : traindataSet) {
50            apply(testdata, traindata);
51        }
52    }
53
54    /*
55     * (non-Javadoc)
56     *
57     * @see de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy#apply(weka.core.Instances,
58     * weka.core.Instances)
59     */
60    @Override
61    public void apply(Instances testdata, Instances traindata) {
62        Resample resample = new Resample();
63        resample.setSampleSizePercent(100);
64        resample.setBiasToUniformClass(1.0);
65
66        Instances traindataSample;
67        try {
68            resample.setInputFormat(traindata);
69            traindataSample = Filter.useFilter(traindata, resample);
70        }
71        catch (Exception e) {
72            throw new RuntimeException(e);
73        }
74        traindata.clear();
75        for (int i = 0; i < traindataSample.size(); i++) {
76            traindata.add(traindataSample.get(i));
77        }
78    }
79
80}
Note: See TracBrowser for help on using the repository browser.