source: trunk/CrossPare/src/de/ugoe/cs/cpdp/dataprocessing/AttributeNonRemoval.java @ 117

Last change on this file since 117 was 86, checked in by sherbold, 9 years ago
  • switched workspace encoding to UTF-8 and fixed broken characters
  • Property svn:mime-type set to text/plain
File size: 3.1 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 java.util.ArrayList;
18
19import org.apache.commons.collections4.list.SetUniqueList;
20
21import weka.core.Instances;
22
23/**
24 * Removes attributes from all data sets, except the one defined, using their name.
25 *
26 * @author Fabian Trautsch
27 */
28public class AttributeNonRemoval implements ISetWiseProcessingStrategy, IProcessesingStrategy {
29
30    /**
31     * names of the attributes to be kept (determined by {@link #setParameter(String)})
32     */
33    private ArrayList<String> attributeNames = new ArrayList<String>();
34
35    /**
36     * Sets that attributes that will be kept. The string contains the blank-separated names of the
37     * attributes to be kept. <br>
38     * <br>
39     * Note, that keeping of attributes with blanks is currently not supported!
40     *
41     * @param parameters
42     *            string with the blank-separated attribute names
43     */
44    @Override
45    public void setParameter(String parameters) {
46        if (parameters != null) {
47            String[] attributeNamesArray = parameters.split(" ");
48            for (String attributeName : attributeNamesArray) {
49                attributeNames.add(attributeName);
50            }
51        }
52    }
53
54    /**
55     * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances,
56     *      org.apache.commons.collections4.list.SetUniqueList)
57     */
58    @Override
59    public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
60        for (String attributeName : attributeNames) {
61            for (int i = 0; i < testdata.numAttributes(); i++) {
62                if (!attributeName.equals(testdata.attribute(i).name())) {
63                    testdata.deleteAttributeAt(i);
64                    for (Instances traindata : traindataSet) {
65                        traindata.deleteAttributeAt(i);
66                    }
67                }
68            }
69        }
70    }
71
72    /**
73     * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances,
74     *      weka.core.Instances)
75     */
76    @Override
77    public void apply(Instances testdata, Instances traindata) {
78        for (int i = testdata.numAttributes() - 1; i >= 0; i--) {
79            if (!attributeNames.contains(testdata.attribute(i).name())) {
80                testdata.deleteAttributeAt(i);
81                traindata.deleteAttributeAt(i);
82            }
83        }
84    }
85}
Note: See TracBrowser for help on using the repository browser.