0
votes

I have the following sparse ARFF File in Weka – I want to build a classifier from the given sparse ARFF file (training dataset) using a Weka Java API. The program is reading the file [not throwing any Exception] but not able to read the instances. When I print the number of instances – the program prints as 0. Thanks in advance for inputs.

@RELATION ample

@ATTRIBUTE T1 numeric

@ATTRIBUTE T2 numeric

@ATTRIBUTE T3 numeric

@ATTRIBUTE T4 numeric

@ATTRIBUTE T5 numeric

@ATTRIBUTE C1 {0, 1}

@DATA

{0 3, 1 2, 2 1, 6 1}

{3 3, 4 2, 6 0}

ArffLoader loader = new ArffLoader();    
loader.setFile(new File("C:\\SAMPLE-01.arff"));    
Instances data = loader.getStructure();    
data.setClassIndex(data.numAttributes() - 1);    
System.out.println("Number of Attributes : " + data.numAttributes());    
System.out.println("Number of Instances : " + data.numInstances());    
1

1 Answers

0
votes

I believe that your sparse data is not properly formatted in your ARFF file. It should be something like this:

@RELATION ample
@ATTRIBUTE T1 numeric
@ATTRIBUTE T2 numeric
@ATTRIBUTE T3 numeric
@ATTRIBUTE T4 numeric
@ATTRIBUTE T5 numeric
@ATTRIBUTE C1 {0, 1}

@DATA
{0 3, 1 2, 2 1, 5 1}
{3 3, 4 2, 5 0}

And then you can use a class somewhat that looks like mine:

public class SO_Test {
    DataSource source = null;
    Instances data = null;
    public void setDataset(String trainingFile){
        try {
            source = new DataSource(trainingFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            data = source.getDataSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (data.classIndex() == -1)
            data.setClassIndex(data.numAttributes() - 1);
    }
    public static void main(String[] args) throws Exception{
        SO_Test s = new SO_Test();
        s.setDataset("1.arff");
        System.out.println("Number of Attributes : " + s.data.numAttributes());    
        System.out.println("Number of Instances : " + s.data.numInstances());
    }
}