Formatting Data for the Decision Tree Generator

In my last post I explained a bit about what needs to be passed to the decision tree generator’s constructor function, but I didn’t go into detail about how to get the numbers into the formats specified. Luckily it’s very easy! I have included a function in the decision tree generator sketch which will take a properly formatted text file and and automatically turn it into the column array of integers that you need. Also in the example sketch folder I have included some such text files as examples. Essentially you will need one text file each for the output array and for each of your attributes. The text file should be formatted to contain only the number 0 through however many levels that particular attribute or output can take on. Each number should be on its own line and the file should contain no punctuation. This may sound tedious to make, but Microsoft Excel, Matlab and many other free spreadsheet programs can save a column of numbers in just such a format by saving the file as a .txt or .csv. The text file just needs to be placed in the same folder as the Processing .pde file (the sketch itself) and it should be good to go. Now you should be ready to follow this example and make your decision tree! The example shows generically how to load the output, three attributes and then create their labels and variables. The variable and text file names can obviously re-named to anything you may want.

  //*****************
  // Load a text file with separate values (just an integer number) on each line
  // the text file is converted to an integer array which can be input to the decision tree generator

  int[] output = loadData("outputs.txt");
  int[] attribute0 = loadData("attribute0.txt");
  int[] attribute1 = loadData("attribute1.txt");
  int[] attribute2 = loadData("attribute2.txt");

  //******************
  // OUTPUT Information
  String[] output_labels = {"out_label1", "out_label2"};

  // INPUT #0 / Attribute 0
  int num_attribute0_levels = 3;   // 0 = level0, 1 = level1, 2 = level2
  String[] attribute0_labels = {"level0", "level1", "level2"};

  // INPUT #1 / Attribute 1
  int num_attribute1_levels = 2;   // 0 = false, 1 = true
  String[] attribute1_labels = {"false", "true"};

  // INPUT #2 / Attribute 2
  int num_attribute2_levels = 4;  // 0 = low, 1 = med, 2 = high
  String[] attribute2_labels = {"Low", "Med", "High"};

  // INPUT Information
  int[][] input_attributes = {attribute0, attribute1, attribute2};
  String[] attribute_labels = {"Attribute_0", "Attribute_1", "Attribute_2"};
  String[][] attribute_level_labels = {attribute1_labels, attribute2_labels, attribute3_labels};
  Decision_Tree tree;
  int leave_out = 2;
  int[] node = {1, 1};
  float screen_width = width;

Leave a comment