コード例 #1
0
        mlknn_callable = lambda train_gen, get_labels_of_record_arg: mlknn_threshold.MlknnThreshold(
            train_gen, lambda sample, k: get_neighbours(sample, k, train_gen),
            k, smoothing_param, get_labels_of_record_arg, lambda x: 1, printer)

    elif classifier_name == 'mlknn-tensembled-tree':
        k = map(int, k.strip().split(','))
        PRINTER("loaded k-list: " + str(k))
        from mlknn import mlknn_tensembled
        mlknn_callable = lambda train_gen, get_labels_of_record_arg: mlknn_tensembled.MlknnTEnsembled(
            train_gen, lambda sample, k: get_neighbours(sample, k, train_gen),
            k, get_labels_of_record_arg, lambda x: 1, printer)

    label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)

    from mltools.ml_hierarchical import MlHierarchical
    classifier = MlHierarchical(train_generator_list, mlknn_callable,
                                label_mappings, get_labels_of_record)

    PRINTER("Time taken for training:" + str(start - time()))

    PRINTER("------------------------")
    PRINTER("---Testing classifier---")
    PRINTER("------------------------")
    test_generator = read_pickle(load_test_generator)
    labels = read_pickle(load_labels_path)

    classify_oracle = mc2lmc_tomka_blad
    from mltools.multilabel_evaluate import multilabel_evaluate_printresults
    PRINTER("-----------RESULTS-----------")
    multilabel_evaluate_printresults(lambda: test_generator, classify_oracle,
                                     classifier.__getattribute__('classify'),
                                     len(labels),
コード例 #2
0
def main(train_generator_list, labels, elements_count, classifier_name, k, smoothing_param, distancematrix, test_generator):
    PRINTER("Finding label list...")
    get_labels_of_record = mc2lmc_tomka_blad
    find_all_labels = lambda frecords: get_labels_min_occurence(lambda: gen_lmc(frecords), 1)
    
    PRINTER("Loading distance matrix...")
    import sys
    sys.path.append(r'../')
    from data_io.matrix_io import fread_smatrix
    (rows, cols, data) = fread_smatrix(distancematrix)
    id2rowind, id2colind = {}, {}
    for ind, id in enumerate(rows):
        id2rowind[id] = ind
    for ind, id in enumerate(cols):
        id2colind[id] = ind
        
    #print "len(train_generator_list):",len(train_generator_list)
    #print "len(test_generator_list):",len(test_generator)
    #print "len(rows):",len(rows) 
    #print "(rows, cols, data):", (rows, cols, data)
    
    
    PRINTER("Training classifier...")
    from time import time
    
    def printer(x):
        #import logging
        logging.info('['+classifier_name+']'+x)

    def distance(a, b): 
        try:
            return data[id2rowind[a['an']]][id2colind[b['an']]]
        except:
            return data[id2colind[b['an']]][id2rowind[a['an']]]
        
        
    start = time()
    if classifier_name=='mlknn_basic':
        def get_neighbours(sample, k):
            return find_closest_points_sorted(sample, train_generator_list, [sample], k, distance)
        k = int(k)
        from mlknn import mlknn_basic
        classifier = mlknn_basic.MlknnBasic(train_generator_list, get_neighbours, k, smoothing_param, get_labels_of_record, lambda x:1, printer)
    
    elif classifier_name == 'mlknn_threshold':
        def get_neighbours(sample, k):
            return find_closest_points_sorted(sample, train_generator_list, [sample], k, distance)
        k = int(k)
        from mlknn import mlknn_threshold
        classifier = mlknn_threshold.MlknnThreshold(train_generator_list, get_neighbours, k, smoothing_param, get_labels_of_record, lambda x:1, printer)
        
    elif classifier_name == 'mlknn_tensembled':
        def get_neighbours(sample, k):
            return find_closest_points_sorted(sample, train_generator_list, [sample], k, distance)
        k = map(int, k.strip().split(','))
        PRINTER("loaded k-list: "+str(k))
        from mlknn import mlknn_tensembled
        classifier = mlknn_tensembled.MlknnTEnsembled(train_generator_list, get_neighbours, k, get_labels_of_record, lambda x:1, printer)
    
    elif classifier_name=='mlknn-basic-tree':
        def get_neighbours(sample, k, train_gen):
            return find_closest_points_sorted(sample, train_gen, [sample], k, distance)
        k = int(k)
        from mlknn import mlknn_basic
        mlknn_callable = lambda train_gen, get_labels_of_record_arg: mlknn_basic.MlknnBasic(train_gen, lambda sample, k: get_neighbours(sample, k, train_gen), 
                                                                           k, smoothing_param, get_labels_of_record_arg, lambda x:1, printer)
        label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)
        from mltools.ml_hierarchical import MlHierarchical
        classifier = MlHierarchical(train_generator_list, mlknn_callable, label_mappings, get_labels_of_record)
    
    elif classifier_name == 'mlknn-threshold-tree':
        def get_neighbours(sample, k, train_gen):
            return find_closest_points_sorted(sample, train_gen, [sample], k, distance)
        k = int(k)
        from mlknn import mlknn_threshold
        mlknn_callable = lambda train_gen, get_labels_of_record_arg: mlknn_threshold.MlknnThreshold(train_gen, lambda sample, k: get_neighbours(sample, k, train_gen), 
                                                                           k, smoothing_param, get_labels_of_record_arg, lambda x:1, printer)
        label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)
        from mltools.ml_hierarchical import MlHierarchical
        classifier = MlHierarchical(train_generator_list, mlknn_callable, label_mappings, get_labels_of_record)
    
    elif classifier_name == 'mlknn-tensembled-tree':
        def get_neighbours(sample, k, train_gen):
            return find_closest_points_sorted(sample, train_gen, [sample], k, distance)
        k = map(int, k.strip().split(','))
        PRINTER("loaded k-list: "+str(k))
        from mlknn import mlknn_tensembled
        mlknn_callable = lambda train_gen, get_labels_of_record_arg: mlknn_tensembled.MlknnTEnsembled(train_gen, lambda sample, k: get_neighbours(sample, k, train_gen),
                                                                                                      k, get_labels_of_record_arg, lambda x:1, printer)
        label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)
        from mltools.ml_hierarchical import MlHierarchical
        classifier = MlHierarchical(train_generator_list, mlknn_callable, label_mappings, get_labels_of_record)
    
    
    PRINTER("Time taken for training:"+str(start-time()))    
    PRINTER("------------------------")
    PRINTER("---Testing classifier---")
    PRINTER("------------------------")

    classify_oracle = mc2lmc_tomka_blad
    from mltools.multilabel_evaluate import multilabel_evaluate, multilabel_evaluate_printresults
    accuracy, precision, recall, hammingloss, subset01loss, fmeasure = multilabel_evaluate(lambda: test_generator, classify_oracle, classifier.__getattribute__('classify'), len(labels), 
                    [('full label', lambda x: x), ('half label', lambda x: x[:3]), ('low label', lambda x: x[:2])])
    PRINTER("-----------RESULTS-----------")
    multilabel_evaluate_printresults(accuracy, precision, recall, hammingloss, subset01loss, fmeasure, PRINTER)
    return accuracy, precision, recall, hammingloss, subset01loss, fmeasure
コード例 #3
0
    elements_count = read_pickle(load_elements_count_path)

    train_generator = lambda: train_generator_list
    #train mlknn:
    PRINTER("Training Distance...")
    zbldistance = JaccardDistance(train_generator,
                                  elements_count - int(elements_count / 10),
                                  distancetrainingsteps)

    get_labels_of_record = mc2lmc_tomka_blad
    find_all_labels = lambda frecords: get_labels_min_occurence(
        lambda: gen_lmc(frecords), 1)

    mlknn_callable = lambda train_gen: MlKnn(
        train_gen, zbldistance, find_closest_points, k, smoothingparam,
        find_all_labels, get_labels_of_record)

    label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)
    record_mappings = (lambda x: gen_1record_prefixed(x, 2),
                       lambda x: gen_1record_prefixed(x, 3), lambda x: x)

    PRINTER("Training hierarchical mlknn...")
    from time import time
    start = time()
    hierarhical_mlknn = MlHierarchical(train_generator, mlknn_callable,
                                       label_mappings, record_mappings)
    PRINTER("time taken for training:" + str(start - time()))

    from tools.pickle_tools import save_pickle
    save_pickle(hierarhical_mlknn, save_classifier_path)
        from mlknn import mlknn_threshold
        mlknn_callable = lambda train_gen, get_labels_of_record_arg: mlknn_threshold.MlknnThreshold(train_gen, lambda sample, k: get_neighbours(sample, k, train_gen), 
                                                                           k, smoothing_param, get_labels_of_record_arg, lambda x:1, printer)
    
    elif classifier_name == 'mlknn-tensembled-tree':
        k = map(int, k.strip().split(','))
        PRINTER("loaded k-list: "+str(k))
        from mlknn import mlknn_tensembled
        mlknn_callable = lambda train_gen, get_labels_of_record_arg: mlknn_tensembled.MlknnTEnsembled(train_gen, lambda sample, k: get_neighbours(sample, k, train_gen), 
                                                                                                      k, get_labels_of_record_arg, lambda x:1, printer)

    label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)
    
    
    from mltools.ml_hierarchical import MlHierarchical
    classifier = MlHierarchical(train_generator_list, mlknn_callable, label_mappings, get_labels_of_record)
        
    PRINTER("Time taken for training:"+str(start-time()))
    
    PRINTER("------------------------")
    PRINTER("---Testing classifier---")
    PRINTER("------------------------")
    test_generator = read_pickle(load_test_generator) 
    labels = read_pickle(load_labels_path)


    classify_oracle = mc2lmc_tomka_blad
    from mltools.multilabel_evaluate import multilabel_evaluate_printresults
    PRINTER("-----------RESULTS-----------")
    multilabel_evaluate_printresults(lambda: test_generator, classify_oracle, classifier.__getattribute__('classify'), len(labels), 
                    [('full label', lambda x: x), ('half label', lambda x: x[:3]), ('low label', lambda x: x[:2])], labels)
            distancetrainingsteps)
    else:
        from mlknn.txt_cosine_distance import TxtCosineDistance
        zbldistance = TxtCosineDistance(distancetype)

    get_labels_of_record = mc2lmc_tomka_blad
    mlknn_callable = lambda train_gen, get_labels_of_record_arg: MlKnnFractional(
        train_gen, zbldistance, find_closest_points, k,
        get_labels_of_record_arg)

    label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)

    PRINTER("Training hierarchical mlknn...")
    from time import time
    start = time()
    hierarhical_mlknn = MlHierarchical(train_generator, mlknn_callable,
                                       label_mappings, get_labels_of_record)
    PRINTER("time taken for training:" + str(start - time()))

    PRINTER("Testing hierarchical mlknn fractional...")
    test_generator = read_pickle(load_test_generator)
    labels = read_pickle(load_labels_path)

    #print "Finding out if the ML-hierarchical has internal data..."
    #check_internal_data(hierarhical_mlknn)
    classify_oracle = mc2lmc_tomka_blad

    #print "----------------------------------------------------"
    #print "Hierachical MLKNN:"
    from mltools.multilabel_evaluate import multilabel_evaluate_printresults
    PRINTER("-----------RESULTS-----------")
    multilabel_evaluate_printresults(
コード例 #6
0
     from mlknn.jaccard_distance import JaccardDistance
     zbldistance = JaccardDistance(train_generator, elements_count-int(elements_count/10), distancetrainingsteps)
 else:
     from mlknn.txt_cosine_distance import TxtCosineDistance 
     zbldistance = TxtCosineDistance(distancetype)
 
 get_labels_of_record = mc2lmc_tomka_blad
 mlknn_callable = lambda train_gen, get_labels_of_record_arg: MlKnn(train_gen, zbldistance, find_closest_points, 
                      k, smoothingparam, get_labels_of_record_arg)
 
 label_mappings = (lambda x: x[:2], lambda x: x[:3], lambda x: x)
 
 PRINTER("Training hierarchical mlknn...")
 from time import time
 start = time()
 hierarhical_mlknn = MlHierarchical(train_generator, mlknn_callable, label_mappings, get_labels_of_record)
 PRINTER("time taken for training:"+str(start-time()))
 
 PRINTER("Testing hierarchical mlknn...")
 test_generator = read_pickle(load_test_generator) 
 labels = read_pickle(load_labels_path)
 
 #print "Finding out if the ML-hierarchical has internal data..."
 #check_internal_data(hierarhical_mlknn)
 classify_oracle = mc2lmc_tomka_blad
 
 #print "----------------------------------------------------"
 #print "Hierachical MLKNN:"
 from mltools.multilabel_evaluate import multilabel_evaluate_printresults
 PRINTER("-----------RESULTS-----------")
 multilabel_evaluate_printresults(lambda: test_generator, classify_oracle, hierarhical_mlknn.__getattribute__('classify'), len(labels),