LIBSVM is the famous SVM open source component, currently has java.c/c++,.net and so on several versions, I use is 2.9
The main use class under the LIBSVM namespace:
Svm_model is a model class, which is obtained by training or loading a well-trained model file.
Svm_parameter is a parameter class, mainly for support vector machine set parameters, the specific parameters are as follows:
Svm_parameter.svm_type
SVM Type: The SVM setting type (default Svm_parameter. C_SVC)
Svm_parameter. The C_svc-c-svc N (n>=2) classifier allows for incomplete classification with the exception value penalty factor C.
Svm_parameter. Nu_svc--ν-svc N A classifier that is similar to the incomplete classification. The parameter Nu replaces C, whose value is in the interval "0,1", the greater the NU, the smoother the decision boundary.
Svm_parameter. one_class– a class of SVM single classifier, all the training data extracted from the same class, and then SVM established a dividing line to divide the class in the feature space occupied by the region and other classes in the feature space occupied area.
Svm_parameter. Epsilon_svr--ε-svr return. The distance between the eigenvector of the training set and the fitted super-plane needs to be less than p. The exception value penalty factor C is adopted.
Svm_parameter. Nu_svr--ν-svr return; Nu instead of P
Svm_parameter.kernel_type
Kernel function Type: kernel function set type (svm_parameter. LINEAR)
Svm_parameter. linear– linearity: U ' XV-no mapping to high dimensional space, linear differentiation (or regression) is done in the original feature space, which is the quickest choice. D (x, y) = X?y = = (x, y)
Svm_parameter. poly– polynomial: (Γxu ' XV + coef0) ^degree-polynomial core: D (x, y) = (gamma* (x?y) +coef0) degree
Svm_parameter. RBF–RBF function: exp (-γx|u-v|^2)-Radial base, is a good choice for most cases: D (x, y) = exp (-gamma*|x-y|2)
Svm_parameter. Sigmoid–sigmoid:tanh (Γxu ' XV + COEF0)-The SIGMOID function is used as a kernel function: D (x, y) = Tanh (gamma* (x?y) +COEF0)
degree, gamma, COEF0: are all parameters of the kernel function, see the equation of the kernel function above.
Svm_parameter.degree
degree settings in kernel functions (default 3)
Svm_parameter.coef0
COEF0 settings in kernel functions (default 0)
Svm_parameter.shrinking
Whether to use heuristics, 0 or 1 (default 1)
Svm_parameter.nu
Set parameters for Ν-svc, a class of SVM and ν-SVR (default 0.5)
Svm_parameter. C
Set parameters for C-svc,ε-svr and ν-svr (default 1)
Svm_parameter.cache_size
Set the cache memory size in megabytes (default 40)
Svm_problem is equivalent to a training set, which can be used to teach data to be added to the trainer.
Svm_node internal data structure used to save the data to be trained
We generally use the following methods in the SVM Keynote program:
Svm.svm_train (Svm_problem,svm_parameter) This method returns a well-trained Svm_model
Svm.svm_load_model (file name); This method returns a well-trained Svm_model
Svm.svm_save_model (filename, svm_model); This method saves Svm_model to a file
Svm.svm_predict_values (svm_model,svm_node,double); The method returns the Doule class value, Svm_node the Svm_model test, and the return value determines the location of the Svm_node in the model.
=================================================================================================
LIBSVM the call of the library function
The library function is in the "LIBSVM" package.
In the Java version, the following functions can be called:
public class SVM {
public static final int libsvm_version=310;
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 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 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);
}
Note The index of the last element of svm_node[] in the Java version is not-1.
Users can customize their own output format by:
Your_print_func = new Svm_print_interface ()
{
public void print (String s)
{
Your own format
}
};
Svm.svm_set_print_string_function (Your_print_func);
====================================================================================================
The general steps used by LIBSVM are:
1) Prepare the data set in the format required by the LIBSVM package;
2) Simple scaling operation of the data;
3) Consider choosing the RBF kernel function
4) using cross-validation to select the best parameters C and γ;
5) Use the best parameters C and gamma to train the whole training set to obtain support vector machine model, 6) use the acquired model to test and forecast.
Svm_type c_svc% The type of SVM used in training, here is csvc−
Kernel_type RBF% Training uses the kernel function type, here is the RBF kernel
Gamma 0.047619 is the same as gamma in the Operation parameter settings
Nr_class 2 categories at the time of classification, here is the two classification problem
TOTAL_SV 604 Total number of support vectors
rho-0.337784% constant entry in the decision function B
Label 0 1 category label
NR_SV 314 290 The number of support vectors corresponding to each type of label
SV% below support vector
1 1:-0.963808 2:0.906788 ... 19:-0.197706 20:-0.928853 21:-1 1 1:-0.885128 2:0.768219 ... 19:-0.452573 20:-0.980591 21:-1 .....
1 1:-0.847359 2:0.485921 ... 19:-0.541457 20:-0.989077 21:-1
% for the classification problem, the above support vector column meaning is the same as the training data set, for the regression problem, slightly different from the training data in the label labels (that is, the Y value) corresponding to the location of the model file in the support vector is now stored in the Lagrange coefficient α value
LIBSVM Java Invocation Description