Advertisement
kristina7

Дрва на одлучување - Колоквиум 2017

Jan 21st, 2018
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.18 KB | None | 0 0
  1. data = [[242.0, 23.2, 25.4, 30.0, 38.4, 13.4, 1],
  2.         [290.0, 24.0, 26.3, 31.2, 40.0, 13.8, 1],
  3.         [340.0, 23.9, 26.5, 31.1, 39.8, 15.1, 1],
  4.         [363.0, 26.3, 29.0, 33.5, 38.0, 13.3, 1],
  5.         [430.0, 26.5, 29.0, 34.0, 36.6, 15.1, 1],
  6.         [450.0, 26.8, 29.7, 34.7, 39.2, 14.2, 1],
  7.         [500.0, 26.8, 29.7, 34.5, 41.1, 15.3, 1],
  8.         [390.0, 27.6, 30.0, 35.0, 36.2, 13.4, 1],
  9.         [450.0, 27.6, 30.0, 35.1, 39.9, 13.8, 1],
  10.         [500.0, 28.5, 30.7, 36.2, 39.3, 13.7, 1],
  11.         [475.0, 28.4, 31.0, 36.2, 39.4, 14.1, 1],
  12.         [500.0, 28.7, 31.0, 36.2, 39.7, 13.3, 1],
  13.         [500.0, 29.1, 31.5, 36.4, 37.8, 12.0, 1],
  14.         [500.0, 29.5, 32.0, 37.3, 37.3, 13.6, 1],
  15.         [600.0, 29.4, 32.0, 37.2, 40.2, 13.9, 1],
  16.         [600.0, 29.4, 32.0, 37.2, 41.5, 15.0, 1],
  17.         [700.0, 30.4, 33.0, 38.3, 38.8, 13.8, 1],
  18.         [700.0, 30.4, 33.0, 38.5, 38.8, 13.5, 1],
  19.         [610.0, 30.9, 33.5, 38.6, 40.5, 13.3, 1],
  20.         [650.0, 31.0, 33.5, 38.7, 37.4, 14.8, 1],
  21.         [575.0, 31.3, 34.0, 39.5, 38.3, 14.1, 1],
  22.         [685.0, 31.4, 34.0, 39.2, 40.8, 13.7, 1],
  23.         [620.0, 31.5, 34.5, 39.7, 39.1, 13.3, 1],
  24.         [680.0, 31.8, 35.0, 40.6, 38.1, 15.1, 1],
  25.         [700.0, 31.9, 35.0, 40.5, 40.1, 13.8, 1],
  26.         [725.0, 31.8, 35.0, 40.9, 40.0, 14.8, 1],
  27.         [720.0, 32.0, 35.0, 40.6, 40.3, 15.0, 1],
  28.         [714.0, 32.7, 36.0, 41.5, 39.8, 14.1, 1],
  29.         [850.0, 32.8, 36.0, 41.6, 40.6, 14.9, 1],
  30.         [1000.0, 33.5, 37.0, 42.6, 44.5, 15.5, 1],
  31.         [920.0, 35.0, 38.5, 44.1, 40.9, 14.3, 1],
  32.         [955.0, 35.0, 38.5, 44.0, 41.1, 14.3, 1],
  33.         [925.0, 36.2, 39.5, 45.3, 41.4, 14.9, 1],
  34.         [975.0, 37.4, 41.0, 45.9, 40.6, 14.7, 1],
  35.         [950.0, 38.0, 41.0, 46.5, 37.9, 13.7, 1],
  36.         [270.0, 23.6, 26.0, 28.7, 29.2, 14.8, 2],
  37.         [270.0, 24.1, 26.5, 29.3, 27.8, 14.5, 2],
  38.         [306.0, 25.6, 28.0, 30.8, 28.5, 15.2, 2],
  39.         [540.0, 28.5, 31.0, 34.0, 31.6, 19.3, 2],
  40.         [800.0, 33.7, 36.4, 39.6, 29.7, 16.6, 2],
  41.         [1000.0, 37.3, 40.0, 43.5, 28.4, 15.0, 2],
  42.         [40.0, 12.9, 14.1, 16.2, 25.6, 14.0, 3],
  43.         [69.0, 16.5, 18.2, 20.3, 26.1, 13.9, 3],
  44.         [78.0, 17.5, 18.8, 21.2, 26.3, 13.7, 3],
  45.         [87.0, 18.2, 19.8, 22.2, 25.3, 14.3, 3],
  46.         [120.0, 18.6, 20.0, 22.2, 28.0, 16.1, 3],
  47.         [0.0, 19.0, 20.5, 22.8, 28.4, 14.7, 3],
  48.         [110.0, 19.1, 20.8, 23.1, 26.7, 14.7, 3],
  49.         [120.0, 19.4, 21.0, 23.7, 25.8, 13.9, 3],
  50.         [150.0, 20.4, 22.0, 24.7, 23.5, 15.2, 3],
  51.         [145.0, 20.5, 22.0, 24.3, 27.3, 14.6, 3],
  52.         [160.0, 20.5, 22.5, 25.3, 27.8, 15.1, 3],
  53.         [140.0, 21.0, 22.5, 25.0, 26.2, 13.3, 3],
  54.         [160.0, 21.1, 22.5, 25.0, 25.6, 15.2, 3],
  55.         [169.0, 22.0, 24.0, 27.2, 27.7, 14.1, 3],
  56.         [161.0, 22.0, 23.4, 26.7, 25.9, 13.6, 3],
  57.         [200.0, 22.1, 23.5, 26.8, 27.6, 15.4, 3],
  58.         [180.0, 23.6, 25.2, 27.9, 25.4, 14.0, 3],
  59.         [290.0, 24.0, 26.0, 29.2, 30.4, 15.4, 3],
  60.         [272.0, 25.0, 27.0, 30.6, 28.0, 15.6, 3],
  61.         [390.0, 29.5, 31.7, 35.0, 27.1, 15.3, 3],
  62.         [55.0, 13.5, 14.7, 16.5, 41.5, 14.1, 4],
  63.         [60.0, 14.3, 15.5, 17.4, 37.8, 13.3, 4],
  64.         [90.0, 16.3, 17.7, 19.8, 37.4, 13.5, 4],
  65.         [120.0, 17.5, 19.0, 21.3, 39.4, 13.7, 4],
  66.         [150.0, 18.4, 20.0, 22.4, 39.7, 14.7, 4],
  67.         [140.0, 19.0, 20.7, 23.2, 36.8, 14.2, 4],
  68.         [170.0, 19.0, 20.7, 23.2, 40.5, 14.7, 4],
  69.         [145.0, 19.8, 21.5, 24.1, 40.4, 13.1, 4],
  70.         [200.0, 21.2, 23.0, 25.8, 40.1, 14.2, 4],
  71.         [273.0, 23.0, 25.0, 28.0, 39.6, 14.8, 4],
  72.         [300.0, 24.0, 26.0, 29.0, 39.2, 14.6, 4],
  73.         [6.7, 9.3, 9.8, 10.8, 16.1, 9.7, 5],
  74.         [7.5, 10.0, 10.5, 11.6, 17.0, 10.0, 5],
  75.         [7.0, 10.1, 10.6, 11.6, 14.9, 9.9, 5],
  76.         [9.7, 10.4, 11.0, 12.0, 18.3, 11.5, 5],
  77.         [9.8, 10.7, 11.2, 12.4, 16.8, 10.3, 5],
  78.         [8.7, 10.8, 11.3, 12.6, 15.7, 10.2, 5],
  79.         [10.0, 11.3, 11.8, 13.1, 16.9, 9.8, 5],
  80.         [9.9, 11.3, 11.8, 13.1, 16.9, 8.9, 5],
  81.         [9.8, 11.4, 12.0, 13.2, 16.7, 8.7, 5],
  82.         [12.2, 11.5, 12.2, 13.4, 15.6, 10.4, 5],
  83.         [13.4, 11.7, 12.4, 13.5, 18.0, 9.4, 5],
  84.         [12.2, 12.1, 13.0, 13.8, 16.5, 9.1, 5],
  85.         [19.7, 13.2, 14.3, 15.2, 18.9, 13.6, 5],
  86.         [19.9, 13.8, 15.0, 16.2, 18.1, 11.6, 5],
  87.         [200.0, 30.0, 32.3, 34.8, 16.0, 9.7, 6],
  88.         [300.0, 31.7, 34.0, 37.8, 15.1, 11.0, 6],
  89.         [300.0, 32.7, 35.0, 38.8, 15.3, 11.3, 6],
  90.         [300.0, 34.8, 37.3, 39.8, 15.8, 10.1, 6],
  91.         [430.0, 35.5, 38.0, 40.5, 18.0, 11.3, 6],
  92.         [345.0, 36.0, 38.5, 41.0, 15.6, 9.7, 6],
  93.         [456.0, 40.0, 42.5, 45.5, 16.0, 9.5, 6],
  94.         [510.0, 40.0, 42.5, 45.5, 15.0, 9.8, 6],
  95.         [540.0, 40.1, 43.0, 45.8, 17.0, 11.2, 6],
  96.         [500.0, 42.0, 45.0, 48.0, 14.5, 10.2, 6],
  97.         [567.0, 43.2, 46.0, 48.7, 16.0, 10.0, 6],
  98.         [770.0, 44.8, 48.0, 51.2, 15.0, 10.5, 6],
  99.         [950.0, 48.3, 51.7, 55.1, 16.2, 11.2, 6],
  100.         [1250.0, 52.0, 56.0, 59.7, 17.9, 11.7, 6],
  101.         [1600.0, 56.0, 60.0, 64.0, 15.0, 9.6, 6],
  102.         [1550.0, 56.0, 60.0, 64.0, 15.0, 9.6, 6],
  103.         [1650.0, 59.0, 63.4, 68.0, 15.9, 11.0, 6],
  104.         [5.9, 7.5, 8.4, 8.8, 24.0, 16.0, 7],
  105.         [32.0, 12.5, 13.7, 14.7, 24.0, 13.6, 7],
  106.         [40.0, 13.8, 15.0, 16.0, 23.9, 15.2, 7],
  107.         [51.5, 15.0, 16.2, 17.2, 26.7, 15.3, 7],
  108.         [70.0, 15.7, 17.4, 18.5, 24.8, 15.9, 7],
  109.         [100.0, 16.2, 18.0, 19.2, 27.2, 17.3, 7],
  110.         [78.0, 16.8, 18.7, 19.4, 26.8, 16.1, 7],
  111.         [80.0, 17.2, 19.0, 20.2, 27.9, 15.1, 7],
  112.         [85.0, 17.8, 19.6, 20.8, 24.7, 14.6, 7],
  113.         [85.0, 18.2, 20.0, 21.0, 24.2, 13.2, 7],
  114.         [110.0, 19.0, 21.0, 22.5, 25.3, 15.8, 7],
  115.         [115.0, 19.0, 21.0, 22.5, 26.3, 14.7, 7],
  116.         [125.0, 19.0, 21.0, 22.5, 25.3, 16.3, 7],
  117.         [130.0, 19.3, 21.3, 22.8, 28.0, 15.5, 7],
  118.         [120.0, 20.0, 22.0, 23.5, 26.0, 14.5, 7],
  119.         [120.0, 20.0, 22.0, 23.5, 24.0, 15.0, 7],
  120.         [130.0, 20.0, 22.0, 23.5, 26.0, 15.0, 7],
  121.         [135.0, 20.0, 22.0, 23.5, 25.0, 15.0, 7],
  122.         [110.0, 20.0, 22.0, 23.5, 23.5, 17.0, 7],
  123.         [130.0, 20.5, 22.5, 24.0, 24.4, 15.1, 7],
  124.         [150.0, 20.5, 22.5, 24.0, 28.3, 15.1, 7],
  125.         [145.0, 20.7, 22.7, 24.2, 24.6, 15.0, 7],
  126.         [150.0, 21.0, 23.0, 24.5, 21.3, 14.8, 7],
  127.         [170.0, 21.5, 23.5, 25.0, 25.1, 14.9, 7],
  128.         [225.0, 22.0, 24.0, 25.5, 28.6, 14.6, 7],
  129.         [145.0, 22.0, 24.0, 25.5, 25.0, 15.0, 7],
  130.         [188.0, 22.6, 24.6, 26.2, 25.7, 15.9, 7],
  131.         [180.0, 23.0, 25.0, 26.5, 24.3, 13.9, 7],
  132.         [197.0, 23.5, 25.6, 27.0, 24.3, 15.7, 7],
  133.         [218.0, 25.0, 26.5, 28.0, 25.6, 14.8, 7],
  134.         [300.0, 25.2, 27.3, 28.7, 29.0, 17.9, 7],
  135.         [260.0, 25.4, 27.5, 28.9, 24.8, 15.0, 7],
  136.         [265.0, 25.4, 27.5, 28.9, 24.4, 15.0, 7],
  137.         [250.0, 25.4, 27.5, 28.9, 25.2, 15.8, 7],
  138.         [250.0, 25.9, 28.0, 29.4, 26.6, 14.3, 7],
  139.         [300.0, 26.9, 28.7, 30.1, 25.2, 15.4, 7],
  140.         [320.0, 27.8, 30.0, 31.6, 24.1, 15.1, 7],
  141.         [514.0, 30.5, 32.8, 34.0, 29.5, 17.7, 7],
  142.         [556.0, 32.0, 34.5, 36.5, 28.1, 17.5, 7],
  143.         [840.0, 32.5, 35.0, 37.3, 30.8, 20.9, 7],
  144.         [685.0, 34.0, 36.5, 39.0, 27.9, 17.6, 7],
  145.         [700.0, 34.0, 36.0, 38.3, 27.7, 17.6, 7],
  146.         [700.0, 34.5, 37.0, 39.4, 27.5, 15.9, 7],
  147.         [690.0, 34.6, 37.0, 39.3, 26.9, 16.2, 7],
  148.         [900.0, 36.5, 39.0, 41.4, 26.9, 18.1, 7],
  149.         [650.0, 36.5, 39.0, 41.4, 26.9, 14.5, 7],
  150.         [820.0, 36.6, 39.0, 41.3, 30.1, 17.8, 7],
  151.         [850.0, 36.9, 40.0, 42.3, 28.2, 16.8, 7],
  152.         [900.0, 37.0, 40.0, 42.5, 27.6, 17.0, 7],
  153.         [1015.0, 37.0, 40.0, 42.4, 29.2, 17.6, 7],
  154.         [820.0, 37.1, 40.0, 42.5, 26.2, 15.6, 7],
  155.         [1100.0, 39.0, 42.0, 44.6, 28.7, 15.4, 7],
  156.         [1000.0, 39.8, 43.0, 45.2, 26.4, 16.1, 7],
  157.         [1100.0, 40.1, 43.0, 45.5, 27.5, 16.3, 7],
  158.         [1000.0, 40.2, 43.5, 46.0, 27.4, 17.7, 7],
  159.         [1000.0, 41.1, 44.0, 46.6, 26.8, 16.3, 7]]
  160.  
  161. class decisionnode:
  162.     def __init__(self, col=-1, value=None, results=None, tb=None, fb=None):
  163.         self.col = col
  164.         self.value = value
  165.         self.results = results
  166.         self.tb = tb
  167.         self.fb = fb
  168.  
  169.  
  170. def sporedi_broj(row, column, value):
  171.     return row[column] >= value
  172.  
  173.  
  174. def sporedi_string(row, column, value):
  175.     return row[column] == value
  176.  
  177.  
  178. # Divides a set on a specific column. Can handle numeric
  179. # or nominal values
  180. def divideset(rows, column, value):
  181.     # Make a function that tells us if a row is in
  182.     # the first group (true) or the second group (false)
  183.     split_function = None
  184.     if isinstance(value, int) or isinstance(value, float):  # ako vrednosta so koja sporeduvame e od tip int ili float
  185.         # split_function=lambda row:row[column]>=value # togas vrati funkcija cij argument e row i vrakja vrednost true ili false
  186.         split_function = sporedi_broj
  187.     else:
  188.         # split_function=lambda row:row[column]==value # ako vrednosta so koja sporeduvame e od drug tip (string)
  189.         split_function = sporedi_string
  190.  
  191.     # Divide the rows into two sets and return them
  192.     set_false = []
  193.     set_true = []
  194.     for row in rows:
  195.         if split_function(row, column, value):
  196.             set_true.append(row)
  197.         else:
  198.             set_false.append(row)
  199.     set1 = [row for row in rows if
  200.             split_function(row, column, value)]  # za sekoj row od rows za koj split_function vrakja true
  201.     set2 = [row for row in rows if
  202.             not split_function(row, column, value)]  # za sekoj row od rows za koj split_function vrakja false
  203.     # return (set1, set2)
  204.     return (set_true, set_false)
  205.  
  206. # Create counts of possible results (the last column of
  207. # each row is the result)
  208. def uniquecounts(rows):
  209.     results = {}
  210.     for row in rows:
  211.         # The result is the last column
  212.         r = row[-1]
  213.         results.setdefault(r, 0)
  214.         results[r] += 1
  215.  
  216.     return results
  217.  
  218. # Probability that a randomly placed item will
  219. # be in the wrong category
  220.  
  221. def log2(x):
  222.     from math import log
  223.     l2 = log(x) / log(2)
  224.     return l2
  225.  
  226.  
  227. # Entropy is the sum of p(x)log(p(x)) across all
  228. # the different possible results
  229. def entropy(rows):
  230.     results = uniquecounts(rows)
  231.     # Now calculate the entropy
  232.     ent = 0.0
  233.     for r in results.keys():
  234.         p = float(results[r]) / len(rows)
  235.         ent = ent - p * log2(p)
  236.     return ent
  237.  
  238.  
  239. def buildtree(rows, scoref=entropy):
  240.     if len(rows) == 0: return decisionnode()
  241.     current_score = scoref(rows)
  242.  
  243.     # Set up some variables to track the best criteria
  244.     best_gain = 0.0
  245.     best_column = -1
  246.     best_value = None
  247.     best_subsetf = None
  248.     best_subsett = None
  249.  
  250.     column_count = len(rows[0]) - 1
  251.     for col in range(column_count):
  252.         # Generate the list of different values in
  253.         # this column
  254.         column_values = set()
  255.         for row in rows:
  256.             column_values.add(row[col])
  257.         # Now try dividing the rows up for each value
  258.         # in this column
  259.         for value in column_values:
  260.             (set1, set2) = divideset(rows, col, value)
  261.  
  262.             # Information gain
  263.             p = float(len(set1)) / len(rows)
  264.             gain = current_score - p * scoref(set1) - (1 - p) * scoref(set2)
  265.             if gain > best_gain and len(set1) > 0 and len(set2) > 0:
  266.                 best_gain = gain
  267.                 best_column = col
  268.                 best_value = value
  269.                 best_subsett = set1
  270.                 best_subsetf = set2
  271.                 # best_criteria = (col, value)
  272.                 # best_sets = (set1, set2)
  273.  
  274.     # Create the subbranches
  275.     if best_gain > 0:
  276.         trueBranch = buildtree(best_subsett, scoref)
  277.         falseBranch = buildtree(best_subsetf, scoref)
  278.         return decisionnode(col=best_column, value=best_value,
  279.                             tb=trueBranch, fb=falseBranch)
  280.     else:
  281.         return decisionnode(results=uniquecounts(rows))
  282.  
  283.  
  284.  
  285.  
  286. def printtree(tree, indent=''):
  287.     # Is this a leaf node?
  288.     if tree.results != None:
  289.         print(indent + str(sorted(tree.results.items())))
  290.     else:
  291.         # Print the criteria
  292.         print(indent + str(tree.col) + ':' + str(tree.value) + '? ')
  293.         # Print the branches
  294.         print(indent + 'T->')
  295.         printtree(tree.tb, indent + '  ')
  296.         print(indent + 'F->')
  297.         printtree(tree.fb, indent + '  ')
  298.  
  299.  
  300.  
  301. def classify(observation, tree):
  302.     if tree.results != None:
  303.         return tree.results
  304.     else:
  305.         vrednost = observation[tree.col]
  306.         branch = None
  307.  
  308.         if isinstance(vrednost, int) or isinstance(vrednost, float):
  309.             if vrednost >= tree.value:
  310.                 branch = tree.tb
  311.             else:
  312.                 branch = tree.fb
  313.         else:
  314.             if vrednost == tree.value:
  315.                 branch = tree.tb
  316.             else:
  317.                 branch = tree.fb
  318.  
  319.         return classify(observation, branch)
  320.  
  321.  
  322. if __name__ == "__main__":
  323.     index = input()
  324.     trainingData = []
  325.     br = 0
  326.     klasa=1
  327.     for d in data:
  328.         if br < 5 and d[6] == klasa:
  329.             trainingData.append(d)
  330.             br = br + 1
  331.         if br == 5:
  332.             klasa = klasa + 1
  333.             br = 0
  334.     t = buildtree(trainingData)
  335.     klasa = classify(data[int(index)], t)
  336.     print(klasa)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement