Guest User

Untitled

a guest
Jul 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. import numpy as np
  2. from random import randint
  3.  
  4. class _Node:
  5. def __init__(self, att=0, list_value=None, list_Obidset=None, count=None, pos=0, num_class=0):
  6. if list_value is None:
  7. list_value = []
  8. if list_Obidset is None:
  9. list_Obidset = []
  10. if count is None:
  11. count = [0] * num_class
  12. self.att = att
  13. self.list_value = list_value
  14. self.list_Obidset = list_Obidset
  15. self.count = count
  16. self.pos = pos
  17.  
  18. class _NodeRule:
  19. def __init__(self, att, list_value, label_pos, rule_sup, conf):
  20. self.att = att
  21. self.list_value = list_value
  22. self.label_pos = label_pos
  23. self.rule_sup = rule_sup
  24. self.conf = conf
  25.  
  26.  
  27. class CarMiner:
  28. 'Car-Miner estimator'
  29. def __init__(self, minSup, minConf, verbose=False):
  30. self.minSup = minSup
  31. self.minConf = minConf
  32. self.verbose = verbose
  33.  
  34. # Ham lay GIAO 2 list
  35. def __Intersect(self, x, y):
  36. z = []
  37. for item in x:
  38. if item in y:
  39. z.append(item)
  40. return z
  41.  
  42. # Ham lay HOP 2 list
  43. def __Union(self, x, y):
  44. return list(set(x + y))
  45.  
  46. def __BuildOneItemset(self, list_Lr, X, y):
  47. # Xay dung danh sach cac nut 1-itemset pho bien
  48. for i in range(self.num_attr):
  49. temp = X[:,i]
  50. # Duyet lan 1 de dinh dang lai du lieu doc: itemset <=> Cac Obidset
  51. list_ValueTemp = np.unique(temp)
  52. list_ObidsetTemp = []
  53. list_count = []
  54.  
  55. # Lay ra cac Obidset, count ma item don xuat hien, item j se ung voi list obidset thu j
  56. for value1 in list_ValueTemp:
  57. Obidset_temp = []
  58. for k in range(len(temp)):
  59. if temp[k] == value1:
  60. Obidset_temp.append(k)
  61. list_ObidsetTemp.append(Obidset_temp)
  62.  
  63. #Dem so dong tuong ung voi so lop
  64. count_temp = [0] * self.num_class
  65. for k in Obidset_temp:
  66. count_temp[y[k]] += 1
  67. list_count.append(count_temp)
  68.  
  69. # Add node to list_Lr
  70. for j in range(len(list_ValueTemp)):
  71. # Check if >= self.minSup
  72. if np.max(list_count[j]) < self.minSup * self.num_sample:
  73. continue
  74. node = _Node(
  75. 2**i,
  76. [list_ValueTemp[j]],
  77. list_ObidsetTemp[j],
  78. list_count[j],
  79. np.argmax(list_count[j]),
  80. self.num_class
  81. )
  82. # Add node to list_Lr
  83. list_Lr.append(node)
  84.  
  85. def __CarMiner(self, list_Lr, y):
  86. for i in range(len(list_Lr)):
  87. conf = list_Lr[i].count[list_Lr[i].pos] / len(list_Lr[i].list_Obidset)
  88. if conf >= self.minConf:
  89. node_Rule = _NodeRule(
  90. list_Lr[i].att,
  91. list_Lr[i].list_value,
  92. list_Lr[i].pos,
  93. list_Lr[i].count[list_Lr[i].pos],
  94. conf
  95. )
  96. self.CARs.append(node_Rule)
  97.  
  98. Pi = []
  99. for j in range(i+1, len(list_Lr)):
  100. if list_Lr[i].att != list_Lr[j].att: # Menh de 1
  101. node = _Node(
  102. list_Lr[i].att | list_Lr[j].att,
  103. self.__Union(list_Lr[i].list_value, list_Lr[j].list_value),
  104. self.__Intersect(list_Lr[i].list_Obidset, list_Lr[j].list_Obidset),
  105. num_class=self.num_class
  106. )
  107.  
  108. if len(node.list_Obidset) == len(list_Lr[i].list_Obidset): # Menh de 2
  109. node.count = list_Lr[i].count
  110. node.pos = list_Lr[i].pos
  111.  
  112. elif len(node.list_Obidset) == len(list_Lr[j].list_Obidset): # Menh de 2
  113. node.count = list_Lr[j].count
  114. node.pos = list_Lr[j].pos
  115.  
  116. elif len(node.list_Obidset) == 0:
  117. continue
  118.  
  119. else:
  120. for k in node.list_Obidset:
  121. node.count[y[k]] += 1
  122. node.pos = np.argmax(node.count)
  123.  
  124. if node.count[node.pos] >= self.minSup * self.num_sample:
  125. Pi.append(node)
  126. self.__CarMiner(Pi, y)
  127.  
  128. def fit(self, X, y):
  129. self.num_class = len(np.unique(y))
  130. self.num_attr = X.shape[1]
  131. self.num_sample = X.shape[0]
  132. self.list_Lr = []
  133. self.CARs = []
  134. self.__BuildOneItemset(self.list_Lr, X, y)
  135. self.__CarMiner(self.list_Lr, y)
  136. self.CARs = sorted(self.CARs, key=lambda k: (k.conf, k.rule_sup), reverse=True)
  137. # Print results
  138. if self.verbose:
  139. print("Number of Rules: %d" % len(self.CARs))
  140.  
  141. def predict(self, X, verbose=False):
  142. y_pred = np.asarray([])
  143. randomPredict = 0
  144. for x in X:
  145. flag = False
  146. for rule in self.CARs:
  147. x_value = []
  148. att = rule.att
  149. for i in range(self.num_attr):
  150. if att % 2 == 1:
  151. x_value.append(x[i])
  152. att = att >> 1
  153. # Compare x_value with rule's value
  154. if x_value == rule.list_value:
  155. y_pred = np.append(y_pred, [rule.label_pos], axis=0)
  156. flag = True
  157. break
  158. # If no rule matched, randomly predict label
  159. if not flag:
  160. randomPredict += 1
  161. y_pred = np.append(y_pred, [randint(0, self.num_class - 1)], axis=0)
  162. if verbose:
  163. print("Number of randomly predicted items: %d" % randomPredict)
  164. return y_pred
Add Comment
Please, Sign In to add comment