SVM matlab

关于SVM参数的优化选取,国际上并没有公认统一的最好的方法,现在目前常用的方法就是让c和g在一定的范围内取值,对于取定的c和g对于把训练集作为原始数据集利用K-CV方法得到在此组c和g下训练集验证分类准确率,最终取使得训练集验证分类准确率最高的那组c和g做为最佳的参数,但有一个问题就是可能会有多组的c和g对应于最高的验证分类准确率,这种情况怎么处理?这里采用的手段是选取能够达到最高验证分类准确率中参数c最小的那组c和g做为最佳的参数,如果对应最小的c有多组g,就选取搜索到的第一组c和g做为最佳的参数。这样做的理由是:过高的c会导致过学习状态发生,即训练集分类准确率很高而测试集分类准确率很低(分类器的泛化能力降低),所以在能够达到最高验证分类准确率中的所有的成对的c和g中认为较小的惩罚参数c是更佳的选择对象。

  1. 以上的寻参思想在libsvm-mat-2.89-3[FarutoUltimate3.0]工具箱中已经实现SVMcgForClass.m (分类问题寻优)、SVMcgForRegress.m (回归问题参数寻优):
  2. [bestCVaccuracy,bestc,bestg]=SVMcgForClass(train_label,train,cmin,cmax,gmin,gmax,v,cstep,gstep,accstep)
    输入:
    train_label:训练集的标签,格式要求与svmtrain相同。
    train:训练集,格式要求与svmtrain相同。
    cmin,cmax:惩罚参数c的变化范围,即在[2cmin,2cmax]范围内寻找最佳的参数c,默认值为cmin=-8,cmax=8,即默认惩罚参数c的范围是[2(-8),28]。
    gmin,gmax:RBF核参数g的变化范围,即在[2gmin,2gmax]范围内寻找最佳的RBF核参数g,默认值为gmin=-8,gmax=8,即默认RBF核参数g的范围是[2(-8),28]。
    v:进行Cross Validation过程中的参数,即对训练集进行v-fold Cross Validation,默认为3,即默认进行3折CV过程。
    cstep,gstep:进行参数寻优是c和g的步进大小,即c的取值为2cmin,2(cmin+cstep),…,2cmax,,g的取值为2gmin,2(gmin+gstep),…,2gmax,默认取值为cstep=1,gstep=1。
    accstep:最后参数选择结果图中准确率离散化显示的步进间隔大小([0,100]之间的一个数),默认为4.5。
    输出:
    bestCVaccuracy:最终CV意义下的最佳分类准确率。
    bestc:最佳的参数c。
    bestg:最佳的参数g。
  3. 网格参数寻优函数(回归问题):SVMcgForRegress[bestCVmse,bestc,bestg]= SVMcgForRegress(train_label,train,cmin,cmax,gmin,gmax,v,cstep,gstep,msestep)
    其输入输出与SVMcgForClass类似,这里不再赘述。

%% IV. SVM模型创建/训练
%%
% %首先进行粗略选择:
% [bestmse,bestc,bestg] = SVMcgForRegress(tn_train,pn_train,-8,8,-8,8);
%
% % 打印粗略选择结果
% disp(‘打印粗略选择结果’);
% str = sprintf( ‘Best Cross Validation MSE = %g Best c = %g Best g = %g’,bestmse,bestc,bestg);
% disp(str);
%
% % 根据粗略选择的结果图再进行精细选择:
% [bestmse,bestc,bestg] = SVMcgForRegress(tn_train,pn_train,-4,4,-4,4,3,0.5,0.5,0.05);
%
% % 打印精细选择结果
% disp(‘打印精细选择结果’);
% str = sprintf( ‘Best Cross Validation MSE = %g Best c = %g Best g = %g’,bestmse,bestc,bestg);
% disp(str);

%%
% 2. 创建/训练SVM
cmd= [’ -t 2’,’ -c 1.4142’,’ -g 0.0625’,’ -s 3 -p 0.01’];
%cmd = [’ -t 2’,’ -c ‘,num2str(bestc),’ -g ‘,num2str(bestg),’ -s 3 -p 0.01’];
model = svmtrain(tn_train,pn_train,cmd);

%% V. SVM仿真预测
[Predict_1,error_1,dec_values] = svmpredict(tn_train,pn_train,model);
[Predict_2,error_2,dec_values] = svmpredict(tn_test,pn_test,model);

%%
% 1. 反归一化
predict_1 = mapminmax(‘reverse’,Predict_1,outputps);
predict_2 = mapminmax(‘reverse’,Predict_2,outputps);

转载出处:http://www.matlabsky.com/thread-12411-1-1.html

need to conduct installation. If you have modified the sources and would like to re-build the package, type 'mex -setup' in MATLAB to choose a compiler for mex first. Then type 'make' to start the installation. Starting from MATLAB 7.1 (R14SP3), the default MEX file extension is changed from .dll to .mexw32 or .mexw64 (depends on 32-bit or 64-bit Windows). If your MATLAB is older than 7.1, you have to build these files yourself. Example: matlab> mex -setup (ps: MATLAB will show the following messages to setup default compiler.) Please choose your compiler for building external interface (MEX) files: Would you like mex to locate installed compilers [y]/n? y Select a compiler: [1] Microsoft Visual C/C++ version 7.1 in C:\Program Files\Microsoft Visual Studio [0] None Compiler: 1 Please verify your choices: Compiler: Microsoft Visual C/C++ 7.1 Location: C:\Program Files\Microsoft Visual Studio Are these correct?([y]/n): y matlab> make Under 64-bit Windows, Visual Studio 2005 user will need "X64 Compiler and Tools". The package won't be installed by default, but you can find it in customized installation options. For list of supported/compatible compilers for MATLAB, please check the following page: http://www.mathworks.com/support/compilers/current_release/ Usage ===== matlab> model = svmtrain(training_label_vector, training_instance_matrix [, 'libsvm_options']); -training_label_vector: An m by 1 vector of training labels (type must be double). -training_instance_matrix: An m by n matrix of m training instances with n features. It can be dense or sparse (type must be double). -libsvm_options: A string of training options in the same format as that of LIBSVM. matlab> [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model [, 'libsvm_options']); -testing_label_vector: An m by 1 vector of prediction labels. If labels of test data are unknown, simply use any random values. (type must be double) -testing_instance_matrix: An m by n matrix of m testing instances with n features. It can be dense or sparse. (type must be double) -model: The output of svmtrain. -libsvm_options: A string of testing options in the same format as that of LIBSVM. Returned Model Structure ======================== The 'svmtrain' function returns a model which can be used for future prediction. It is a structure and is organized as [Parameters, nr_class, totalSV, rho, Label, ProbA, ProbB, nSV, sv_coef, SVs]: -Parameters: parameters -nr_class: number of classes; = 2 for regression/one-class svm -totalSV: total #SV -rho: -b of the decision function(s) wx+b -Label: label of each class; empty for regression/one-class SVM -ProbA: pairwise probability information; empty if -b 0 or in one-class SVM -ProbB: pairwise probability information; empty if -b 0 or in one-class SVM -nSV: number of SVs for each class; empty for regression/one-class SVM -sv_coef: coefficients for SVs in decision functions -SVs: support vectors If you do not use the option '-b 1', ProbA and ProbB are empty matrices. If the '-v' option is specified, cross validation is conducted and the returned model is just a scalar: cross-validation accuracy for classification and mean-squared error for regression. More details about this model can be found in LIBSVM FAQ (http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html) and LIBSVM implementation document (http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf). Result of Prediction ==================== The function 'svmpredict' has three outputs. The first one, predictd_label, is a vector of predicted labels. The second output, accuracy, is a vector including accuracy (for classification), mean squared error, and squared correlation coefficient (for regression). The third is a matrix containing decision values or probability estimates (if '-b 1' is specified). If k is the number of classes, for decision values, each row includes results of predicting k(k-1)/2 binary-class SVMs. For probabilities, each row contains k values indicating the probability that the testing instance is in each class. Note that the order of classes here is the same as 'Label' field in the model structure. Examples ======== Train and test on the provided data heart_scale: matlab> load heart_scale.mat matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07'); matlab> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model); % test the training data For probability estimates, you need '-b 1' for training and testing: matlab> load heart_scale.mat matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07 -b 1'); matlab> load heart_scale.mat matlab> [predict_label, accuracy, prob_estimates] = svmpredict(heart_scale_label, heart_scale_inst, model, '-b 1'); To use precomputed kernel, you must include sample serial number as the first column of the training and testing data (assume your kernel matrix is K, # of instances is n): matlab> K1 = [(1:n)', K]; % include sample serial number as first column matlab> model = svmtrain(label_vector, K1, '-t 4'); matlab> [predict_label, accuracy, dec_values] = svmpredict(label_vector, K1, model); % test the training data We give the following detailed example by splitting heart_scale into 150 training and 120 testing data. Constructing a linear kernel matrix and then using the precomputed kernel gives exactly the same testing error as using the LIBSVM built-in linear kernel. matlab> load heart_scale.mat matlab> matlab> % Split Data matlab> train_data = heart_scale_inst(1:150,:); matlab> train_label = heart_scale_label(1:150,:); matlab> test_data = heart_scale_inst(151:270,:); matlab> test_label = heart_scale_label(151:270,:); matlab> matlab> % Linear Kernel matlab> model_linear = svmtrain(train_label, train_data, '-t 0'); matlab> [predict_label_L, accuracy_L, dec_values_L] = svmpredict(test_label, test_data, model_linear); matlab> matlab> % Precomputed Kernel matlab> model_precomputed = svmtrain(train_label, [(1:150)', train_data*train_data'], '-t 4'); matlab> [predict_label_P, accuracy_P, dec_values_P] = svmpredict(test_label, [(1:120)', test_data*train_data'], model_precomputed); matlab> matlab> accuracy_L % Display the accuracy using linear kernel matlab> accuracy_P % Display the accuracy using precomputed kernel Note that for testing, you can put anything in the testing_label_vector. For more details of precomputed kernels, please read the section ``Precomputed Kernels'' in the README of the LIBSVM package. Other Utilities =============== A matlab function libsvmread reads files in LIBSVM format: [label_vector, instance_matrix] = libsvmread('data.txt'); Two outputs are labels and instances, which can then be used as inputs of svmtrain or svmpredict. A matlab function libsvmwrite writes Matlab matrix to a file in LIBSVM format: libsvmwrite('data.txt', label_vector, instance_matrix] The instance_matrix must be a sparse matrix. (type must be double) These codes are prepared by Rong-En Fan and Kai-Wei Chang from National Taiwan University. Additional Information ====================== This interface was initially written by Jun-Cheng Chen, Kuan-Jen Peng, Chih-Yuan Yang and Chih-Huai Cheng from Department of Computer Science, National Taiwan University. The current version was prepared by Rong-En Fan and Ting-Fan Wu. If you find this tool useful, please cite LIBSVM as follows Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support vector machines, 2001. Software available at http://www.csie.ntu.edu.tw/~cjlin/libsvm For any question, please contact Chih-Jen Lin , or check the FAQ page: http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#/Q9:_MATLAB_interface
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值