Exemple #1
0
def MLEM2_RuleClusteringByConsistentSimExceptMRule_Identified(
        FILENAME, iter1, iter2, k, m, p):
    # rule induction
    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)

    # rule save
    if not os.path.isfile(fullpath_filename):
        mlem2.savePickleRules(rules, fullpath_filename)

    # rule clustering
    filepath = DIR_UCI + '/' + FILENAME + '/' + FILENAME + '-train' + str(
        iter1) + '-' + str(iter2) + '.tsv'
    decision_table = mlem2.getDecisionTable(filepath)
    colnames = mlem2.getColNames(decision_table)

    filepath = DIR_UCI + '/' + FILENAME + '/' + FILENAME + '.nominal'
    list_nominal = mlem2.getNominalList(filepath)
    list_judgeNominal = mlem2.getJudgeNominal(decision_table, list_nominal)

    fullpath_filename = DIR_UCI + '/' + FILENAME + '/rules_cluster_consistent_sim_except_mrule/' + 'rules-' + str(
        k) + '_' + str(iter1) + '-' + str(iter2) + '.pkl'
    rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile(
        fullpath_filename
    ) else clustering.getRuleClusteringByConsistentSimilarityExceptMRule(
        rules, colnames, list_judgeNominal, k=k, m=m)

    # PerIdentifiedClass を求める
    ans = mlem2.getPerIdentifiedClass(rules, p)

    # save
    savepath = DIR_UCI + '/' + FILENAME + '/Identify_MLEM2_RuleClusteringByConsistentSimExceptMRule.csv'
    with open(savepath, "a") as f:
        f.writelines(
            'Identify_MLEM2_RuleClusteringByConsistentSimExceptMRule,{k},{p},{FILENAME},{iter1},{iter2},{ans}'
            .format(
                FILENAME=FILENAME, k=k, p=p, iter1=iter1, iter2=iter2,
                ans=ans) + "\n")

    return (ans)
def MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS(
        FILENAME, iter1, iter2, k, m):
    # rule induction
    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)

    # rule save
    if not os.path.isfile(fullpath_filename):
        mlem2.savePickleRules(rules, fullpath_filename)

    # rule clustering
    filepath = DIR_UCI + '/' + FILENAME + '/' + FILENAME + '-train' + str(
        iter1) + '-' + str(iter2) + '.tsv'
    decision_table = mlem2.getDecisionTable(filepath)
    colnames = mlem2.getColNames(decision_table)

    filepath = DIR_UCI + '/' + FILENAME + '/' + FILENAME + '.nominal'
    list_nominal = mlem2.getNominalList(filepath)
    list_judgeNominal = mlem2.getJudgeNominal(decision_table, list_nominal)

    fullpath_filename = DIR_UCI + '/' + FILENAME + '/rules_cluster_consistent_sim_except_mrule/' + 'rules-' + str(
        k) + '_' + str(iter1) + '-' + str(iter2) + '.pkl'
    rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile(
        fullpath_filename
    ) else clustering.getRuleClusteringByConsistentSimilarityExceptMRule(
        rules, colnames, list_judgeNominal, k=k, m=m)

    # rule save
    if not os.path.isfile(fullpath_filename):
        mlem2.savePickleRules(rules, fullpath_filename)

    # test data setup
    filepath = DIR_UCI + '/' + FILENAME + '/' + FILENAME + '-test' + str(
        iter1) + '-' + str(iter2) + '.tsv'
    decision_table_test = mlem2.getDecisionTable(filepath)
    decision_table_test = decision_table_test.dropna()
    decision_class = decision_table_test[
        decision_table_test.columns[-1]].values.tolist()

    filepath = DIR_UCI + '/' + FILENAME + '/' + FILENAME + '.nominal'
    list_nominal = mlem2.getNominalList(filepath)
    list_judgeNominal = mlem2.getJudgeNominal(decision_table_test,
                                              list_nominal)

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

    # 正答率を求める
    accuracy = accuracy_score(decision_class, predictions)

    #print('{FILENAME} : {iter1} {iter2}'.format(FILENAME=FILENAME,iter1=iter1,iter2=iter2))
    #logging.info('MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS,{k},{FILENAME},{iter1},{iter2},{acc}'.format(FILENAME=FILENAME,k=k,iter1=iter1,iter2=iter2,acc=accuracy))
    savepath = DIR_UCI + '/' + FILENAME + '/MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS.csv'
    with open(savepath, "a") as f:
        f.writelines(
            'MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS,{k},{FILENAME},{iter1},{iter2},{acc}'
            .format(
                FILENAME=FILENAME, k=k, iter1=iter1, iter2=iter2,
                acc=accuracy) + "\n")

    return (accuracy)
def MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS(FILENAME, iter1, iter2, k, m) :
    # rule induction
    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) 

    # rule save
    if not os.path.isfile(fullpath_filename): mlem2.savePickleRules(rules, fullpath_filename) 

    # rule clustering
    filepath = DIR_UCI+'/'+FILENAME+'/'+FILENAME+'-train'+str(iter1)+'-'+str(iter2)+'.tsv'
    decision_table = mlem2.getDecisionTable(filepath)
    colnames = mlem2.getColNames(decision_table)
    
    filepath = DIR_UCI+'/'+FILENAME+'/'+FILENAME+'.nominal'
    list_nominal = mlem2.getNominalList(filepath)
    list_judgeNominal = mlem2.getJudgeNominal(decision_table, list_nominal)

    fullpath_filename = DIR_UCI+'/'+FILENAME+'/rules_cluster_consistent_sim_except_mrule/'+'rules-'+str(k)+'_'+str(iter1)+'-'+str(iter2)+'.pkl'
    rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile(fullpath_filename) else clustering.getRuleClusteringByConsistentSimilarityExceptMRule(rules, colnames, list_judgeNominal, k=k, m=m)

    # rule save
    if not os.path.isfile(fullpath_filename): mlem2.savePickleRules(rules, fullpath_filename) 

    # test data setup
    filepath = DIR_UCI+'/'+FILENAME+'/'+FILENAME+'-test'+str(iter1)+'-'+str(iter2)+'.tsv'
    decision_table_test = mlem2.getDecisionTable(filepath)
    decision_table_test = decision_table_test.dropna()
    decision_class = decision_table_test[decision_table_test.columns[-1]].values.tolist()

    filepath = DIR_UCI+'/'+FILENAME+'/'+FILENAME+'.nominal'
    list_nominal = mlem2.getNominalList(filepath)
    list_judgeNominal = mlem2.getJudgeNominal(decision_table_test, list_nominal)
    
    # predict by LERS
    predictions = LERS.predictByLERS(rules, decision_table_test, list_judgeNominal)
    
    # 正答率を求める
    accuracy = accuracy_score(decision_class, predictions)
    
    #print('{FILENAME} : {iter1} {iter2}'.format(FILENAME=FILENAME,iter1=iter1,iter2=iter2))    
    #logging.info('MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS,{k},{FILENAME},{iter1},{iter2},{acc}'.format(FILENAME=FILENAME,k=k,iter1=iter1,iter2=iter2,acc=accuracy))
    savepath = DIR_UCI+'/'+FILENAME+'/MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS.csv'
    with open(savepath, "a") as f :
        f.writelines('MLEM2_RuleClusteringByConsistentSimExceptMRule_LERS,{k},{FILENAME},{iter1},{iter2},{acc}'.format(FILENAME=FILENAME,k=k,iter1=iter1,iter2=iter2,acc=accuracy)+"\n")
    
    return(accuracy)