Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Sep 12 15:39:43 2019
  5.  
  6. @author: kylepaulus
  7. """
  8.  
  9.  
  10. import monkdata as m
  11. import drawtree_qt5 as u
  12. import dtree as d
  13. from operator import itemgetter
  14. """
  15.  
  16. Compute the average gain across all 6 attributes and print results
  17.  
  18. """
  19.  
  20.  
  21. for i in range(6):
  22. print(' ' + 'A' + str(i+1)+' =' + ' ' + str(d.averageGain(m.monk1, m.attributes[i])))
  23.  
  24.  
  25. #print(d.averageGain(m.monk1, m.attributes[0]))
  26. #print(d.averageGain(m.monk1, m.attributes[1]))
  27. #print(d.averageGain(m.monk1, m.attributes[2]))
  28. #print(d.averageGain(m.monk1, m.attributes[3]))
  29. #print(d.averageGain(m.monk1, m.attributes[4]))
  30. #print(d.averageGain(m.monk1, m.attributes[5]))
  31.  
  32.  
  33. """
  34.  
  35. Create a for loop for all 4 subsets of
  36.  
  37.  
  38. """
  39.  
  40. #monk1_A51 = d.select(m.monk1, m.attributes[4],1)
  41. #monk1_A52 = d.select(m.monk1, m.attributes[4],2)
  42. #monk1_A53 = d.select(m.monk1, m.attributes[4],3)
  43. #monk1_A54 = d.select(m.monk1, m.attributes[4],4)
  44.  
  45. #import pandas as pd
  46. #test = pd.DataFrame()
  47. #for i in range(5):
  48. # for x in d.select(m.monk1, m.attributes[4],i):
  49. # test = test.append(pd.DataFrame(x.attribute, index=[0]))
  50. #test = test.reset_index()
  51. #
  52. #
  53. #
  54. #a = d.select(m.monk1, m.attributes[4],i)
  55.  
  56. for i in range(5):
  57. for x in d.select(m.monk1, m.attributes[4],i):
  58. print('Monk1'+ ' '+ 'A5' + '=' + str(i) + str(x.attribute))
  59.  
  60. """
  61.  
  62. Create a 4 loop that tests 6 attributes of A5 and computes average gain for
  63. each attribute, then prints a label = to the value
  64.  
  65. Nested for loop 1-4, and a for loop 1-6
  66.  
  67.  
  68. """
  69.  
  70. #print(d.averageGain(monk1_A51, m.attributes[0]))
  71. #print(d.averageGain(monk1_A51, m.attributes[1]))
  72. #print(d.averageGain(monk1_A51, m.attributes[2]))
  73. #print(d.averageGain(monk1_A51, m.attributes[3]))
  74. #print(d.averageGain(monk1_A51, m.attributes[4]))
  75. #print(d.averageGain(monk1_A51, m.attributes[5]))
  76. #
  77. #
  78. #print(d.averageGain(monk1_A52, m.attributes[0]))
  79. #print(d.averageGain(monk1_A52, m.attributes[1]))
  80. #print(d.averageGain(monk1_A52, m.attributes[2]))
  81. #print(d.averageGain(monk1_A52, m.attributes[3]))
  82. #print(d.averageGain(monk1_A52, m.attributes[4]))
  83. #print(d.averageGain(monk1_A52, m.attributes[5]))
  84. #
  85. #print(d.averageGain(monk1_A53, m.attributes[0]))
  86. #print(d.averageGain(monk1_A53, m.attributes[1]))
  87. #print(d.averageGain(monk1_A53, m.attributes[2]))
  88. #print(d.averageGain(monk1_A53, m.attributes[3]))
  89. #print(d.averageGain(monk1_A53, m.attributes[4]))
  90. #print(d.averageGain(monk1_A53, m.attributes[5]))
  91. #
  92. #print(d.averageGain(monk1_A54, m.attributes[0]))
  93. #print(d.averageGain(monk1_A54, m.attributes[1]))
  94. #print(d.averageGain(monk1_A54, m.attributes[2]))
  95. #print(d.averageGain(monk1_A54, m.attributes[3]))
  96. #print(d.averageGain(monk1_A54, m.attributes[4]))
  97. #print(d.averageGain(monk1_A54, m.attributes[5]))
  98.  
  99.  
  100. #creat an empty list to call
  101. monk_subsets = []
  102. for i in range(4):
  103. monk_subsets.append(d.select(m.monk1, m.attributes[4],i+1))
  104. #print(a5_list)
  105.  
  106.  
  107. for i in range(4):
  108. print('Monk1 A5 Subsets = ' + str(i+1))
  109. for j in range(6):
  110. print(' ' + 'Monk1'+ ' ' + 'A'+ str(j+1) + ' ' + '=' +
  111. str(d.averageGain(monk_subsets[i], m.attributes[j])))
  112.  
  113. #for set in monk_subsets:
  114. # for attributes in m.attributes:
  115. # print(' '+ str(d.averageGain(monk_subsets[i], m.attributes[i])))
  116.  
  117. """
  118.  
  119. Assignment 5 Build and draw a tree and compute error
  120.  
  121.  
  122. """
  123.  
  124. #t = d.buildTree(m.monk3, m.attributes)
  125. #error= 1-(d.check(t, m.monk3test))
  126. #print(error)
  127. #
  128. #
  129. #
  130. #s = d.allPruned(t)[11]
  131. #print(len(d.allPruned(s)))
  132. #
  133. #u.drawTree(s)
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. """
  141.  
  142. Assignment 6 write a loop that prunes the trees and keeps pruning until the
  143. model reaches a better value
  144.  
  145.  
  146. """
  147.  
  148. import random
  149. def partition(data, fraction):
  150. ldata = list(data)
  151. random.shuffle(ldata)
  152. breakPoint = int(len(ldata) * fraction)
  153. return ldata[:breakPoint], ldata[breakPoint:]
  154.  
  155.  
  156. monk1train, monk1val = partition(m.monk1, 0.6)
  157.  
  158. """
  159. while (all pruned function)error >= error(original tree)
  160. compute(error (all pruned))
  161. print(tree)
  162.  
  163. """
  164. original_tree= d.buildTree(monk1train, m.attributes)
  165. #pruned_tree = d.allPruned(original_tree)[0]
  166. #error_original_tree = 1-(d.check(original_tree, monk1val))
  167. #print(error_original_tree)
  168. #error_pruned_tree = 1 - (d.check(pruned_tree, monk1val))
  169. #print(error_pruned_tree)
  170. #
  171. #for i in range(100):
  172. # while error_original_tree <= error_pruned_tree:
  173. # pruned_tree = d.allPruned(original_tree)[i]
  174. # print(error_pruned_tree)
  175.  
  176.  
  177.  
  178. """
  179. Input: Tree and a validation set.
  180. Returns a sorted list of tuples with pruned trees and accuracy like:
  181. [(tree, 0.8),(tree2, 0.85),...]
  182. """
  183. def makelist(origtree, val):
  184. lst = d.allPruned(origtree)
  185. output = []
  186. for x in lst:
  187. output.append((x,d.check(x,val)))
  188. output = sorted(output,key=itemgetter(1))
  189. return output
  190.  
  191.  
  192. print(makelist(original_tree, monk1val))
  193.  
  194. """
  195.  
  196. Looks if the performance of a tree can be improved by pruning recursively.
  197. Returns error number of the improved tree.
  198.  
  199.  
  200. """
  201. def error_of_improved_tree(original_tree,val):
  202. error = 1 - d.check(original_tree, val)
  203. lst = makelist(original_tree, val)
  204. if d.check(original_tree, val) > lst[-1][1]:
  205. return error
  206. else:
  207. return error_of_improved_tree(lst[-1][0],val)
  208.  
  209. print(error_of_improved_tree(original_tree,monk1val))
  210.  
  211.  
  212.  
  213. def biggie(training, partition_parameter):
  214. lst = []
  215. for i in range(0,10):
  216. train, val = partition(training, partition_parameter)
  217. #original_tree = d.buildTree(train, m.attributes)
  218. lst.append(error_of_improved_tree)
  219. return lst
  220.  
  221. print(biggie(m.monk1, 0.3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement