【问题标题】:LIBSVM data preparationLIBSVM 数据准备
【发布时间】:2014-01-17 23:26:02
【问题描述】:

我正在 Matlab 中做一个关于图像处理的项目,并希望实施 LIBSVM 以进行监督学习。

我在准备数据时遇到了问题。 我有 CSV 格式的数据,当我尝试使用 LIBSVM 常见问题解答中提供的信息将其转换为 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。

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

[标签] [索引 1]:[值 1] [索引 2]:[值 2]......

[标签] [索引 1]:[值 1] [索引 2]:[值 2]......

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

提前感谢您, 西德拉

【问题讨论】:

  • 对于 libsvm,您不需要将数据写入文件。将它们直接传递给 svmtrain 和 svmpredict。
  • @ A. Donda:我正在使用 LIBSVM(支持向量机库)csie.ntu.edu.tw/~cjlin/libsvm。为此,我需要将数据写入文件。请指教。我也会尝试使用 svmtrain 和 svmpredict,谢谢。
  • 我知道那个库,我自己使用它。如果使用 Matlab 接口,则无需将数据写入文件:csie.ntu.edu.tw/~cjlin/libsvm/#matlab> 标准下载包中应包含相应的 mex 文件。
  • @A.Donda :函数“libsvmwrite”将数据写入文件对吗? libsvmwrite('filename', label_vector, instance_matrix) 。其中 'filename' 是要写入数据的文件的名称。例如,“文件名”是“heart.train”,即使在库中也给出了示例。
  • @A.Donda :如果不使用 libsvmwrite,我还能如何进行数据准备?取悦指南。

标签: matlab libsvm


【解决方案1】:

正如@A.Donda 回答的那样,如果您可以在 matlab 中进行训练和预测,则不必将数据传输为“libsvm”格式。

当您想在 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);

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

【讨论】:

    【解决方案2】:

    您不必将数据写入文件,而是可以使用 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 中的函数不兼容 - 请确保调用正确的函数。

    【讨论】:

    • 我的值超过了 (256*256)*434。所以我不可能写出'training_instance_matrix'。我在 CSV(逗号分隔值)文件中有数据,我需要使用 "libsvmwrite" 将其转换为 libsvm 格式,但由于某种原因,我没有得到所需的格式。
    • 我有 CSV 格式的数据,然后我使用:matlab> SPECTF = csvread('SPECTF.train'); % 读取一个 csv 文件 matlab> labels = SPECTF(:, 1);来自第一列的 % 标签 matlab> features = SPECTF(:, 2:end); matlab> features_sparse = 稀疏(特征); % 特征必须在稀疏矩阵中 matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);
    • 我发现了我的错误。我没有将标签写入 CSV 文件,因此它将第一个值作为标签!有关如何在 matlab 中将标签和数据写入 CSV 文件的任何建议?
    • 据我所知,svmtrain 的参数没有大小限制,当然可用内存除外。 “(256*256)*434”是什么意思?
    猜你喜欢
    • 2017-03-19
    • 2017-05-15
    • 2014-11-15
    • 2011-04-10
    • 2019-01-02
    • 1970-01-01
    • 2013-07-25
    • 2018-03-29
    • 2013-08-23
    相关资源
    最近更新 更多