【问题标题】:LibSvm add features using the JAVA apiLibSvm 使用 JAVA api 添加功能
【发布时间】:2014-04-24 05:31:12
【问题描述】:

我有一个文本,我想通过使用 java API 添加功能来进行训练。查看示例,构建训练集的主要类是 svm_problem。看起来 svm_node 代表一个 featureindexfeaturevalue strong> 是特征的权重)。

我所做的是有一个地图(只是为了简化问题),它保持特征和索引之间的关联。对于我的每个体重>示例,我都会创建一个新节点:

  svm_node currentNode = new svm_node();
  int index = feature.getIndexInMap();
  double value = feature.getWeight();
  currentNode.index = index;
  currentNode.value = value;

我的直觉正确吗? svm_problem.y 指的是什么?它是指标签的索引吗? svm_problem.l 只是两个向量的长度吗?

【问题讨论】:

  • 我建议更改标题。在我看来,它并不代表关于 libsvm 使用的真正问题,并且与是否是文本特征这一事实几乎没有关系。

标签: machine-learning svm libsvm


【解决方案1】:

您的直觉非常接近,但 svm_node 是一种模式而不是一种特征。变量 svm_problem.y 是一个包含每个模式标签的数组,svm_problem.l 是训练集的大小。

另外,请注意 svm_parameter.nr_weight 是每个标签的权重(如果您有一个不平衡的训练集很有用),但如果您不打算使用它,则必须将该值设置为零。

让我给你看一个 C++ 的简单例子:

#include "svm.h"
#include <iostream>

using namespace std;

int main()
{
    svm_parameter params;


    params.svm_type = C_SVC;
    params.kernel_type = RBF;
    params.C = 1;
    params.gamma = 1;
    params.nr_weight = 0;
    params.p= 0.0001;

    svm_problem problem;
    problem.l = 4;
    problem.y = new double[4]{1,-1,-1,1};
    problem.x = new svm_node*[4];

    {
    problem.x[0] = new svm_node[3];
    problem.x[0][0].index = 1;
    problem.x[0][0].value = 0;
    problem.x[0][1].index = 2;
    problem.x[0][1].value = 0;
    problem.x[0][2].index = -1;

    }

    {
    problem.x[1] = new svm_node[3];
    problem.x[1][0].index = 1;
    problem.x[1][0].value = 1;
    problem.x[1][1].index = 2;
    problem.x[1][1].value = 0;
    problem.x[1][2].index = -1;
    }

    {
    problem.x[2] = new svm_node[3];
    problem.x[2][0].index = 1;
    problem.x[2][0].value = 0;
    problem.x[2][1].index = 2;
    problem.x[2][1].value = 1;
    problem.x[2][2].index = -1;
    }

   {
    problem.x[3] = new svm_node[3];
    problem.x[3][0].index = 1;
    problem.x[3][0].value = 1;
    problem.x[3][1].index = 2;
    problem.x[3][1].value = 1;
    problem.x[3][2].index = -1;

    }

    for(int i=0; i<4; i++)
    {
        cout << problem.y[i] << endl;
    }

    svm_model * model = svm_train(&problem, &params);
    svm_save_model("mymodel.svm", model);

    for(int i=0; i<4; i++)
    {
        double d = svm_predict(model, problem.x[i]);

        cout << "Prediction " << d << endl;
    }
    /* We should free the memory at this point. 
       But this example is large enough already */ 
}

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多