Weka – Include / Exclude attributes

ML, Weka
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 remove.setInvertSelection(false). 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…
Read More