javaweka

weka.core.UnassignedDatasetException: Instance doesn't have access to a dataset


I am implementing a basic machine learning code in java. I try to implement J48 decision tree learner. Here is the source code:

    public void ReadFile(){

    String csvFile = "~/animal1.arff";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";             
    Instances instances = null;        
    InfoGainAttributeEval eval = null;
    Ranker search = null;        
    AttributeSelection attSelect = null;   

    J48 tree =null;
    String[] options =null;

    try {
        eval = new InfoGainAttributeEval();    

        search = new Ranker();
        attSelect = new AttributeSelection();              

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

            attSelect.setEvaluator(eval);                      
            attSelect.setSearch(search);               
            instances = new Instances(br);      

            attSelect.SelectAttributes(instances);

            int[] indices = attSelect.selectedAttributes();
            System.out.println(Utils.arrayToString(indices));     


            tree = new J48();
            options = new String[1];
            options[0] = "-U";

            tree.setOptions(options);

            tree.buildClassifier(instances);

            System.out.println(tree);


            double[] vals = new double[instances.numAttributes()];
            vals[0] = 1.0;  //hair {false, true}
            vals[1] = 0.0;  //feathers {false, true}
            vals[2] = 0.0;  //eggs {false, true}
            vals[3] = 1.0;  //milk {false, true}
            vals[4] = 0.0;  //airborne {false, true}
            vals[5] = 0.0;  //aquatic {false, true}
            vals[6] = 0.0;  //predator {false, true}
            vals[7] = 1.0;  //toothed {false, true}
            vals[8] = 1.0;  //backbone {false, true}
            vals[9] = 1.0;  //breathes {false, true}
            vals[10] = 1.0;  //venomous {false, true}
            vals[11] = 0.0;  //fins {false, true}
            vals[12] = 4.0;  //legs INTEGER [0,9]
            vals[13] = 1.0;  //tail {false, true}
            vals[14] = 1.0;  //domestic {false, true}
            vals[15] = 0.0;  //catsize {false, true}

            Instance myUnicorn = new Instance(1.0, vals);

            double result = tree.classifyInstance(myUnicorn);

            System.out.println(myUnicorn.classAttribute().value((int) result));

        }


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

I am getting weka.core.UnassignedDatasetException: Instance doesn't have access to a dataset! exception in

double result = tree.classifyInstance(myUnicorn);

Could you please help me? Thanks in advance.


Solution

  • As Leonardo Foderaro said, Weka throws "UnassignedDatasetException" solves my problem.