kristina7

СНЗ - Лаб 2 - Задача 1

Jan 22nd, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.25 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. trainingData=[['slashdot','USA','yes',18,'None'],
  4.         ['google','France','yes',23,'Premium'],
  5.         ['google','France','yes',23,'Basic'],
  6.         ['google','France','yes',23,'Basic'],
  7.         ['digg','USA','yes',24,'Basic'],
  8.         ['kiwitobes','France','yes',23,'Basic'],
  9.         ['google','UK','no',21,'Premium'],
  10.         ['(direct)','New Zealand','no',12,'None'],
  11.         ['(direct)','UK','no',21,'Basic'],
  12.         ['google','USA','no',24,'Premium'],
  13.         ['slashdot','France','yes',19,'None'],
  14.         ['digg','USA','no',18,'None'],
  15.         ['google','UK','no',18,'None'],
  16.         ['kiwitobes','UK','no',19,'None'],
  17.         ['digg','New Zealand','yes',12,'Basic'],
  18.         ['slashdot','UK','no',21,'None'],
  19.         ['google','UK','yes',18,'Basic'],
  20.         ['kiwitobes','France','yes',19,'Basic']]
  21.  
  22. class decisionnode:
  23.     def __init__(self, col=-1, value=None, results=None, tb=None, fb=None):
  24.         self.col = col
  25.         self.value = value
  26.         self.results = results
  27.         self.tb = tb
  28.         self.fb = fb
  29.  
  30.  
  31. def sporedi_broj(row, column, value):
  32.     return row[column] >= value
  33.  
  34.  
  35. def sporedi_string(row, column, value):
  36.     return row[column] == value
  37.  
  38.  
  39. # Divides a set on a specific column. Can handle numeric
  40. # or nominal values
  41. def divideset(rows, column, value):
  42.     # Make a function that tells us if a row is in
  43.     # the first group (true) or the second group (false)
  44.     split_function = None
  45.     if isinstance(value, int) or isinstance(value, float):  # ako vrednosta so koja sporeduvame e od tip int ili float
  46.         # split_function=lambda row:row[column]>=value # togas vrati funkcija cij argument e row i vrakja vrednost true ili false
  47.         split_function = sporedi_broj
  48.     else:
  49.         # split_function=lambda row:row[column]==value # ako vrednosta so koja sporeduvame e od drug tip (string)
  50.         split_function = sporedi_string
  51.  
  52.     # Divide the rows into two sets and return them
  53.     set_false = []
  54.     set_true = []
  55.     for row in rows:
  56.         if split_function(row, column, value):
  57.             set_true.append(row)
  58.         else:
  59.             set_false.append(row)
  60.     set1 = [row for row in rows if
  61.             split_function(row, column, value)]  # za sekoj row od rows za koj split_function vrakja true
  62.     set2 = [row for row in rows if
  63.             not split_function(row, column, value)]  # za sekoj row od rows za koj split_function vrakja false
  64.     # return (set1, set2)
  65.     return (set_true, set_false)
  66.  
  67.  
  68.  
  69. # Create counts of possible results (the last column of
  70. # each row is the result)
  71. def uniquecounts(rows):
  72.     results = {}
  73.     for row in rows:
  74.         # The result is the last column
  75.         r = row[-1]
  76.         results.setdefault(r, 0)
  77.         results[r] += 1
  78.  
  79.     return results
  80.  
  81. # Probability that a randomly placed item will
  82. # be in the wrong category
  83.  
  84. def log2(x):
  85.     from math import log
  86.     l2 = log(x) / log(2)
  87.     return l2
  88.  
  89.  
  90. # Entropy is the sum of p(x)log(p(x)) across all
  91. # the different possible results
  92. def entropy(rows):
  93.     results = uniquecounts(rows)
  94.     # Now calculate the entropy
  95.     ent = 0.0
  96.     for r in results.keys():
  97.         p = float(results[r]) / len(rows)
  98.         ent = ent - p * log2(p)
  99.     return ent
  100.  
  101.  
  102. def buildtree(rows, scoref=entropy):
  103.     if len(rows) == 0: return decisionnode()
  104.     current_score = scoref(rows)
  105.  
  106.     # Set up some variables to track the best criteria
  107.     best_gain = 0.0
  108.     best_column = -1
  109.     best_value = None
  110.     best_subsetf = None
  111.     best_subsett = None
  112.  
  113.     column_count = len(rows[0]) - 1
  114.     for col in range(column_count):
  115.         # Generate the list of different values in
  116.         # this column
  117.         column_values = set()
  118.         for row in rows:
  119.             column_values.add(row[col])
  120.         # Now try dividing the rows up for each value
  121.         # in this column
  122.         for value in column_values:
  123.             (set1, set2) = divideset(rows, col, value)
  124.  
  125.             # Information gain
  126.             p = float(len(set1)) / len(rows)
  127.             gain = current_score - p * scoref(set1) - (1 - p) * scoref(set2)
  128.             if gain > best_gain and len(set1) > 0 and len(set2) > 0:
  129.                 best_gain = gain
  130.                 best_column = col
  131.                 best_value = value
  132.                 best_subsett = set1
  133.                 best_subsetf = set2
  134.                 # best_criteria = (col, value)
  135.                 # best_sets = (set1, set2)
  136.  
  137.     # Create the subbranches
  138.     if best_gain > 0:
  139.         trueBranch = buildtree(best_subsett, scoref)
  140.         falseBranch = buildtree(best_subsetf, scoref)
  141.         return decisionnode(col=best_column, value=best_value,
  142.                             tb=trueBranch, fb=falseBranch)
  143.     else:
  144.         return decisionnode(results=uniquecounts(rows))
  145.  
  146. def printtree(tree, indent='', lvl=0):
  147.     # Is this a leaf node?
  148.     if tree.results != None:
  149.         print(indent + str(sorted(tree.results.items())))
  150.     else:
  151.         # Print the criteria
  152.         print(indent + str(tree.col) + ':' + str(tree.value) + '? Level='+ str(lvl))
  153.         # Print the branches
  154.         print(indent + 'T->'),
  155.         printtree(tree.tb, indent + '  ' , lvl+1)
  156.         print(indent + 'F->'),
  157.         printtree(tree.fb, indent + '  ', lvl+1)
  158.  
  159. def classify(observation, tree):
  160.     if tree.results != None:
  161.         return tree.results
  162.     else:
  163.         vrednost = observation[tree.col]
  164.         branch = None
  165.  
  166.         if isinstance(vrednost, int) or isinstance(vrednost, float):
  167.             if vrednost >= tree.value:
  168.                 branch = tree.tb
  169.             else:
  170.                 branch = tree.fb
  171.         else:
  172.             if vrednost == tree.value:
  173.                 branch = tree.tb
  174.             else:
  175.                 branch = tree.fb
  176.  
  177.         return classify(observation, branch)
  178.  
  179. if __name__ == "__main__":
  180.     #referrer='(direct)'
  181.     #location='France'
  182.     #readFAQ='no'
  183.     #pagesVisited=20
  184.     #serviceChosen='Basic'
  185.  
  186.     referrer = input()
  187.     location = input()
  188.     readFAQ = input()
  189.     pagesVisited = input()
  190.     serviceChosen = input()
  191.  
  192.     testCase = [referrer, location, readFAQ, pagesVisited, serviceChosen]
  193.     trainingData.append(testCase)
  194.     t = buildtree(trainingData)
  195.     printtree(t,"",0)
Advertisement
Add Comment
Please, Sign In to add comment