feature-extractionfeature-selectionarff

How to remove particular attributes from arff file and produce modified arff?


(not manually) i have 96 features and want to remove some 20 features from arff and produce modified arff. used weka for feature selection now want to remove those less imp features. can anyone suggest code for this


Solution

  • Here you go... just change the source and destination file path...

    import java.io.File;
    import weka.core.Instances;
    import weka.core.converters.ArffLoader;
    import weka.core.converters.ArffSaver;
    import weka.filters.Filter;
    import weka.filters.unsupervised.attribute.Remove;
    
    
    public class Convert4 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try
            {
                ArffLoader loader2= new ArffLoader();
                loader2.setSource(new File("C:/Users/RAHUL/Desktop/stack.arff"));
                Instances data2= loader2.getDataSet();
                //Load Arff
                 String[] options = new String[2];
                 options[0] = "-R";                                    // "range"
                 options[1] = "1";                                     // first attribute
                 Remove remove = new Remove();                         // new instance of filter
                 remove.setOptions(options);                           // set options
                 remove.setInputFormat(data2);                          // inform filter about dataset **AFTER** setting options
                 Instances newData2 = Filter.useFilter(data2, remove);   // apply filter
                 ArffSaver saver = new ArffSaver();
                 saver.setInstances(newData2);
                 saver.setFile(new File("C:/Users/RAHUL/Desktop/stack2.arff"));
                 saver.writeBatch();
    }
    catch (Exception e)
    {}
    }
    }
    

    Cheers :)