def getDescriptors(decision_table) : list_descriptors = defaultdict(list) columns = mlem2.getColNames(decision_table) for i in range(len(columns)) : tmp = list(pd.Series(decision_table.ix[:,i]).unique()) list_descriptors[columns[i]] = tmp return(list_descriptors)
def MLEM2_RuleClusteringByConsistentExceptMRule_STAT(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_except_mrule/' + 'rules-' + str( k) + '_' + str(iter1) + '-' + str(iter2) + '.pkl' rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile( fullpath_filename ) else clustering.getRuleClusteringByConsistentExceptMRule( rules, colnames, list_judgeNominal, k=k, m=m) # rule save if not os.path.isfile(fullpath_filename): mlem2.savePickleRules(rules, fullpath_filename) # rules の数を求める num = len(rules) # 平均の長さを求める leng = mlem2.getMeanLength(rules) # 平均支持度を求める support = mlem2.getMeanSupport(rules) # ファイルにsave savepath = DIR_UCI + '/' + FILENAME + '/MLEM2_RuleClusteringByConsistentExceptMRule_STAT.csv' with open(savepath, "a") as f: f.writelines( 'MLEM2_RuleClusteringByConsistentExceptMRule_STAT,{k},{FILENAME},{iter1},{iter2},{num},{leng},{support}' .format(FILENAME=FILENAME, k=k, iter1=iter1, iter2=iter2, num=num, leng=leng, support=support) + "\n") return (0)
def MLEM2_RuleClusteringByConsistentSim_Identified(FILENAME, iter1, iter2, k, 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/' + 'rules-' + str( k) + '_' + str(iter1) + '-' + str(iter2) + '.pkl' rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile( fullpath_filename ) else clustering.getRuleClusteringByConsistentSimilarity( rules, colnames, list_judgeNominal, k=k) # rule save if not os.path.isfile(fullpath_filename): mlem2.savePickleRules(rules, fullpath_filename) # PerIdentifiedClass を求める ans = mlem2.getPerIdentifiedClass(rules, p) # save savepath = DIR_UCI + '/' + FILENAME + '/Identify_MLEM2_RuleClusteringByConsistentSim.csv' with open(savepath, "a") as f: f.writelines( 'Identify_MLEM2_RuleClusteringByConsistentSim,{k},{p},{FILENAME},{iter1},{iter2},{ans}' .format( FILENAME=FILENAME, k=k, p=p, iter1=iter1, iter2=iter2, ans=ans) + "\n") return (ans)
def MLEM2_RuleClusteringBySim_LERS(FILENAME, iter1, iter2, k) : # 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_sim/'+'rules-'+str(k)+'_'+str(iter1)+'-'+str(iter2)+'.pkl' rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile(fullpath_filename) else clustering.getRuleClusteringBySimilarity(rules, colnames, list_judgeNominal, k=k) # 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_RuleClusteringBySim_LERS,{k},{FILENAME},{iter1},{iter2},{acc}'.format(FILENAME=FILENAME,k=k,iter1=iter1,iter2=iter2,acc=accuracy)) savepath = DIR_UCI+'/'+FILENAME+'/MLEM2_RuleClusteringBySim_LERS.csv' with open(savepath, "a") as f : f.writelines('MLEM2_RuleClusteringBySim_LERS,{k},{FILENAME},{iter1},{iter2},{acc}'.format(FILENAME=FILENAME,k=k,iter1=iter1,iter2=iter2,acc=accuracy)+"\n") return(accuracy)
# ======================================== # main # ======================================== if __name__ == "__main__": FILENAME = 'hayes-roth' iter1 = 5 iter2 = 4 rules = mlem2.getRulesByMLEM2(FILENAME, iter1, iter2) filepath = '/mnt/data/uci/'+FILENAME+'/'+FILENAME+'-train'+str(iter1)+'-'+str(iter2)+'.tsv' decision_table = mlem2.getDecisionTable(filepath) colnames = mlem2.getColNames(decision_table) filepath = '/mnt/data/uci/'+FILENAME+'/'+FILENAME+'.nominal' list_nominal = mlem2.getNominalList(filepath) list_judgeNominal = mlem2.getJudgeNominal(decision_table, list_nominal) # ルールクラスタリング #rules_new = getRuleClusteringBySimilarity(rules, colnames, list_judgeNominal, k=3) #rules_new = getRuleClusteringByRandom(rules, k=3) #rules_new = getRuleClusteringBySameCondition(rules, k=3) #rules_new = getRuleClusteringByConsistentSimilarity(rules, colnames, list_judgeNominal, k=3) #rules_new = getRuleClusteringByConsistentSimilarityExceptMRule(rules, colnames, list_judgeNominal, k=3, m=3) #rules_new = getRuleClusteringByConsistentTimesSimilarityExceptMRule(rules, colnames, list_judgeNominal, k=3, m=3) rules_new = getRuleClusteringBySimilarityExceptMRule(rules, colnames, list_judgeNominal, k=3, m=3) rules_new = getRuleClusteringByConsistentExceptMRule(rules, colnames, list_judgeNominal, k=3, m=3)
def MLEM2_RuleClusteringBySim_LERS(FILENAME, iter1, iter2, k): # 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_sim/' + 'rules-' + str( k) + '_' + str(iter1) + '-' + str(iter2) + '.pkl' rules = mlem2.loadPickleRules(fullpath_filename) if os.path.isfile( fullpath_filename) else clustering.getRuleClusteringBySimilarity( rules, colnames, list_judgeNominal, k=k) # 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_RuleClusteringBySim_LERS,{k},{FILENAME},{iter1},{iter2},{acc}'.format(FILENAME=FILENAME,k=k,iter1=iter1,iter2=iter2,acc=accuracy)) savepath = DIR_UCI + '/' + FILENAME + '/MLEM2_RuleClusteringBySim_LERS.csv' with open(savepath, "a") as f: f.writelines( 'MLEM2_RuleClusteringBySim_LERS,{k},{FILENAME},{iter1},{iter2},{acc}' .format( FILENAME=FILENAME, k=k, iter1=iter1, iter2=iter2, acc=accuracy) + "\n") return (accuracy)
# ======================================== # main # ======================================== if __name__ == "__main__": FILENAME = 'hayes-roth' iter1 = 5 iter2 = 4 rules = mlem2.getRulesByMLEM2(FILENAME, iter1, iter2) filepath = '/mnt/data/uci/' + FILENAME + '/' + FILENAME + '-train' + str( iter1) + '-' + str(iter2) + '.tsv' decision_table = mlem2.getDecisionTable(filepath) colnames = mlem2.getColNames(decision_table) filepath = '/mnt/data/uci/' + FILENAME + '/' + FILENAME + '.nominal' list_nominal = mlem2.getNominalList(filepath) list_judgeNominal = mlem2.getJudgeNominal(decision_table, list_nominal) # ルールクラスタリング #rules_new = getRuleClusteringBySimilarity(rules, colnames, list_judgeNominal, k=3) #rules_new = getRuleClusteringByRandom(rules, k=3) #rules_new = getRuleClusteringBySameCondition(rules, k=3) #rules_new = getRuleClusteringByConsistentSimilarity(rules, colnames, list_judgeNominal, k=3) #rules_new = getRuleClusteringByConsistentSimilarityExceptMRule(rules, colnames, list_judgeNominal, k=3, m=3) #rules_new = getRuleClusteringByConsistentTimesSimilarityExceptMRule(rules, colnames, list_judgeNominal, k=3, m=3) rules_new = getRuleClusteringBySimilarityExceptMRule(rules, colnames, list_judgeNominal,