研究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;i++) { if(argv[i].charAt(0) != '-') break; if(++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<argv.length-1) model_file_name = argv[i+1]; else { int p = argv[i].lastIndexOf('/'); ++p; // whew... model_file_name = argv[i].substring(p)+".model"; } }
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)); Vector<Double> vy = new Vector<Double>(); Vector<svm_node[]> vx = new Vector<svm_node[]>(); 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<m;j++) { x[j] = new svm_node(); x[j].index = atoi(st.nextToken()); x[j].value = atof(st.nextToken()); } if(m>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<prob.l;i++) prob.x[i] = vx.elementAt(i); prob.y = new double[prob.l]; for(int i=0;i<prob.l;i++) prob.y[i] = vy.elementAt(i); if(param.gamma == 0 && max_index > 0) param.gamma = 1.0/max_index; if(param.kernel_type == svm_parameter.PRECOMPUTED) for(int i=0;i<prob.l;i++) { if (prob.x[i][0].index != 0) { System.err.print("Wrong kernel matrix: first column must be 0:sample_serial_number\n"); System.exit(1); } if ((int)prob.x[i][0].value <= 0 || (int)prob.x[i][0].value > 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<end;j++) target[perm[j]] = svm_predict_probability(submodel,prob.x[perm[j]],prob_estimates); } else for(j=begin;j<end;j++) target[perm[j]] = svm_predict(submodel,prob.x[perm[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)