def test_describe_rule_bag(self): name, indices, feature = self.bag activated = randint(0, len(indices) - 1) activated_name = feature.names[activated] not_activated = randint(0, len(indices) - 1) not_activated_name = feature.names[not_activated] picked_class = randint(0, self.n_classes - 1) picked_class_name = self.class_representations[picked_class] index = indices[activated] not_index = indices[not_activated] rule = Rule(attrs=[ RuleAttribute(index, True, 0.5), RuleAttribute(not_index, False, 0.5) ], stats=RuleStats(picked_class, 0.9, 150), artificial=False) self.assertEqual( describe_rule(rule, self.fe), " %s in {%s} and not in {%s}\n" "⇒ y = %s\n" "Confidence: 0.900. Support: 150." % ( name, activated_name, not_activated_name, picked_class_name, ))
def create_rule(): cls = DescriptionsTests feature_id = FeatureId.internal_type index1 = cls.fe.feature_to_indices[FeatureGroup.parents][0][feature_id][0] index2 = cls.fe.feature_to_indices[FeatureGroup.right][0][feature_id][0] index3 = cls.categorical[1][3] rule = Rule(attrs=[ RuleAttribute(index1, True, 4), RuleAttribute(index2, True, 4.5), RuleAttribute(index3, True, 0.5) ], stats=RuleStats(2, 0.9, 150), artificial=False) return rule
def _assemble_rules(rules_tree: dict) -> List[Rule]: rules = [] rule_attrs = ( RuleAttribute(*params) for params in zip(rules_tree["features"], rules_tree["cmps"], rules_tree["thresholds"])) for cls, conf, length in zip(rules_tree["cls"], rules_tree["conf"], rules_tree["lengths"]): rules.append( Rule(tuple(islice(rule_attrs, int(length))), RuleStats(cls, conf))) return rules
def _assemble_rules(rules_tree: dict) -> List[Rule]: rules = [] rule_attrs = ( RuleAttribute(*params) for params in zip(rules_tree["features"], rules_tree["cmps"], rules_tree["thresholds"])) for cls, conf, support, artificial, length in zip( rules_tree["cls"], rules_tree["conf"], rules_tree["support"], rules_tree["artificial"], rules_tree["lengths"]): rules.append( Rule(attrs=tuple(islice(rule_attrs, int(length))), stats=RuleStats(int(cls), float(conf), int(support)), artificial=bool(artificial))) return rules
def test_describe_rule_ordinal(self): name, indices, feature = self.ordinal picked_class = randint(0, self.n_classes - 1) picked_class_name = self.class_representations[picked_class] index = indices[0] rule = Rule(attrs=[RuleAttribute(index, True, 4.5)], stats=RuleStats(picked_class, 0.9, 150), artificial=False) self.assertEqual( describe_rule(rule, self.fe), " %s ≥ %d\n" "⇒ y = %s\n" "Confidence: 0.900. Support: 150." % ( name, ceil(4.5), picked_class_name, ))
def test_describe_rule_parent(self): feature_id = FeatureId.internal_type index = self.fe.feature_to_indices[ FeatureGroup.parents][0][feature_id][0] picked_class = randint(0, self.n_classes - 1) picked_class_name = self.class_representations[picked_class] rule = Rule(attrs=[RuleAttribute(index, True, 4)], stats=RuleStats(picked_class, 0.9, 150), artificial=False) self.assertEqual( describe_rule(rule, self.fe), " ^1.%s = AnyTypeAnnotation\n" "⇒ y = %s\n" "Confidence: 0.900. Support: 150." % ( feature_id.name, picked_class_name, ))
def test_describe_rule_left(self): feature_id = FeatureId.diff_line index = self.fe.feature_to_indices[FeatureGroup.left][0][feature_id][0] picked_class = randint(0, self.n_classes - 1) picked_class_name = self.class_representations[picked_class] rule = Rule(attrs=[RuleAttribute(index, True, 4.5)], stats=RuleStats(picked_class, 0.9, 150), artificial=False) self.assertEqual( describe_rule(rule, self.fe), " -1.%s ≥ %d\n" "⇒ y = %s\n" "Confidence: 0.900. Support: 150." % ( feature_id.name, ceil(4.5), picked_class_name, ))