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 _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 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=[(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=[(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_right(self): feature_id = FeatureId.length index = self.fe.feature_to_indices[FeatureGroup.right][0][feature_id][0] picked_class = randint(0, self.n_classes - 1) picked_class_name = self.class_representations[picked_class] rule = Rule(attrs=[(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, ))
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 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 test_describe_rule_categorical(self): name, indices, feature = self.categorical activated = randint(0, len(indices) - 1) activated_name = feature.names[activated] picked_class = randint(0, self.n_classes - 1) picked_class_name = self.class_representations[picked_class] index = indices[activated] rule = Rule(attrs=[(index, True, 0.5)], stats=RuleStats(picked_class, 0.9, 150), artificial=False) self.assertEqual(describe_rule(rule, self.fe), " %s = %s\n" "⇒ y = %s\n" "Confidence: 0.900. Support: 150." % ( name, activated_name, picked_class_name, ))
def describe_rule_attrs(rule: Rule, feature_extractor: FeatureExtractor) -> Sequence[str]: """ Format the rule as text. We take features metadata to convert the integer indices to human-readable names. :param rule: The rule to describe. :param feature_extractor: The FeatureExtractor used to create those rules. :return: The description of the rule. """ result = [] for feature, feature_id, splits, node_index, group in rule.group_features( feature_extractor): desc = describe_rule_splits( feature, "%s%s" % (group.format(node_index), feature_id.name), splits) result.append(desc) return result
def __getitem__(self, item): return Rule(tuple(), RuleStats(self.ys[item], 1., 1000), False)