[86] | 1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany
|
---|
[41] | 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 |
|
---|
[32] | 15 | package de.ugoe.cs.cpdp.dataprocessing;
|
---|
| 16 |
|
---|
| 17 | import java.util.ArrayList;
|
---|
| 18 |
|
---|
| 19 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
| 20 |
|
---|
| 21 | import weka.core.Instances;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
[41] | 24 | * Removes attributes from all data sets, except the one defined, using their name.
|
---|
| 25 | *
|
---|
[32] | 26 | * @author Fabian Trautsch
|
---|
| 27 | */
|
---|
| 28 | public class AttributeNonRemoval implements ISetWiseProcessingStrategy, IProcessesingStrategy {
|
---|
| 29 |
|
---|
[41] | 30 | /**
|
---|
| 31 | * names of the attributes to be kept (determined by {@link #setParameter(String)})
|
---|
| 32 | */
|
---|
| 33 | private ArrayList<String> attributeNames = new ArrayList<String>();
|
---|
[32] | 34 |
|
---|
[41] | 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 | }
|
---|
[32] | 53 |
|
---|
[41] | 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 | }
|
---|
[32] | 85 | }
|
---|