Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

otest.py

By: a guest on Mar 18th, 2010  |  syntax: Python  |  size: 1.08 KB  |  hits: 114  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import orange, orngC45, sys, pickle
  4.  
  5. def make_tree(fname, memory, remember):
  6.         data = orange.ExampleTable(fname)
  7.         print fname, data.domain.classVar.values
  8.         tree = orange.C45Learner(data)
  9.         if remember: memory.append(tree)
  10.         tfname = fname  + '.tree'
  11.         f = open(tfname, 'w')
  12.         pickle.dump(tree, f)
  13.         f.close()
  14.         print 'Tree saved to', tfname
  15.         print 'classVar:', tree.classVar.values
  16.  
  17. def load_tree(fname, memory, remember):
  18.         tfname = fname  + '.tree'
  19.         f = open(tfname)
  20.         tree = pickle.load(f)
  21.         if remember: memory.append(tree)
  22.         f.close()
  23.         print 'Tree loaded from', tfname
  24.         print 'classVar:', tree.classVar.values
  25.         orngC45.printTree(tree)
  26.  
  27. if __name__ == '__main__':
  28.         f_data1 = 'zoo.tab'
  29.         f_data2 = 'zoo2.tab'
  30.         memory = []
  31.         if len(sys.argv) > 1:
  32.                 remember = 'r' in sys.argv[1]
  33.                 for c in sys.argv[1]:
  34.                         if c == '1':
  35.                                 make_tree(f_data1, memory, remember)
  36.                         elif c == '2':
  37.                                 make_tree(f_data2, memory, remember)
  38.                         elif c == 'a':
  39.                                 load_tree(f_data1, memory, remember)
  40.                         elif c == 'b':
  41.                                 load_tree(f_data2, memory, remember)