weka Loading Instances Loading ARFF Files


Example

Depending on the version of Weka being used different methods for loading ARFF files should be utilised.

Weka <3.5.5

The following sample code shows how to load an ARFF file:

import weka.core.Instances;
import java.io.BufferedReader;
import java.io.FileReader;
...
BufferedReader reader = new BufferedReader(new FileReader("data.arff"));
Instances data = new Instances(reader);
reader.close();
data.setClassIndex(data.numAttributes() - 1);

The class index shows what attribute should be used for classification. In most ARFF files this is the last attribute which is why it is set to data.numAttributes() - 1. If you are using a Weka function, such as buildClassifier, you must set the class index.

Weka >=3.5.5

In the latest version of Weka it is very easy to load an ARFF file. This method can also load CSV files and any other files Weka can understand.

import weka.core.converters.ConverterUtils.DataSource;
...
DataSource source = new DataSource("data.arff");
Instances data = source.getDataSet();
if (data.classIndex() == -1) {
  data.setClassIndex(data.numAttributes() - 1);
}