如何在Matlab中使用单类支持向量机?

4
我将一张图片的一些像素标记为前景,其余的像素未被标记。我想使用支持向量机 (SVM),并将标记过的像素属性(如颜色)作为 SVM 的输入,以将剩余的像素标记为背景或前景。使用单类作为输入是否可行?还是需要一些像素标记为背景(双类输入)?
提前感谢。
编辑:我发现了http://scikit-learn.org/stable/auto_examples/svm/plot_oneclass.htmlhttp://www.csie.ntu.edu.tw/~cjlin/libsvm/这两个网站上介绍了单类 SVM,但我不知道如何在 Matlab 中使用它们。

你有没有标记为背景的像素? - marcman
嗨,不,我没有,我只有关于前台的信息。 - Majid Ramzani
LIBSVM在该软件包中具有MATLAB接口。 - marcman
非常感谢,但是你能帮我学习如何使用它吗? - Majid Ramzani
1
他们在压缩包中有一个自述文件。里面的解释相当清楚明了。 - marcman
2个回答

8

在Matlab中设置LIBSVM的步骤在官方软件包中的README文件中有描述,可以在这里下载。

在安装了适用于您的Matlab版本的LIBSVM之后,您可以使用以下方式训练SVM模型:

matlab> model = svmtrain(training_label_vector, training_instance_matrix [, 'libsvm_options']);

解释(摘自README

 -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.

培训选项包括:

-s svm_type : set type of SVM (default 0)
    0 -- C-SVC      (multi-class classification)
    1 -- nu-SVC     (multi-class classification)
    2 -- one-class SVM
    3 -- epsilon-SVR    (regression)
    4 -- nu-SVR     (regression)
-t kernel_type : set type of kernel function (default 2)
    0 -- linear: u'*v
    1 -- polynomial: (gamma*u'*v + coef0)^degree
    2 -- radial basis function: exp(-gamma*|u-v|^2)
    3 -- sigmoid: tanh(gamma*u'*v + coef0)
    4 -- precomputed kernel (kernel values in training_set_file)
-d degree : set degree in kernel function (default 3)
-g gamma : set gamma in kernel function (default 1/num_features)
-r coef0 : set coef0 in kernel function (default 0)
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB (default 100)
-e epsilon : set tolerance of termination criterion (default 0.001)
-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
-v n : n-fold cross validation mode
-q : quiet mode (no outputs)

如果您想训练一个One-Class-SVM(例如用于异常检测),则必须选择-s 2作为选项。
此外,参数nu在调整已训练的SVM时可能也很有趣,以及适当的内核参数用于所选内核类型(例如通过网格搜索)。
要通过LIBSVM训练One-Class-SVM,您应该只提供属于少数派类别的数据。
尽管如此,对于您的问题类型(因为您不会做某种形式的异常检测,特征/样本也不稀少),您应该选择普通的两类SVM。

我正在寻找设置SVM类型的“-s 2”,谢谢。 - Majid Ramzani

0
用Libsvm解决这个问题的代码很简单:
%train
[heart_scale_label, heart_scale_inst] = libsvmread('heart3');
model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07  -s 2');
%predict
[heart_scale_label2, heart_scale_inst2] = libsvmread('heart4');
[predict_label, accuracy, dec_values] = svmpredict(heart_scale_label2,
heart_scale_inst2, model); % test the training data

嗨,你明白如何将“heart_scale_label2”作为“predict”的参数吗?我没有标签,因为我需要进行预测! - Leo91

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接