Zahzah
Zahzah

Reputation: 81

Create and classify new instance Weka

I've created a Decision Tree (J48) using Weka API and Java. First I train my decision tree using an arff file.

public static void Tree(String Path) throws Exception {//Path path for the arff file
    J48 tree = new J48();         // new instance of tree
    DataSource source = new DataSource(Path);
    Instances data = source.getDataSet();
    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }
    tree.buildClassifier(data);
    System.out.println(tree.toString());
}

The used arff file includes 780 instances. Each instance has 6 attributes {PT1, w1, d1, PT2, w2, d2} all numeric and a class {yes, no}. My code is working and I can see the resulting Decision Tree using

System.out.println(tree.toString());

Now, I want to create a new Instance (not using another arff file) and classify this new instance. Say the values for this new instance are, for example, {50, 5, 800, 74, 3, 760}. Then the Decision Tree must return the corresponding class (a "yes" or a "no").

Upvotes: 1

Views: 4254

Answers (1)

Zahzah
Zahzah

Reputation: 81

I found a solution to my problem, and I hope it'll be useful.

    //Declaring attributes
    Attribute PT1 = new Attribute("PT1");
    Attribute w1 = new Attribute("w1");
    Attribute d1 = new Attribute("d1");
    Attribute PT2 = new Attribute("PT2");
    Attribute w2 = new Attribute("w2");
    Attribute d2 = new Attribute("d2");

    // Declare the class attribute along with its values contains two nominal values yes and no using FastVector. "ScheduledFirst" is the name of the class attribute        
    FastVector fvClassVal = new FastVector(2);
    fvClassVal.addElement("yes");
    fvClassVal.addElement("no");
    Attribute Class = new Attribute("ScheduledFirst", fvClassVal);

    // Declare the feature vector
    FastVector fvWekaAttributes = new FastVector(7);
    // Add attributes 
    fvWekaAttributes.addElement(PT1);
    fvWekaAttributes.addElement(w1);
    fvWekaAttributes.addElement(d1);
    fvWekaAttributes.addElement(PT2);
    fvWekaAttributes.addElement(w2);
    fvWekaAttributes.addElement(d2);
    fvWekaAttributes.addElement(Class);
    // Declare Instances which is required since I want to use classification/Prediction
    Instances dataset = new Instances("whatever", fvWekaAttributes, 0);

    //Creating a double array and defining values
    double[] attValues = new double[dataset.numAttributes()];
    attValues[0] = 50;
    attValues[1] = 5;
    attValues[2] = 800;
    attValues[3] = 74;
    attValues[4] = 3;
    attValues[5] = 760;

    //Create the new instance i1
    Instance i1 = new Instance(1.0, attValues);
    //Add the instance to the dataset (Instances) (first element 0)        
    dataset.add(i1);
    //Define class attribute position
    dataset.setClassIndex(dataset.numAttributes()-1);

    //Will print 0 if it's a "yes", and 1 if it's a "no"
    System.out.println(tree.classifyInstance(dataset.instance(0)));
    //Here I call dataset.instance(0) since there is only one instance added in the dataset, if you do add another one you can use dataset.instance(0), etc.

Upvotes: 4

Related Questions