Пример #1
0
def train_model(dataset):
    model_result_dict = {}
    for model in model_list:
        model_result_dict[model] = util.ResultItem()

    for k in range(0, rand_num):
        if rand_num > 1:
            print 'shuffle datset...'
            dataset.shuffle_file()

        for model in model_list:
            print '-----------------------------------'
            print ' Experiment on %s' % model + ' Random %d' % k
            print '-----------------------------------'

            #create destination folder
            dst_folder = dataset.name + '/%s' % model
            if os.path.exists(dst_folder) == False:
                os.makedirs(dst_folder)

            #output file
            result_file = '{0}/{1}_result_{2}.txt'.format(dst_folder, model, k)
            result_file = result_file.replace('/', os.sep)
            #clear the file if it already exists
            open(result_file, 'w').close()

            if model == 'liblinear':

                result_once = run_liblinear.run(dataset, model_config,
                                                result_file)
            elif model == 'FGM':
                result_once = run_fgm.run(dataset, model_config, result_file)
            elif model == 'mRMR':
                run_mRMR.run(dataset, model_config)
                continue
            elif model == 'BIF':
                run_bif.run(dataset, model_config)
                continue
            else:
                param_config = ''
                #get parameters
                if is_default_param == False:
                    param_config = dataset.get_best_param(model)

                result_once = run_ofs.run(dataset, model, model_config,
                                          param_config, result_file)

            model_result_dict[model].Add(result_once)
        dataset.del_rand_file()

    #average the result
    if (rand_num > 1):
        for key, val in model_result_dict.iteritems():
            val.Divide(rand_num)

    return model_result_dict
Пример #2
0
def train_fs(dataset, model, model_config, result_file):
    if model == 'liblinear':
        result_once = run_liblinear.run(dataset, model_config, result_file)
    elif model == 'FGM':
        result_once = run_fgm.run(dataset, model_config, result_file)
    elif model == 'mRMR':
        run_mRMR.run(dataset, model_config)
    elif model == 'BIF':
        run_bif.run(dataset, model_config)
    else:
        param_config = ''
        #get parameters
        if model_config['is_default_param'] == False:
            param_config = dataset.get_best_param(model)

        result_once = run_ofs.run(dataset,model, model_config, param_config, result_file)
    return result_once