title: Predicting data created on-the-fly in WEKA using a premade model file
body: <p>I want to create a WEKA Java program that reads a group of newly created data that will be fed to a premade model from the GUI version.</p>
  
  <p>Here is the program:</p>
  
  <pre><code>import java.util.ArrayList;
  import weka.classifiers.Classifier;
  import weka.core.Attribute;
  import weka.core.DenseInstance;
  import weka.core.Instances;
  import weka.core.Utils;
  
  
  public class UseModelWithData {
  
    public static void main(String[] args) throws Exception {
      // load model
      String rootPath = "G:/";
      Classifier classifier = (Classifier) weka.core.SerializationHelper.read(rootPath+"j48.model");
  
      // create instances
      Attribute attr1 = new Attribute("age");
      Attribute attr2 = new Attribute("menopause");
      Attribute attr3 = new Attribute("tumor-size");
      Attribute attr4 = new Attribute("inv-nodes");
      Attribute attr5 = new Attribute("node-caps");
      Attribute attr6 = new Attribute("deg-malig");
      Attribute attr7 = new Attribute("breast");
      Attribute attr8 = new Attribute("breast-quad");
      Attribute attr9 = new Attribute("irradiat");
      Attribute attr10 = new Attribute("Class");
  
      ArrayList&lt;Attribute&gt; attributes = new ArrayList&lt;Attribute&gt;();
      attributes.add(attr1);
      attributes.add(attr2);
      attributes.add(attr3);
      attributes.add(attr4);
      attributes.add(attr5);
      attributes.add(attr6);
      attributes.add(attr7);
      attributes.add(attr8);
      attributes.add(attr9);
      attributes.add(attr10);
  
      // predict instance class values
      Instances testing = new Instances("Test dataset", attributes, 0);
  
      // add data
      double[] values = new double[testing.numAttributes()];
      values[0] = testing.attribute(0).addStringValue("60-69");
      values[1] = testing.attribute(1).addStringValue("ge40");
      values[2] = testing.attribute(2).addStringValue("10-14");
      values[3] = testing.attribute(3).addStringValue("15-17");
      values[4] = testing.attribute(4).addStringValue("yes");
      values[5] = testing.attribute(5).addStringValue("2");
      values[6] = testing.attribute(6).addStringValue("right");
      values[7] = testing.attribute(7).addStringValue("right_up");
      values[8] = testing.attribute(0).addStringValue("yes");
      values[9] = Utils.missingValue();
  
      // add data to instance
      testing.add(new DenseInstance(1.0, values));
      // instance row to predict
      int index = 10;
      // perform prediction
      double myValue = classifier.classifyInstance(testing.instance(10));
      // get the name of class value
      String prediction = testing.classAttribute().value((int) myValue);
  
      System.out.println("The predicted value of the instance [" 
          + Integer.toString(index) + "]: " + prediction);
  
    }
  
  }
  </code></pre>
  
  <p>My references include:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/20017957/how-to-reuse-saved-classifier-created-from-explorerin-weka-in-eclipse-java">Using a premade WEKA model in Java</a></li>
  <li>the WEKA Manual provided in the 3.7.10 version - <strong>17.3 Creating datasets in memory</strong></li>
  <li><a href="https://stackoverflow.com/questions/16450958/java-how-can-i-assemble-create-a-single-instance-for-classification-using-a-wek">Creating a single instance for classification in WEKA </a></li>
  </ul>
  
  <p>So far the part where I create a new <code>Instance</code> inside the script causes the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 1
  </code></pre>
  
  <p>in the line </p>
  
  <pre><code>double myValue = classifier.classifyInstance(testing.instance(10));
  </code></pre>
  
  <p><strong>I just want to use a latest row of instance values to a premade WEKA model. How do I solve this?</strong></p>
  
  <hr>
  
  <p><strong>Resources</strong></p>
  
  <ul>
  <li><a href="http://pastebin.com/C0wY3Ar2" rel="nofollow noreferrer">Program file</a></li>
  <li><a href="http://pastebin.com/K6hqPnhc" rel="nofollow noreferrer">Arff file</a></li>
  <li><a href="http://www.fileconvoy.com/dfl.phpid=gc0d51e1bbf551644999416893dcbcd1eaa372e202" rel="nofollow noreferrer">j48.model</a></li>
  </ul>