deko96

Lab 2 Zadaca 2

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