Guest User

Untitled

a guest
Nov 20th, 2013
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3.     import weka.classifiers.Classifier;
  4.     import weka.core.Attribute;
  5.     import weka.core.DenseInstance;
  6.     import weka.core.Instances;
  7.     import weka.core.Utils;
  8.  
  9.  
  10.     public class UseModelWithData {
  11.  
  12.       public static void main(String[] args) throws Exception {
  13.         // load model
  14.         String rootPath = "G:/";
  15.         Classifier classifier = (Classifier) weka.core.SerializationHelper.read(rootPath+"j48.model");
  16.        
  17.         // create instances
  18.         Attribute attr1 = new Attribute("age");
  19.         Attribute attr2 = new Attribute("menopause");
  20.         Attribute attr3 = new Attribute("tumor-size");
  21.         Attribute attr4 = new Attribute("inv-nodes");
  22.         Attribute attr5 = new Attribute("node-caps");
  23.         Attribute attr6 = new Attribute("deg-malig");
  24.         Attribute attr7 = new Attribute("breast");
  25.         Attribute attr8 = new Attribute("breast-quad");
  26.         Attribute attr9 = new Attribute("irradiat");
  27.         Attribute attr10 = new Attribute("Class");
  28.        
  29.         ArrayList<Attribute> attributes = new ArrayList<Attribute>();
  30.         attributes.add(attr1);
  31.         attributes.add(attr2);
  32.         attributes.add(attr3);
  33.         attributes.add(attr4);
  34.         attributes.add(attr5);
  35.         attributes.add(attr6);
  36.         attributes.add(attr7);
  37.         attributes.add(attr8);
  38.         attributes.add(attr9);
  39.         attributes.add(attr10);
  40.  
  41.         // predict instance class values
  42.         Instances testing = new Instances("Test dataset", attributes, 0);
  43.        
  44.         // add data
  45.         double[] values = new double[testing.numAttributes()];
  46.         values[0] = testing.attribute(0).addStringValue("60-69");
  47.         values[1] = testing.attribute(1).addStringValue("ge40");
  48.         values[2] = testing.attribute(2).addStringValue("10-14");
  49.         values[3] = testing.attribute(3).addStringValue("15-17");
  50.         values[4] = testing.attribute(4).addStringValue("yes");
  51.         values[5] = testing.attribute(5).addStringValue("2");
  52.         values[6] = testing.attribute(6).addStringValue("right");
  53.         values[7] = testing.attribute(7).addStringValue("right_up");
  54.         values[8] = testing.attribute(0).addStringValue("yes");
  55.         values[9] = Utils.missingValue();
  56.        
  57.         // add data to instance
  58.         testing.add(new DenseInstance(1.0, values));
  59.         // instance row to predict
  60.         int index = 10;
  61.         // perform prediction
  62.         double myValue = classifier.classifyInstance(testing.instance(10));
  63.         // get the name of class value
  64.         String prediction = testing.classAttribute().value((int) myValue);
  65.        
  66.         System.out.println("The predicted value of the instance ["
  67.             + Integer.toString(index) + "]: " + prediction);
  68.        
  69.       }
  70.  
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment