def maxCoverRule_wrong(cars_list, data_case): for i in range(len(cars_list)): if cars_list[i].class_label != data_case[-1]: temp_data_case = data_case[:-1] temp_data_case.append(cars_list[i].class_label) if cba_cb_m1.is_satisfy(temp_data_case, cars_list[i]): return i return None
def allCoverRules(u, data_case, c_rule, cars_list): w_set = set() for rule_index in u: # have higher precedences than cRule if compare(cars_list[rule_index], c_rule) > 0: # wrongly classify the data case if cba_cb_m1.is_satisfy(data_case, cars_list[rule_index]) == False: w_set.add(rule_index) return w_set
def maxCoverRule_correct(cars_list, data_case): for i in range(len(cars_list)): if cars_list[i].class_label == data_case[-1]: if cba_cb_m1.is_satisfy(data_case, cars_list[i]): return i return None
def classifier_builder_m2(cars, dataset): classifier = Classifier_m2() cars_list = cba_cb_m1.sort(cars) for i in range(len(cars_list)): cars_list[i] = ruleitem2rule(cars_list[i], dataset) # stage 1 q = set() u = set() a = set() mark_set = set() for i in range(len(dataset)): c_rule_index = maxCoverRule_correct(cars_list, dataset[i]) w_rule_index = maxCoverRule_wrong(cars_list, dataset[i]) if c_rule_index is not None: u.add(c_rule_index) if c_rule_index: cars_list[c_rule_index].classCasesCovered[dataset[i][-1]] += 1 if c_rule_index and w_rule_index: if compare(cars_list[c_rule_index], cars_list[w_rule_index]) > 0: q.add(c_rule_index) mark_set.add(c_rule_index) else: a.add((i, dataset[i][-1], c_rule_index, w_rule_index)) elif c_rule_index is None and w_rule_index is not None: a.add((i, dataset[i][-1], c_rule_index, w_rule_index)) # stage 2 for entry in a: if cars_list[entry[3]] in mark_set: if entry[2] is not None: cars_list[entry[2]].classCasesCovered[entry[1]] -= 1 cars_list[entry[3]].classCasesCovered[entry[1]] += 1 else: if entry[2] is not None: w_set = allCoverRules(u, dataset[entry[0]], cars_list[entry[2]], cars_list) else: w_set = allCoverRules(u, dataset[entry[0]], None, cars_list) for w in w_set: cars_list[w].replace.add((entry[2], entry[0], entry[1])) cars_list[w].classCasesCovered[entry[1]] += 1 q |= w_set # stage 3 rule_errors = 0 q = sort_with_index(q, cars_list) data_cases_covered = list([False] * len(dataset)) for r_index in q: if cars_list[r_index].classCasesCovered[ cars_list[r_index].class_label] != 0: for entry in cars_list[r_index].replace: if data_cases_covered[entry[1]]: cars_list[r_index].classCasesCovered[entry[2]] -= 1 else: if entry[0] is not None: cars_list[entry[0]].classCasesCovered[entry[2]] -= 1 for i in range(len(dataset)): datacase = dataset[i] if datacase: is_satisfy_value = cba_cb_m1.is_satisfy( datacase, cars_list[r_index]) if is_satisfy_value: dataset[i] = [] data_cases_covered[i] = True rule_errors += errorsOfRule(cars_list[r_index], dataset) class_distribution = compClassDistr(dataset) default_class = selectDefault(class_distribution) default_errors = defErr(default_class, class_distribution) total_errors = rule_errors + default_errors classifier.add(cars_list[r_index], default_class, total_errors) classifier.discard() return classifier