ivanakarpuzova

Untitled

Sep 8th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 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(object):
  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. def sporedi_broj(value1, value2):
  31. return value1 >= value2
  32.  
  33.  
  34. def sporedi_string(value1, value2):
  35. return value1 == value2
  36.  
  37.  
  38. # In[43]:
  39.  
  40.  
  41. def get_compare_func(value):
  42. if isinstance(value, int) or isinstance(value, float):
  43. comparer = sporedi_broj
  44. else:
  45. comparer = sporedi_string
  46. return comparer
  47.  
  48.  
  49. def compare_values(v1, v2):
  50. sporedi = get_compare_func(v1)
  51. return sporedi(v1, v2)
  52.  
  53.  
  54. def divideset(rows, column, value):
  55. sporedi = get_compare_func(value)
  56. # print(split_function)
  57. # Divide the rows into two sets and return them
  58. set_false = []
  59. set_true = []
  60. for row in rows:
  61. uslov = sporedi(row[column], value)
  62. # print(column, value, row[column], uslov, row)
  63. if uslov:
  64. set_true.append(row)
  65. else:
  66. set_false.append(row)
  67. # print(len(set_true), len(set_false))
  68. # set_true = [row for row in rows if
  69. # split_function(row, column, value)] # za sekoj row od rows za koj split_function vrakja true
  70. # set_false = [row for row in rows if
  71. # not split_function(row, column, value)] # za sekoj row od rows za koj split_function vrakja false
  72. return (set_true, set_false)
  73.  
  74.  
  75. def uniquecounts(rows):
  76. d = {}
  77. for r in rows:
  78. # print(r[-1])
  79. d.setdefault(r[-1], 0)
  80. d[r[-1]] += 1
  81. return d
  82.  
  83.  
  84. from math import log
  85.  
  86. log2 = lambda x: log(x) / log(2)
  87.  
  88.  
  89. def entropy(rows):
  90. results = uniquecounts(rows)
  91. # Now calculate the entropy
  92. ent = 0.0
  93. n = len(rows)
  94. for label, cnt in results.items():
  95. # print(r)
  96. p = float(cnt) / n
  97. # print(label, cnt, p)
  98. ent -= p * log2(p)
  99. return ent
  100.  
  101.  
  102. def info_gain(current_score, sets, scoref=entropy):
  103. m = sum([len(s) for s in sets])
  104. gain = current_score
  105. for s in sets:
  106. n = len(s)
  107. p = 1. * n / m
  108. gain -= p * scoref(s)
  109. return gain
  110.  
  111.  
  112. def buildtree(rows, scoref=entropy):
  113. if len(rows) == 0:
  114. 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_column = -1
  120. best_value = None
  121. best_subsetf = None
  122. best_subsett = None
  123.  
  124. column_count = len(rows[0]) - 1
  125. for col in range(column_count):
  126. # Generate the list of different values in
  127. # this column
  128. # column_values = set()
  129. # for row in rows:
  130. # column_values.add(row[col])
  131. # print(column_values)
  132. column_values = set([row[col] for row in rows])
  133. # print('Zemame vo predvid podelba po:', col, len(column_values), column_values)
  134. # continue
  135. # Now try dividing the rows up for each value
  136. # in this column
  137. for value in column_values:
  138. sets = divideset(rows, col, value)
  139.  
  140. # Information gain
  141. # p = float(len(set1)) / len(rows)
  142. # gain = current_score - p * scoref(set1) - (1 - p) * scoref(set2)
  143. gain = info_gain(current_score, sets, scoref)
  144. if gain > best_gain and len(sets) > 0 and len(sets[0]) > 0 and len(sets[1]) > 0:
  145. best_gain = gain
  146. best_column = col
  147. best_value = value
  148. best_subsett = sets[0]
  149. best_subsetf = sets[1]
  150. # best_criteria = (col, value)
  151. # best_sets = (set1, set2)
  152. # print('Dividing dataset', col, value, gain, sets)
  153. # pronajden e korenot
  154. # return
  155. # Create the subbranches
  156. if best_gain > 0:
  157. # print(best_subsett)
  158. # print(best_subsetf)
  159. #print(best_column, best_value, best_gain)
  160. #print('Starting true subbranch')
  161. trueBranch = buildtree(best_subsett, scoref)
  162. #print()
  163. #print('Starting false subbranch')
  164. falseBranch = buildtree(best_subsetf, scoref)
  165. #print()
  166. return decisionnode(col=best_column, value=best_value,
  167. tb=trueBranch, fb=falseBranch)
  168.  
  169. else:
  170. #print('Terminalen jazol')
  171. #print()
  172. return decisionnode(results=uniquecounts(rows))
  173.  
  174.  
  175. def printtree(tree, indent=''):
  176. # Is this a leaf node?
  177. if tree.results != None:
  178. print(indent + str(sorted(tree.results.items())))
  179. else:
  180. # Print the criteria
  181. print(indent + str(tree.col) + ':' + str(tree.value) + '? ')
  182. # Print the branches
  183. print(indent + 'T->')
  184. printtree(tree.tb, indent + ' ')
  185. print(indent + 'F->')
  186. printtree(tree.fb, indent + ' ')
  187.  
  188.  
  189. def classify(observation, tree):
  190. if tree.results != None:
  191. recnik = tree.results;
  192. #print(recnik)
  193. lista =[]
  194. for k in recnik.keys():
  195. torka = (k,recnik[k])
  196. lista.append(torka)
  197. #print(torka)
  198.  
  199. brKlasi = len(lista)
  200. #print(brKlasi)
  201. if brKlasi == 1:
  202. return lista[0][0]
  203. else:
  204. lista.sort()
  205. return lista[0][0]
  206. # return tree.results
  207. else:
  208. vrednost = observation[tree.col]
  209. if compare_values(vrednost, tree.value):
  210. branch = tree.tb
  211. else:
  212. branch = tree.fb
  213. return classify(observation, branch)
  214.  
  215.  
  216. if __name__ == "__main__":
  217. # referrer='slashdot'
  218. # location='UK'
  219. # readFAQ='no'
  220. # pagesVisited=21
  221. # serviceChosen='Unknown'
  222.  
  223. referrer=input()
  224. location=input()
  225. readFAQ=input()
  226. pagesVisited=input()
  227. serviceChosen=input()
  228.  
  229. testCase=[referrer, location, readFAQ, pagesVisited, serviceChosen]
  230.  
  231. t=buildtree(trainingData)
  232. print (classify(testCase,t))
Advertisement
Add Comment
Please, Sign In to add comment