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 |
|
---|
15 | package de.ugoe.cs.cpdp.dataprocessing;
|
---|
16 |
|
---|
17 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
18 |
|
---|
19 | import weka.core.Instances;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Removes an attributes from all data sets using their name.
|
---|
23 | *
|
---|
24 | * @author Steffen Herbold
|
---|
25 | */
|
---|
26 | public class AttributeRemoval implements ISetWiseProcessingStrategy, IProcessesingStrategy {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * names of the attributes to be removed (determined by {@link #setParameter(String)})
|
---|
30 | */
|
---|
31 | private String[] attributeNames = new String[] { };
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Sets that attributes that will be removed. The string contains the blank-separated names of
|
---|
35 | * the attributes to be removed. <br>
|
---|
36 | * <br>
|
---|
37 | * Note, that removal of attributes with blanks is currently not supported!
|
---|
38 | *
|
---|
39 | * @param parameters
|
---|
40 | * string with the blank-separated attribute names
|
---|
41 | */
|
---|
42 | @Override
|
---|
43 | public void setParameter(String parameters) {
|
---|
44 | if (parameters != null) {
|
---|
45 | attributeNames = parameters.split(" ");
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances,
|
---|
51 | * org.apache.commons.collections4.list.SetUniqueList)
|
---|
52 | */
|
---|
53 | @Override
|
---|
54 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
55 | for (String attributeName : attributeNames) {
|
---|
56 | for (int i = 0; i < testdata.numAttributes(); i++) {
|
---|
57 | if (attributeName.equals(testdata.attribute(i).name())) {
|
---|
58 | testdata.deleteAttributeAt(i);
|
---|
59 | for (Instances traindata : traindataSet) {
|
---|
60 | traindata.deleteAttributeAt(i);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances,
|
---|
69 | * weka.core.Instances)
|
---|
70 | */
|
---|
71 | @Override
|
---|
72 | public void apply(Instances testdata, Instances traindata) {
|
---|
73 | for (String attributeName : attributeNames) {
|
---|
74 | for (int i = 0; i < testdata.numAttributes(); i++) {
|
---|
75 | if (attributeName.equals(testdata.attribute(i).name())) {
|
---|
76 | testdata.deleteAttributeAt(i);
|
---|
77 | traindata.deleteAttributeAt(i);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|