示例#1
0
                                                       label_mappings,
                                                       record_mappings)

    from tools.pickle_tools import save_pickle
    save_pickle(hierarhical_mlknn.mltree.content,
                save_hierarchical_path + "mlknn")
    save_pickle(hierarhical_mlknn, save_hierarchical_path)

    save_pickle(list(train_generator()), save_train_generator_path)
    save_pickle(len(labels), save_lenlabels_path)

    classify_oracle = lambda x: mc2lmc_tomka_blad(x)
    print "----------------------------------------------------"
    print "MLKNN:"
    print "PRINTING TEST SAMPLES:"
    for i in test_generator():
        print classify_oracle(i)

    multilabel_evaluate_printresults(
        test_generator, classify_oracle, hierarhical_mlknn.classify,
        len(labels), {
            'full label': lambda x: x,
            'half label': lambda x: x[:3],
            'low label': lambda x: x[:2]
        })

    #print "----------------------------------------------------"
    #print "STUPID KNN:"
    #multilabel_evaluate_printresults(test_generator, classify_oracle, hierarhical_mlknn.classify_stupid, len(labels),
    #                #{'full label': lambda x: x, 'short label': lambda x: x[:1]})
    #                {'full label': lambda x: x, 'half label': lambda x: x[:3], 'low label': lambda x: x[:2]})
 mlknn_callable = lambda train_gen: mlknn.MlKnn(train_gen, zbldistance, find_closest_points.find_closest_points, 
                      k, smoothingparam)
 
 
 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)
 hierarhical_mlknn = ml_hierarchical.MlHierarchical(train_generator, mlknn_callable, label_mappings, record_mappings)
 
 from tools.pickle_tools import save_pickle
 save_pickle(hierarhical_mlknn.mltree.content, save_hierarchical_path+"mlknn")
 save_pickle(hierarhical_mlknn, save_hierarchical_path)
 
 save_pickle(list(train_generator()), save_train_generator_path)
 save_pickle(len(labels), save_lenlabels_path)
 
 
 classify_oracle = lambda x: mc2lmc_tomka_blad(x)
 print "----------------------------------------------------"
 print "MLKNN:"
 print "PRINTING TEST SAMPLES:"
 for i in test_generator():
     print classify_oracle(i)
 
 multilabel_evaluate_printresults(test_generator, classify_oracle, hierarhical_mlknn.classify, len(labels), 
                 {'full label': lambda x: x, 'half label': lambda x: x[:3], 'low label': lambda x: x[:2]})
 
 #print "----------------------------------------------------"
 #print "STUPID KNN:"
 #multilabel_evaluate_printresults(test_generator, classify_oracle, hierarhical_mlknn.classify_stupid, len(labels), 
 #                #{'full label': lambda x: x, 'short label': lambda x: x[:1]})
 #                {'full label': lambda x: x, 'half label': lambda x: x[:3], 'low label': lambda x: x[:2]})
示例#3
0
 prefix_code_generator = lambda: gen_record_filteredbylabels(prefixed_rec_generator, labelsset)
 print "counting elements..."
 elements_count = len(list(prefix_code_generator()))
 print "number of elements:", elements_count
 
 #split into training and testing samples
 print "splitting into training and testing..."
 train_inds, test_inds = randomly_divide(elements_count, int(elements_count/10))
 train_generator = lambda: gen_record_fromshifts(prefix_code_generator, train_inds)
 test_generator = lambda: gen_record_fromshifts(prefix_code_generator, test_inds)
 print "splitted."
 
 #train mlknn:
 print "training distance..."
 zbldistance = jaccard_distance.JaccardDistance(train_generator, elements_count-int(elements_count/10), distancetrainingsteps)
 #zbldistance = lsi_distance.LsiDistance(train_generator, elements_count-int(elements_count/10), distancetrainingsteps, number_of_topics = 100)
 print "training mlknn..."
 mk = mlknn.MlKnn(train_generator, zbldistance, find_closest_points.find_closest_points, 
                      k, smoothingparam)
 
 classify_oracle = lambda x: mc2lmc_tomka_blad(x)
 print "----------------------------------------------------"
 print "MLKNN:"
 multilabel_evaluate_printresults(test_generator, classify_oracle, mk.classify, len(labels), 
                 {'full label': lambda x: x, 'half label': lambda x: x[:3], 'low label': lambda x: x[:2]})
 
 print "----------------------------------------------------"
 print "STUPID KNN:"
 multilabel_evaluate_printresults(test_generator, classify_oracle, mk.classify_stupid, len(labels), 
                 #{'full label': lambda x: x, 'short label': lambda x: x[:1]})
                 {'full label': lambda x: x, 'half label': lambda x: x[:3], 'low label': lambda x: x[:2]})