1 | package de.ugoe.cs.cpdp.dataprocessing;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
6 |
|
---|
7 | import weka.core.Instances;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Removes attributes from all data sets, except the one defined, using their name.
|
---|
11 | * @author Fabian Trautsch
|
---|
12 | */
|
---|
13 | public class AttributeNonRemoval implements ISetWiseProcessingStrategy, IProcessesingStrategy {
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * names of the attributes to be kept (determined by {@link #setParameter(String)})
|
---|
17 | */
|
---|
18 | private ArrayList<String> attributeNames = new ArrayList<String>();
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Sets that attributes that will be kept. The string contains the blank-separated names of the attributes to be kept.
|
---|
22 | * <br><br>
|
---|
23 | * Note, that keeping of attributes with blanks is currently not supported!
|
---|
24 | * @param parameters string with the blank-separated attribute names
|
---|
25 | */
|
---|
26 | @Override
|
---|
27 | public void setParameter(String parameters) {
|
---|
28 | if( parameters!=null ) {
|
---|
29 | String[] attributeNamesArray = parameters.split(" ");
|
---|
30 | for(String attributeName : attributeNamesArray) {
|
---|
31 | attributeNames.add(attributeName);
|
---|
32 | }
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances, org.apache.commons.collections4.list.SetUniqueList)
|
---|
38 | */
|
---|
39 | @Override
|
---|
40 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
41 | for( String attributeName : attributeNames ) {
|
---|
42 | for( int i=0 ; i<testdata.numAttributes() ; i++ ) {
|
---|
43 | if(!attributeName.equals(testdata.attribute(i).name()) ) {
|
---|
44 | testdata.deleteAttributeAt(i);
|
---|
45 | for( Instances traindata : traindataSet ) {
|
---|
46 | traindata.deleteAttributeAt(i);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances, weka.core.Instances)
|
---|
55 | */
|
---|
56 | @Override
|
---|
57 | public void apply(Instances testdata, Instances traindata) {
|
---|
58 | for(int i=testdata.numAttributes()-1; i>=0; i--) {
|
---|
59 | if(!attributeNames.contains(testdata.attribute(i).name())) {
|
---|
60 | testdata.deleteAttributeAt(i);
|
---|
61 | traindata.deleteAttributeAt(i);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|