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

Last change on this file since 40 was 32, checked in by ftrautsch, 10 years ago

integrating decent into crosspare

  • Property svn:mime-type set to text/plain
File size: 2.1 KB
Line 
1package de.ugoe.cs.cpdp.dataprocessing;
2
3import java.util.ArrayList;
4
5import org.apache.commons.collections4.list.SetUniqueList;
6
7import weka.core.Instances;
8
9/**
10 * Removes attributes from all data sets, except the one defined, using their name.
11 * @author Fabian Trautsch
12 */
13public 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}
Note: See TracBrowser for help on using the repository browser.