ivanakarpuzova

Untitled

Sep 8th, 2019
200
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 1 0
  1. trainingData=[['slashdot','USA','yes',18,'None'],
  2. ['google','France','yes',23,'Premium'],
  3. ['google','France','yes',23,'Basic'],
  4. ['google','France','yes',23,'Basic'],
  5. ['digg','USA','yes',24,'Basic'],
  6. ['kiwitobes','France','yes',23,'Basic'],
  7. ['google','UK','no',21,'Premium'],
  8. ['(direct)','New Zealand','no',12,'None'],
  9. ['(direct)','UK','no',21,'Basic'],
  10. ['google','USA','no',24,'Premium'],
  11. ['slashdot','France','yes',19,'None'],
  12. ['digg','USA','no',18,'None'],
  13. ['google','UK','no',18,'None'],
  14. ['kiwitobes','UK','no',19,'None'],
  15. ['digg','New Zealand','yes',12,'Basic'],
  16. ['slashdot','UK','no',21,'None'],
  17. ['google','UK','yes',18,'Basic'],
  18. ['kiwitobes','France','yes',19,'Basic']]
  19.  
  20.  
  21. class decisionnode:
  22. def __init__(self, col=-1, value=None, results=None, tb=None, fb=None):
  23. self.col = col
  24. self.value = value
  25. self.results = results
  26. self.tb = tb
  27. self.fb = fb
  28.  
  29.  
  30.  
  31. def sporedi_broj(value1, value2):
  32. return value1 >= value2
  33.  
  34.  
  35. def sporedi_string(value1, value2):
  36. return value1 == value2
  37.  
  38.  
  39. # In[43]:
  40.  
  41.  
  42. def get_compare_func(value):
  43. if isinstance(value, int) or isinstance(value, float):
  44. comparer = sporedi_broj
  45. else:
  46. comparer = sporedi_string
  47. return comparer
  48.  
  49.  
  50. def compare_values(v1, v2):
  51. sporedi = get_compare_func(v1)
  52. return sporedi(v1, v2)
  53.  
  54.  
  55. def divideset(rows, column, value):
  56. sporedi = get_compare_func(value)
  57. # print(split_function)
  58. # Divide the rows into two sets and return them
  59. set_false = []
  60. set_true = []
  61. for row in rows:
  62. uslov = sporedi(row[column], value)
  63. # print(column, value, row[column], uslov, row)
  64. if uslov:
  65. set_true.append(row)
  66. else:
  67. set_false.append(row)
  68. # print(len(set_true), len(set_false))
  69. # set_true = [row for row in rows if
  70. # split_function(row, column, value)] # za sekoj row od rows za koj split_function vrakja true
  71. # set_false = [row for row in rows if
  72. # not split_function(row, column, value)] # za sekoj row od rows za koj split_function vrakja false
  73. return (set_true, set_false)
  74.  
  75.  
  76. def uniquecounts(rows):
  77. d = {}
  78. for r in rows:
  79. # print(r[-1])
  80. d.setdefault(r[-1], 0)
  81. d[r[-1]] += 1
  82. return d
  83.  
  84.  
  85. from math import log
  86.  
  87. log2 = lambda x: log(x) / log(2)
  88.  
  89.  
  90. def entropy(rows):
  91. results = uniquecounts(rows)
  92. # Now calculate the entropy
  93. ent = 0.0
  94. n = len(rows)
  95. for label, cnt in results.items():
  96. # print(r)
  97. p = float(cnt) / n
  98. # print(label, cnt, p)
  99. ent -= p * log2(p)
  100. return ent
  101.  
  102.  
  103. def info_gain(current_score, sets, scoref=entropy):
  104. m = sum([len(s) for s in sets])
  105. gain = current_score
  106. for s in sets:
  107. n = len(s)
  108. p = 1. * n / m
  109. gain -= p * scoref(s)
  110. return gain
  111.  
  112.  
  113. def buildtree(rows, scoref=entropy):
  114. if len(rows) == 0: return decisionnode()
  115. current_score = scoref(rows)
  116.  
  117. # Set up some variables to track the best criteria
  118. best_gain = 0.0
  119. best_criteria = None
  120. best_sets = None
  121.  
  122. column_count = len(rows[0]) - 1
  123. for col in range(0, column_count):
  124. # Generate the list of different values in
  125. # this column
  126. column_values = {}
  127. for row in rows:
  128. column_values[row[col]] = 1
  129.  
  130. # Now try dividing the rows up for each value
  131. # in this column
  132. for value in column_values.keys():
  133. (set1, set2) = divideset(rows, col, value)
  134.  
  135. # Information gain
  136. p = float(len(set1)) / len(rows)
  137. gain = current_score - p * scoref(set1) - (1 - p) * scoref(set2)
  138. if gain > best_gain and len(set1) > 0 and len(set2) > 0:
  139. best_gain = gain
  140. best_criteria = (col, value)
  141. best_sets = (set1, set2)
  142.  
  143. # Create the subbranches
  144. if best_gain > 0:
  145. trueBranch = buildtree(best_sets[0])
  146. falseBranch = buildtree(best_sets[1])
  147. return decisionnode(col=best_criteria[0], value=best_criteria[1], tb=trueBranch, fb=falseBranch)
  148. else:
  149. return decisionnode(results=uniquecounts(rows))
  150.  
  151.  
  152. def printtree(tree, indent='',level=0):
  153. # Is this a leaf node?
  154. if tree.results != None:
  155. print(tree.results)
  156. else:
  157. # Print the criteria
  158. print(str(tree.col) + ':' + str(tree.value) + '? ' +'Level=%d' % (level))
  159. # Print the branches
  160. print(indent + 'T->'),
  161. printtree(tree.tb,indent +' ',level+1)
  162. print(indent+'F->'),
  163. printtree(tree.fb,indent+' ',level+1)
  164.  
  165.  
  166. def classify(observation, tree):
  167. if tree.results != None:
  168. # return tree.results
  169. if len(tree.results) == 1:
  170. return list(tree.results.keys())[0], 1.0
  171. else:
  172. inv = sorted([(cnt, label) for label, cnt in tree.results.items()], reverse=True)
  173. label = inv[0][1]
  174. cnt = inv[0][0]
  175. total_count = sum(tree.results.values())
  176. return label, cnt / total_count
  177. # return tree.results
  178. else:
  179. vrednost = observation[tree.col]
  180. if compare_values(vrednost, tree.value):
  181. branch = tree.tb
  182. else:
  183. branch = tree.fb
  184. return classify(observation, branch)
  185.  
  186.  
  187. if __name__ == "__main__":
  188. # referrer='slashdot'
  189. # location='US'
  190. # readFAQ='no'
  191. # pagesVisited=19
  192. # serviceChosen='None'
  193.  
  194. referrer=input()
  195. location=input()
  196. readFAQ=input()
  197. pagesVisited=input()
  198. serviceChosen=input()
  199.  
  200. testCase=[referrer, location, readFAQ, pagesVisited, serviceChosen]
  201. trainingData.append(testCase)
  202. t=buildtree(trainingData)
  203. printtree(t)
Advertisement
Add Comment
Please, Sign In to add comment