Weka – Include / Exclude attributes

In Weka, there may be times when one wants to remove attributes from the data, through code. While I googled and did not find any, I set upon writing my own, Here is the snippet.

For example the attributes are there in a file (attributes.txt), each line is an attribute, we read each attribute and find the corresponding index in the data(ARFF buffer bbcsport.arff).

We then use Remove() class setAttributeIndicesArray() to set the array of indices to be either included or excluded by setting true or false in method

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

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

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

filtered contains the filtered data, use it now any which way!!

Hope this might help someone!