Search code examples
javamatlabconstructorweka

Constructor error while creating an empty dataset in weka


I am trying to classify an instance using the classifyInstance method (described in weka's documentation here) using the Matlab environment.

This method require the instance to be link to a dataset. I am trying to use this constructor to create an empty dataset with the following matlab code:

import java.util.ArrayList.*;
import weka.core.*;
import weka.core.Instances.*;


attInfo = java.util.ArrayList;

attInfo.add(weka.core.Attribute('att1'));
attInfo.add(weka.core.Attribute('att2'));
attInfo.add(weka.core.Attribute('att3'));

dataset= weka.core.Instances(java.lang.String('relation'), attInfo, 2);

When I try to run this code matlab return me the following error:

No constructor 'weka.core.Instances' with matching signature found.

Error in file_name (line 109) dataset = weka.core.Instances(java.lang.String('relation'), attInfo, 5);

What is wrong with the parameters of my constructor?


Solution

  • I end up finding the solution of the problem. The constructor accept a signature which use the deprecated class FastVector. I just added a snapshot of my code in case it might help someone.

    attInfo = FastVector();
    
    attInfo.addElement(weka.core.Attribute('att1'));
    attInfo.addElement(weka.core.Attribute('att2'));
    attInfo.addElement(weka.core.Attribute('att3'));
    
    % build the class attribute
    classValues = FastVector();
    classValues.addElement(java.lang.String('0'));
    classValues.addElement(java.lang.String('1'));
    
    attInfo.addElement(Attribute('Class', classValues));
    
    % create the dataset and define the class attribute
    dataset = Instances('relation', attInfo, 1);
    dataset.setClassIndex(dataset.numAttributes() -1);
    
    % build the instance
    Inst = weka.core.Instance(10);
    for ii = 1:D.numAttributes()
        Inst.setValue(D.attribute(ii-1), 1)
    end
    Inst.setDataset(dataset)
    
    % classify the instance
    classifier.classifyInstance(Inst)
    

    The use of java object such as java.lang.String() also lead to an error.

    I am still curious about why this is happening, but I suspect that might be because of the version of weka that I am using (3.6.11) where the documentation might be for the version 3.7.12 .