研究libsvm 直到能够将其 纳为己用
大部分代码来自libsvm压缩包里的文件,通过分析里面的代码得出其用法。
Title 1—svm_parameter参数的设置:
svm_parameter 该类用来设置libsvm的一些配置参数,参数有点多,具体的参数说明如下:
private static void exit_with_help() { System.out.print( "Usage: svm_train [options] training_set_file [model_file]\n" +"options:\n" +"-s svm_type : set type of SVM (default 0)\n" +" 0 -- C-SVC (multi-class classification)\n" +" 1 -- nu-SVC (multi-class classification)\n" +" 2 -- one-class SVM\n" +" 3 -- epsilon-SVR (regression)\n" +" 4 -- nu-SVR (regression)\n" +"-t kernel_type : set type of kernel function (default 2)\n" +" 0 -- linear: u'*v\n" +" 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" +" 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" +" 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" +" 4 -- precomputed kernel (kernel values in training_set_file)\n" +"-d degree : set degree in kernel function (default 3)\n" +"-g gamma : set gamma in kernel function (default 1/num_features)\n" +"-r coef0 : set coef0 in kernel function (default 0)\n" +"-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" +"-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" +"-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" +"-m cachesize : set cache memory size in MB (default 100)\n" +"-e epsilon : set tolerance of termination criterion (default 0.001)\n" +"-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" +"-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" +"-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" +"-v n : n-fold cross validation mode\n" +"-q : quiet mode (no outputs)\n" ); System.exit(1); }
也可以使用默认的参数,默认的参数为: 需要注意的是为gamma参数为1/num_features而不是0
// default values param.svm_type = svm_parameter.C_SVC; param.kernel_type = svm_parameter.RBF; param.degree = 3; param.gamma = 0; // 1/num_features param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; param.C = 1; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = new int[0]; param.weight = new double[0]; cross_validation = 0;
完整参考代码如下 :
private void parse_command_line(String argv[]) { int i; svm_print_interface print_func = null; // default printing to stdout param = new svm_parameter(); // default values param.svm_type = svm_parameter.C_SVC; param.kernel_type = svm_parameter.RBF; param.degree = 3; param.gamma = 0; // 1/num_features param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; param.C = 1; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = new int[0]; param.weight = new double[0]; cross_validation = 0; // parse options for(i=0;i=argv.length) exit_with_help(); switch(argv[i-1].charAt(1)) { case 's': param.svm_type = atoi(argv[i]); break; case 't': param.kernel_type = atoi(argv[i]); break; case 'd': param.degree = atoi(argv[i]); break; case 'g': param.gamma = atof(argv[i]); break; case 'r': param.coef0 = atof(argv[i]); break; case 'n': param.nu = atof(argv[i]); break; case 'm': param.cache_size = atof(argv[i]); break; case 'c': param.C = atof(argv[i]); break; case 'e': param.eps = atof(argv[i]); break; case 'p': param.p = atof(argv[i]); break; case 'h': param.shrinking = atoi(argv[i]); break; case 'b': param.probability = atoi(argv[i]); break; case 'q': print_func = svm_print_null; i--; break; case 'v': cross_validation = 1; nr_fold = atoi(argv[i]); if(nr_fold < 2) { System.err.print("n-fold cross validation: n must >= 2\n"); exit_with_help(); } break; case 'w': ++param.nr_weight; { int[] old = param.weight_label; param.weight_label = new int[param.nr_weight]; System.arraycopy(old,0,param.weight_label,0,param.nr_weight-1); } { double[] old = param.weight; param.weight = new double[param.nr_weight]; System.arraycopy(old,0,param.weight,0,param.nr_weight-1); } param.weight_label[param.nr_weight-1] = atoi(argv[i-1].substring(2)); param.weight[param.nr_weight-1] = atof(argv[i]); break; default: System.err.print("Unknown option: " + argv[i-1] + "\n"); exit_with_help(); } } svm.svm_set_print_string_function(print_func); // determine filenames if(i>=argv.length) exit_with_help(); input_file_name = argv[i]; if(i
Title 2 : svm_problem 用来保存样本实例的
prob.l l用来保存样本的数量
prob.x x是一个svm_node类型的二维数组,每一行代表一个样本,svm_node实例里的value代表属性值,index代表属性的序号。
prob.y y是一个double类型的一维数组,用来保存样本的类别标签。
prob.y[i] 与 prob.x[i][]构成了一个完整的样本,一个保存类别标签,一个保存属性值。
private void read_problem() throws IOException { BufferedReader fp = new BufferedReader(new FileReader(input_file_name)); Vectorvy = new Vector (); Vector vx = new Vector (); int max_index = 0; while(true) { String line = fp.readLine(); if(line == null) break; StringTokenizer st = new StringTokenizer(line," \t\n\r\f:"); vy.addElement(atof(st.nextToken())); int m = st.countTokens()/2; svm_node[] x = new svm_node[m]; for(int j=0;j 0) max_index = Math.max(max_index, x[m-1].index); vx.addElement(x); } prob = new svm_problem(); prob.l = vy.size(); prob.x = new svm_node[prob.l][]; for(int i=0;i 0) param.gamma = 1.0/max_index; if(param.kernel_type == svm_parameter.PRECOMPUTED) for(int i=0;i max_index) { System.err.print("Wrong input format: sample_serial_number out of range\n"); System.exit(1); } } fp.close(); }
一般的分类步骤如下:
利用训练样本生成一个分类模型,然后利用该分类模型对待分类的样本进行分类
我需要的libsvm的使用步骤:
step 1:设置svm_parameter (注,gamma参数 的使用是在 readproblem函数里面使用的以及当kernel_type == PRECOMPUTED的时候也是在readproblem里面生效的,etc...)
step 2:将自己的样本存储到svm_problem对象中
step 3:利用svm_problem和svm_paramter通过svm.svm_train函数可以训练出一个svm_model
public static svm_model svm_train(svm_problem prob, svm_parameter param)
svm.java里面的参考代码 svm_model submodel = svm_train(subprob,param);
step 4:有了模型之后我们就可以进行分类了:
根据参数的不同调用的分类函数有所不同,但是主要有两个成员函数可以使用 svm_predict_probalility以及svm_predict 返回值就是分类的结果
public static double svm_predict_values(svm_model model, svm_node[] x, double[] dec_values)
public static double svm_predict(svm_model model, svm_node[] x)
参考代码如下
if(param.probability==1 && (param.svm_type == svm_parameter.C_SVC || param.svm_type == svm_parameter.NU_SVC)) { double[] prob_estimates= new double[svm_get_nr_class(submodel)]; for(j=begin;j
svm.java 源码里的开放接口 从字面意义上可以看出点门路...应该有相关文档吧 没去找哈- -以后有需要再去研究
public static svm_model svm_train(svm_problem prob, svm_parameter param)
public static void svm_cross_validation(svm_problem prob, svm_parameter param, int nr_fold, double[] target)
public static int svm_get_svm_type(svm_model model)
public static int svm_get_nr_class(svm_model model)
public static void svm_get_labels(svm_model model, int[] label)
public static void svm_get_sv_indices(svm_model model, int[] indices)
public static int svm_get_nr_sv(svm_model model)
public static double svm_get_svr_probability(svm_model model)
public static double svm_predict_values(svm_model model, svm_node[] x, double[] dec_values)
public static double svm_predict(svm_model model, svm_node[] x)
public static double svm_predict_probability(svm_model model, svm_node[] x, double[] prob_estimates)
public static void svm_save_model(String model_file_name, svm_model model) throws IOException
public static svm_model svm_load_model(String model_file_name) throws IOException
public static svm_model svm_load_model(BufferedReader fp) throws IOException
public static String svm_check_parameter(svm_problem prob, svm_parameter param)
public static int svm_check_probability_model(svm_model model)
public static void svm_set_print_string_function(svm_print_interface print_func)