0

(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

  • Are you using Weka GUI or using Weka in your Java code? – lanenok May 07 '16 at 09:31
  • @lanenok I have used weka for feature selection and now i want to make a modified dataset. But i need a code for removing attributes –  May 07 '16 at 14:06
  • 1
    Possible duplicate of [Skip feature when classifying, but show feature in output](http://stackoverflow.com/questions/10047409/skip-feature-when-classifying-but-show-feature-in-output) – Sivakumar D May 15 '17 at 07:18

2 Answers2

2

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 :)

Rahul Rajput
  • 284
  • 1
  • 3
  • 15
  • i am new to weka thank you so much. i understood about input and output arff. but can you gimme some eg for mentioning range and removing particular attribute for eg some 15 attribute. thanks in advance. –  May 07 '16 at 17:08
  • You can use options[1] = "1-2"; if you want to remove 1 and 2. So if you want to remove from 1-15 use options[1] = "1-15"; – Rahul Rajput May 07 '16 at 17:14
  • Also tick the answer as accepted. If it answers your question. Welcome :) – Rahul Rajput May 07 '16 at 17:15
  • i am able to remove range of attributes from "1-15" but in my arff file i want to remove attribute which doesnt come in range. for eg, from 98 attributes i want to remove 15 attributes and they are like 10, 15,96,44, etc –  May 07 '16 at 20:54
  • @kumar just look at the documentation. You have to separate indexes using comma: https://weka.sourceforge.io/doc.dev/weka/filters/unsupervised/attribute/Remove.html#setOptions-java.lang.String:A- – FonzTech Feb 02 '20 at 15:20
1

Short answer is here for more, check this out https://stackoverflow.com/a/43972890/7588668

BufferedReader datafile = new BufferedReader(new FileReader("bbcsport.arff")); 
BufferedReader attrfile = new BufferedReader(new FileReader("attributes.txt"));


Instances data = new Instances(datafile); 
List<Integer> myList = new ArrayList<Integer>();
String line;

while ((line = attrfile.readLine()) != null) {
  for (n = 0; n < data.numAttributes(); n++) {
    if (data.attribute(n).name().equalsIgnoreCase(line)) {
      if(!myList.contains(n)) 
        myList.add(n); 
    } 
  }
}

int[] attrs = myList.stream().mapToInt(i -> i).toArray();
Remove remove = new Remove();
remove.setAttributeIndicesArray(attrs);
remove.setInvertSelection(false);
remove.setInputFormat(data); // init filter

Instances filtered = Filter.useFilter(data, remove);
Community
  • 1
  • 1