LIBSVM数据准备

时间:2013-12-31 01:28:53

标签: matlab libsvm

我正在Matlab中进行关于图像处理的项目,并希望实现LIBSVM用于监督学习。

我在数据准备方面遇到了问题。 我有CSV格式的数据,当我尝试使用LIBSVM faq中提供的信息将其转换为libsvm格式时: -

    matlab> SPECTF = csvread('SPECTF.train'); % read a csv file
    matlab> labels = SPECTF(:, 1); % labels from the 1st column
    matlab> features = SPECTF(:, 2:end); 
    matlab> features_sparse = sparse(features); % features must be in a sparse matrix
    matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);

我以下列形式获取数据:

3.0012 1:2.1122 2:0.9088 ...... [值1] [索引1]:[值2] [索引2]:[值3]

这是第一个没有索引的值,索引1后面的值是值2.

根据我的阅读,数据应采用以下格式:

[label] [index 1]:[value 1] [index 2]:[value 2] ......

[label] [index 1]:[value 1] [index 2]:[value 2] ......

我需要帮助才能做到这一点。 如果有人能给我一些关于如何给标签的线索,那将非常有用。

提前感谢你, 锡德拉

2 个答案:

答案 0 :(得分:1)

您不必将数据写入文件,而是可以将Matlab接口用于LIBSVM。该界面由两个函数svmtrainsvmpredict组成。如果不带参数调用,每个函数都会打印一个帮助文本:

Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');                                                                          
libsvm_options:                                                                                                                                                      
-s svm_type : set type of SVM (default 0)                                                                                                                            
        0 -- C-SVC                                                                                                                                                   
        1 -- nu-SVC                                                                                                                                                  
        2 -- one-class SVM                                                                                                                                           
        3 -- epsilon-SVR                                                                                                                                             
        4 -- nu-SVR                                                                                                                                                  
-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_instance_matrix)
-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)

Usage: [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
Parameters:
  model: SVM model structure from svmtrain.
  libsvm_options:
    -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
Returns:
  predicted_label: SVM prediction output vector.
  accuracy: a vector with accuracy, mean squared error, squared correlation coefficient.
  prob_estimates: If selected, probability estimate vector.

在具有三个特征的四个点的数据集上训练线性SVM的示例代码:

training_label_vector = [1 ; 1 ; -1 ; -1];
training_instance_matrix = [1 2 3 ; 3 4 5 ; 5 6 7; 7 8 9];
model = svmtrain(training_label_vector, training_instance_matrix, '-t 0');

将生成的model应用于测试数据

testing_instance_matrix = [9 5 1; 2 9 5];
predicted_label = svmpredict(nan(2, 1), testing_instance_matrix, model)

结果

predicted_label =

    -1
    -1

您也可以将真testing_label_vector传递给svmpredict,以便直接计算准确度;我在这里用NaN替换了真正的标签。


请注意在Matlab的 Statistics Toolbox 中还有一个函数svmtrain,它与LIBSVM中的函数不兼容 - 请确保您拨打正确的电话。

答案 1 :(得分:0)

当@ A.Donda回答时,您不必将数据传输到&#lib; vm'格式,如果你可以在matlab中进行训练和预测。

当您想要在Windows或Linux中进行培训和预测工作时,您必须在' libsvm'中创建数据。格式。

从你的错误来看,我认为你没有在每一行的数据功能中给出标签。您应该在数据的每一行中的要素前面添加标签。

matlab> SPECTF = csvread('SPECTF.train'); % read a csv file
matlab> features = SPECTF(:, :); % because there are no labels in your csv file
matlab> labels = [??];% to add the label as your plan 
matlab> features_sparse = sparse(features); % features must be in a sparse  matrix
matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);

您应该提供有关数据的更多信息,以便我们为您提供帮助。顺便说一句,标签数据通常由用户在开始时设置。您可以根据需要将标签数据任意整数设置为一种数据。

相关问题