Beispiel #1
0
def strNumClassRules(rules):
    list_string = []
    for consequent in mlem2.getEstimatedClass(rules):
        rules_consequent = mlem2.getRulesClass(rules, consequent)
        num = len(rules_consequent)
        list_string.append(str(num))
    return (",".join(list_string))
def strNumClassRules(rules):
    list_string = []
    for consequent in mlem2.getEstimatedClass(rules):
        rules_consequent = mlem2.getRulesClass(rules, consequent)
        num = len(rules_consequent)
        list_string.append(str(num))
    return ",".join(list_string)
Beispiel #3
0
def getAccurayRecall(rules, decision_table, list_judgeNominal):
    result = []    
    consequents = mlem2.getEstimatedClass(rules)
    for consequent in consequents :
        target_rules =  mlem2.getRulesClass(rules, consequent)
        target_objects = []
        for rule in target_rules :
            match_objects = decision_table.apply(lambda obj: isExplainRule(obj, rule, list_judgeNominal), axis=1)          
            index_objects = decision_table[match_objects].index.tolist()
            target_objects.extend(index_objects)
        target_objects = list(set(target_objects))
        estimated_classes = decision_table[decision_table.columns[-1]].ix[target_objects]
        accuracy = sum(estimated_classes == consequent)/ len(target_objects)
        recall =  sum(estimated_classes == consequent) / sum(decision_table[decision_table.columns[-1]] == consequent)
        result.append((accuracy, recall))
    return(result)
Beispiel #4
0
def getAccurayRecall(rules, decision_table, list_judgeNominal):
    result = []
    consequents = mlem2.getEstimatedClass(rules)
    for consequent in consequents:
        target_rules = mlem2.getRulesClass(rules, consequent)
        target_objects = []
        for rule in target_rules:
            match_objects = decision_table.apply(
                lambda obj: isExplainRule(obj, rule, list_judgeNominal),
                axis=1)
            index_objects = decision_table[match_objects].index.tolist()
            target_objects.extend(index_objects)
        target_objects = list(set(target_objects))
        estimated_classes = decision_table[
            decision_table.columns[-1]].ix[target_objects]
        accuracy = sum(estimated_classes == consequent) / len(target_objects)
        recall = sum(estimated_classes == consequent) / sum(
            decision_table[decision_table.columns[-1]] == consequent)
        result.append((accuracy, recall))
    return (result)
Beispiel #5
0
def MLEM2_delEAlphaRule_LERS(FILENAME, iter1, iter2, DELFUN, CLASS,
                             ATTRIBUTE_VALUE, alpha):
    print(datetime.now().strftime('%Y/%m/%d %H:%M:%S') + ' ' + FILENAME + ' ' +
          str(iter1) + ' ' + str(iter2) + ' ' + DELFUN.__name__ + ' ' +
          strAttributeValue(ATTRIBUTE_VALUE) + ' ' + str(alpha) + ' ' +
          "START")

    # rule induction and rule save
    fullpath_filename = DIR_UCI + '/' + FILENAME + '/rules/' + 'rules_' + str(
        iter1) + '-' + str(iter2) + '.pkl'
    rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile(
        fullpath_filename) else mlem2.getRulesByMLEM2(FILENAME, iter1, iter2)
    if not os.path.isfile(fullpath_filename):
        mlem2.savePickleRules(rules, fullpath_filename)

    # train data setup
    decision_table_train, decision_class = getData(FILENAME,
                                                   iter1,
                                                   iter2,
                                                   T="train")
    list_judgeNominal = getJudgeNominal(decision_table_train, FILENAME)

    # alpha差別的なルールの基本条件削除 or ルールを削除
    if CLASS != "ALL":
        rules_target = mlem2.getRulesClass(rules, CLASS)
        rules_nontarget = mlem2.getRulesClass(rules, CLASS, judge=False)
        for attr in ATTRIBUTE_VALUE:
            for e in ATTRIBUTE_VALUE[attr]:
                rules_target = DELFUN(rules_target, attr, e,
                                      decision_table_train, list_judgeNominal,
                                      alpha)
        rules_target.extend(rules_nontarget)
        rules = rules_target
    else:
        for attr in ATTRIBUTE_VALUE:
            for e in ATTRIBUTE_VALUE[attr]:
                rules = DELFUN(rules, attr, e, decision_table_train,
                               list_judgeNominal, alpha)

    print(datetime.now().strftime('%Y/%m/%d %H:%M:%S') + ' ' + FILENAME + ' ' +
          str(iter1) + ' ' + str(iter2) + ' ' + DELFUN.__name__ + ' ' +
          strAttributeValue(ATTRIBUTE_VALUE) + ' ' + str(alpha) + ' ' +
          "RULES")

    # test data setup
    decision_table_test, decision_class = getData(FILENAME,
                                                  iter1,
                                                  iter2,
                                                  T="test")
    list_judgeNominal = getJudgeNominal(decision_table_test, FILENAME)

    # predict by LERS
    predictions = LERS.predictByLERS(rules, decision_table_test,
                                     list_judgeNominal)

    # 正答率を求める
    accuracy = accuracy_score(decision_class, predictions)
    # rules の数を求める
    num = len(rules)
    # 各クラスのrulesの数を求める
    num_class = strNumClassRules(rules)
    # 平均の長さを求める
    mean_length = mlem2.getMeanLength(rules)
    # 平均支持度と平均確信度を求める
    list_judgeNominal = getJudgeNominal(decision_table_train, FILENAME)
    mean_support, mean_conf = LERS.getSupportConfidenceRules(
        rules, decision_table_train, list_judgeNominal)
    # AccとRecallを求める
    acc_recall = LERS.getAccurayRecall(rules, decision_table_train,
                                       list_judgeNominal)

    # ファイルにsave
    savepath = DIR_UCI + '/' + FILENAME + '/fairness/02_alpha_preserve/MLEM2_delEAlphaRule_LERS.csv'
    with open(savepath, "a") as f:
        f.writelines(
            'MLEM2_delEAlphaRule_LERS,{DELFUN},{CLASS},{FILENAME},{ATTRIBUTE_VALUE},{alpha},{iter1},{iter2},{acc},{num},{num_class},{mean_length},{mean_support},{mean_conf},{acc_recall}'
            .format(DELFUN=DELFUN.__name__,
                    CLASS=CLASS,
                    FILENAME=FILENAME,
                    ATTRIBUTE_VALUE=strAttributeValue(ATTRIBUTE_VALUE),
                    alpha=alpha,
                    iter1=iter1,
                    iter2=iter2,
                    acc=accuracy,
                    num=num,
                    num_class=num_class,
                    mean_length=mean_length,
                    mean_support=mean_support,
                    mean_conf=mean_conf,
                    acc_recall=strAccRecall(rules, acc_recall)) + "\n")
    print(datetime.now().strftime('%Y/%m/%d %H:%M:%S') + ' ' + FILENAME + ' ' +
          str(iter1) + ' ' + str(iter2) + ' ' + DELFUN.__name__ + ' ' +
          strAttributeValue(ATTRIBUTE_VALUE) + ' ' + str(alpha) + ' ' + "END")

    return (0)
def MLEM2_delEAlphaRule_LERS(FILENAME, iter1, iter2, DELFUN, CLASS, ATTRIBUTE_VALUE, alpha):
    print(
        datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        + " "
        + FILENAME
        + " "
        + str(iter1)
        + " "
        + str(iter2)
        + " "
        + DELFUN.__name__
        + " "
        + strAttributeValue(ATTRIBUTE_VALUE)
        + " "
        + str(alpha)
        + " "
        + "START"
    )

    # rule induction and rule save
    fullpath_filename = DIR_UCI + "/" + FILENAME + "/rules/" + "rules_" + str(iter1) + "-" + str(iter2) + ".pkl"
    rules = (
        mlem2.loadPickleRules(fullpath_filename)
        if os.path.isfile(fullpath_filename)
        else mlem2.getRulesByMLEM2(FILENAME, iter1, iter2)
    )
    if not os.path.isfile(fullpath_filename):
        mlem2.savePickleRules(rules, fullpath_filename)

    # train data setup
    decision_table_train, decision_class = getData(FILENAME, iter1, iter2, T="train")
    list_judgeNominal = getJudgeNominal(decision_table_train, FILENAME)

    # alpha差別的なルールの基本条件削除 or ルールを削除
    if CLASS != "ALL":
        rules_target = mlem2.getRulesClass(rules, CLASS)
        rules_nontarget = mlem2.getRulesClass(rules, CLASS, judge=False)
        for attr in ATTRIBUTE_VALUE:
            for e in ATTRIBUTE_VALUE[attr]:
                rules_target = DELFUN(rules_target, attr, e, decision_table_train, list_judgeNominal, alpha)
        rules_target.extend(rules_nontarget)
        rules = rules_target
    else:
        for attr in ATTRIBUTE_VALUE:
            for e in ATTRIBUTE_VALUE[attr]:
                rules = DELFUN(rules, attr, e, decision_table_train, list_judgeNominal, alpha)

    print(
        datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        + " "
        + FILENAME
        + " "
        + str(iter1)
        + " "
        + str(iter2)
        + " "
        + DELFUN.__name__
        + " "
        + strAttributeValue(ATTRIBUTE_VALUE)
        + " "
        + str(alpha)
        + " "
        + "RULES"
    )

    # test data setup
    decision_table_test, decision_class = getData(FILENAME, iter1, iter2, T="test")
    list_judgeNominal = getJudgeNominal(decision_table_test, FILENAME)

    # predict by LERS
    predictions = LERS.predictByLERS(rules, decision_table_test, list_judgeNominal)

    # 正答率を求める
    accuracy = accuracy_score(decision_class, predictions)
    # rules の数を求める
    num = len(rules)
    # 各クラスのrulesの数を求める
    num_class = strNumClassRules(rules)
    # 平均の長さを求める
    mean_length = mlem2.getMeanLength(rules)
    # 平均支持度と平均確信度を求める
    list_judgeNominal = getJudgeNominal(decision_table_train, FILENAME)
    mean_support, mean_conf = LERS.getSupportConfidenceRules(rules, decision_table_train, list_judgeNominal)
    # AccとRecallを求める
    acc_recall = LERS.getAccurayRecall(rules, decision_table_train, list_judgeNominal)

    # ファイルにsave
    savepath = DIR_UCI + "/" + FILENAME + "/fairness/02_alpha_preserve/MLEM2_delEAlphaRule_LERS.csv"
    with open(savepath, "a") as f:
        f.writelines(
            "MLEM2_delEAlphaRule_LERS,{DELFUN},{CLASS},{FILENAME},{ATTRIBUTE_VALUE},{alpha},{iter1},{iter2},{acc},{num},{num_class},{mean_length},{mean_support},{mean_conf},{acc_recall}".format(
                DELFUN=DELFUN.__name__,
                CLASS=CLASS,
                FILENAME=FILENAME,
                ATTRIBUTE_VALUE=strAttributeValue(ATTRIBUTE_VALUE),
                alpha=alpha,
                iter1=iter1,
                iter2=iter2,
                acc=accuracy,
                num=num,
                num_class=num_class,
                mean_length=mean_length,
                mean_support=mean_support,
                mean_conf=mean_conf,
                acc_recall=strAccRecall(rules, acc_recall),
            )
            + "\n"
        )
    print(
        datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        + " "
        + FILENAME
        + " "
        + str(iter1)
        + " "
        + str(iter2)
        + " "
        + DELFUN.__name__
        + " "
        + strAttributeValue(ATTRIBUTE_VALUE)
        + " "
        + str(alpha)
        + " "
        + "END"
    )

    return 0