Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. public static void Tree(String Path) throws Exception {//Path path for the arff file
  2. J48 tree = new J48(); // new instance of tree
  3. DataSource source = new DataSource(Path);
  4. Instances data = source.getDataSet();
  5. // setting class attribute if the data format does not provide this information
  6. // For example, the XRFF format saves the class attribute information as well
  7. if (data.classIndex() == -1) {
  8. data.setClassIndex(data.numAttributes() - 1);
  9. }
  10. tree.buildClassifier(data);
  11. System.out.println(tree.toString());
  12. }
  13.  
  14. System.out.println(tree.toString());
  15.  
  16. //Declaring attributes
  17. Attribute PT1 = new Attribute("PT1");
  18. Attribute w1 = new Attribute("w1");
  19. Attribute d1 = new Attribute("d1");
  20. Attribute PT2 = new Attribute("PT2");
  21. Attribute w2 = new Attribute("w2");
  22. Attribute d2 = new Attribute("d2");
  23.  
  24. // 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
  25. FastVector fvClassVal = new FastVector(2);
  26. fvClassVal.addElement("yes");
  27. fvClassVal.addElement("no");
  28. Attribute Class = new Attribute("ScheduledFirst", fvClassVal);
  29.  
  30. // Declare the feature vector
  31. FastVector fvWekaAttributes = new FastVector(7);
  32. // Add attributes
  33. fvWekaAttributes.addElement(PT1);
  34. fvWekaAttributes.addElement(w1);
  35. fvWekaAttributes.addElement(d1);
  36. fvWekaAttributes.addElement(PT2);
  37. fvWekaAttributes.addElement(w2);
  38. fvWekaAttributes.addElement(d2);
  39. fvWekaAttributes.addElement(Class);
  40. // Declare Instances which is required since I want to use classification/Prediction
  41. Instances dataset = new Instances("whatever", fvWekaAttributes, 0);
  42.  
  43. //Creating a double array and defining values
  44. double[] attValues = new double[dataset.numAttributes()];
  45. attValues[0] = 50;
  46. attValues[1] = 5;
  47. attValues[2] = 800;
  48. attValues[3] = 74;
  49. attValues[4] = 3;
  50. attValues[5] = 760;
  51.  
  52. //Create the new instance i1
  53. Instance i1 = new Instance(1.0, attValues);
  54. //Add the instance to the dataset (Instances) (first element 0)
  55. dataset.add(i1);
  56. //Define class attribute position
  57. dataset.setClassIndex(dataset.numAttributes()-1);
  58.  
  59. //Will print 0 if it's a "yes", and 1 if it's a "no"
  60. System.out.println(tree.classifyInstance(dataset.instance(0)));
  61. //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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement