Ejemplo n.º 1
0
def grid(problem_path,result_save_path,svm_type,coarse_c_range,coarse_g_range,fine_c_step,fine_g_step):
    '''搜索的主文件;
    svm_type :使用的模型的类型。"libsvm"或者"liblinear"
    coarse_c_range :粗粒度c搜索的范围,为一个truple (begin,end,step)
    coarse_g_range :粗粒度g搜索的范围,为一个truple (begin,end,step)
    fine_c_step :细粒度c搜索的步长,搜索范围为(fine_c-coarse_c_step,fine_c+coarse_c_step,fine_c_step),如果为0,则固定为(fine_c,fine_c,fine_c)
    fine_g_step :细粒度g搜索的步长,搜索范围为(fine_g-coarse_g_step,fine_g+coarse_g_step,fine_g_step),如果为0,则固定为(fine_g,fine_g,fine_g)
    '''
    tms_svm.set_svm_type(svm_type)
    y,x  = tms_svm.read_problem(problem_path)
    fw= file(result_save_path,'w')
    c,g=grid_search_for_large_data(y,x,fw,coarse_c_range,coarse_g_range,fine_c_step,fine_g_step)  
    fw.close()
    return c,g
def ctm_train_model(sample_save_path,svm_type,param,model_save_path):
    '''训练模型,输入样本文件,训练的参数,模型的保存地址,最后会给出模型在训练样本上的测试结果。'''
    tms_svm.set_svm_type(svm_type)
    y,x = tms_svm.read_problem(sample_save_path)
    m = tms_svm.train(y,x,param)
    tms_svm.save_model(model_save_path,m)
    labels = {}.fromkeys(y).keys()
    if len(labels)>2:
        pred_labels, (Micro, Macro, ACC), pred_values = tms_svm.predict(y,x,m)
        print "(Micro=%g, Macro=%g, ACC=%g)"%(Micro, Macro, ACC)
    else:
        pred_labels, (f_score,recall,presion), pred_values=tms_svm.predict(y,x,m)
        print "(f_score=%g,recall=%g,presion=%g)"%(f_score,recall,presion)
    return m
Ejemplo n.º 3
0
def grid(problem_path, result_save_path, svm_type, coarse_c_range,
         coarse_g_range, fine_c_step, fine_g_step):
    '''搜索的主文件;
    svm_type :使用的模型的类型。"libsvm"或者"liblinear"
    coarse_c_range :粗粒度c搜索的范围,为一个truple (begin,end,step)
    coarse_g_range :粗粒度g搜索的范围,为一个truple (begin,end,step)
    fine_c_step :细粒度c搜索的步长,搜索范围为(fine_c-coarse_c_step,fine_c+coarse_c_step,fine_c_step),如果为0,则固定为(fine_c,fine_c,fine_c)
    fine_g_step :细粒度g搜索的步长,搜索范围为(fine_g-coarse_g_step,fine_g+coarse_g_step,fine_g_step),如果为0,则固定为(fine_g,fine_g,fine_g)
    '''
    tms_svm.set_svm_type(svm_type)
    y, x = tms_svm.read_problem(problem_path)
    fw = file(result_save_path, 'w')
    c, g = grid_search_for_large_data(y, x, fw, coarse_c_range, coarse_g_range,
                                      fine_c_step, fine_g_step)
    fw.close()
    return c, g
Ejemplo n.º 4
0
def save_train_for_lsa(test_path,model_save_path,lsa_train_save_path):
    '''predict trainset using the initial classifier  ,and save the trainset with
    lsa format : label score feature
    '''
    y,x = tms_svm.read_problem(test_path)
    m = tms_svm.load_model(model_save_path)
    p_lab,p_acc,p_sc = tms_svm.predict(y,x,m)
    f= file(lsa_train_save_path,'w')
    for i  in range(len(y)):
        f.write(str(int(y[i]))+"\t"+str(p_sc[i][0])+"\t")
        dic =x[i]
        sorted_x = sorted(dic.items(),key = lambda dic:dic[0])
        for key in sorted_x:
            f.write(str(key[0])+":"+str(key[1])+"\t")
        f.write("\n")
    f.close()   
def save_train_for_lsa(test_path, model_save_path, lsa_train_save_path):
    '''predict trainset using the initial classifier  ,and save the trainset with
    lsa format : label score feature
    '''
    y, x = tms_svm.read_problem(test_path)
    m = tms_svm.load_model(model_save_path)
    p_lab, p_acc, p_sc = tms_svm.predict(y, x, m)
    f = file(lsa_train_save_path, 'w')
    for i in range(len(y)):
        f.write(str(int(y[i])) + "\t" + str(p_sc[i][0]) + "\t")
        dic = x[i]
        sorted_x = sorted(dic.items(), key=lambda dic: dic[0])
        for key in sorted_x:
            f.write(str(key[0]) + ":" + str(key[1]) + "\t")
        f.write("\n")
    f.close()
Ejemplo n.º 6
0
def ctm_model_predict(test_path, m):
    '''模型预测,输入测试样本,然后读入进行测试'''
    y, x = tms_svm.read_problem(test_path)
    return tms_svm.predict(y, x, m)
def ctm_model_predict(test_path,m):
    '''模型预测,输入测试样本,然后读入进行测试'''
    y,x = tms_svm.read_problem(test_path)
    return tms_svm.predict(y,x,m)