def test_basic_rule_tree(): rules = [ ("abc", "d"), ("abc", "f"), ("ac", "g"), ("bc", "d"), ("de", "g"), ] rules = map(lambda r: (ItemSet(r[0]), ItemSet(r[1])), rules) tree = RuleTree(4) for (antecedent, consequent) in rules: tree.insert(antecedent, consequent) # (ItemSet, [(antecedent, consquent)...]) test_cases = [ ("abcd", [1, 0, 0, 1, 0]), ("geabcd", [1, 0, 0.5, 1, 0.5]), ("abc", [2 / 3, 0.0, 1 / 3, 2 / 3, 1 / 3]), ("bcd", [0.5, 0.0, 0.25, 0.75, 0.25]), ("def", [0.25, 0.0, 0.25, 0.5, 0.25]), ("geab", [0.0, 0.0, 0.0, 0.25, 0.0]), ] test_cases = list(map(lambda t: (ItemSet(t[0]), t[1]), test_cases)) print() for (itemset, expected_results) in test_cases: print("Adding {}".format(itemset)) tree.record_matches(itemset) for (a, c) in tree.rules(): print(" {} -> {} ; {}".format(a, c, tree.match_count_of(a, c))) assert (expected_results == tree.match_vector())
def train(self, window, rules): self.training_rule_tree = RuleTree() for (antecedent, consequent, _, _, _) in rules: self.training_rule_tree.insert(antecedent, consequent) # Populate the training rule tree with the rule frequencies from # the training window. for transaction in window: self.training_rule_tree.record_matches(transaction) self.previous_rule_tree = self.make_test_tree() self.current_rule_tree = self.make_test_tree() # Record the match vector; the vector of rules' supports in the # training window. self.training_mean, self.training_len = self.training_rule_tree.rule_miss_rate( ) self.num_test_transactions = 0
def train(self, window, rules): assert (len(rules) > 0) assert (len(window) > 0) self.training_rule_tree = RuleTree(len(window)) for (antecedent, consequent, _, _, _) in rules: self.training_rule_tree.insert(antecedent, consequent) # Populate the training rule tree with the rule frequencies from # the training window. for transaction in window: self.training_rule_tree.record_matches(transaction) # Populate the test rule tree with a deep copy of the training set. self.test_rule_tree = deepcopy(self.training_rule_tree) # Record the match vector; the vector of rules' supports in the # training window. self.training_match_vec = self.training_rule_tree.match_vector() self.num_test_transactions = 0 self.rule_vec_mean = RollingMean() self.rag_bag_mean = RollingMean()