读取 libsvm 的训练数据后崩溃

标签 c arrays parsing libsvm

我已经扩展了我的训练数据并尝试进行交叉验证以获得最佳参数,但我不知道该怎么做。我尝试读取缩放后的训练数据并将它们分配给 svm_problem 变量:

svm_node My_svm_node[16400][157];
svm_node temp[157];
FILE *fp =NULL;
fp = fopen("Scaled_Train_Data.txt","r");   //my data is in fp
for(int LineNumber = 0 ; stop !=1 ; LineNumber++)
{   
    //std::cout<<"Line Number "<<LineNumber<<" Is processed .. \n";
    if (readline(fp)==NULL)
    {
        stop = 1;
        break;
    }
    char *p=line;
    int next_index=1;
    int index = 0 ;
    double target;
    double value;

    sscanf(p,"%lf",&target);
    while(isspace(*p)) ++p;     //remove any spaces betweeen numbers ...
    while(!isspace(*p)) ++p;

    while(sscanf(p,"%d:%lf",&index,&value)==2)
    {
        for(i=next_index;i<index;i++)
        {
            temp[i-1].index = i;
            temp[i-1].value = 0;
        }
        temp[index-1].index = index;
        temp[index-1].value = value;
        while(*p!=':') ++p;                         //check to see if we obey the rule of libsvm
        ++p;                                        
        while(isspace(*p)) ++p;                     //remove any spaces between numbers 
        while(*p && !isspace(*p)) ++p;              
        next_index=index+1;
    }   
    temp[index].index = -1;
    temp[index].value = 0;
    x[LineNumber] = temp;
}

我可以向您保证,我能够成功读取数据,并且 temp 变量始终保存我的 scaled_train 数据的一个特征向量。

但是当我打电话

svm_cross_validation(&Test_Data,&param,7,target); 

我收到运行时访问冲突错误。

我填了

  • Test_data.l = 特征向量数量
  • Test_data.y = 功能标签
  • Test_Data.x = 特征值

我不知道这里出了什么问题。

这里也有一些奇怪的东西。当我尝试读取 svm_node 的值和索引时, 我总是得到 scaled_data 的最后一行,但无法看到整个数据。 (我猜问题就出在这里。)

for (int j = 0 ; j < 164000 ; j++)  //number of rows 
{
        for (int i = 0 ; i < 157 ; i++)   //maximum number of features 
            {
                    printf("The x[%d][%d] is %d   %lf",j,i,x[j][i].index,x[j][i].value); //I always get the last row for 16400 times !!!!!
                    getchar();
            }
}

最佳答案

如果您的训练数据采用 LIBSVM 格式(又名 svmlight 格式),最简单的解决方案是查看 LIBSVM 用于读取模型的例程:

void read_problem(const char *filename);

如 LIBSVM 包中的 svm-train.c 中所定义。

关于读取 libsvm 的训练数据后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16616111/

相关文章:

javascript - BugHunt - Javascript 解析错误?

c - 如果 '&' 没有放在 'scanf' 语句中会发生什么?

c - 结构体可以包含的成员数量或变量类型是否有大小限制?

java - 从 args 打印出一组整数,输出为 : The numbers are 1, 2, 3, 4, 5

c - 将数组传递给函数

Java LR 或 LL 解析

c - 这个线程上下文结构有什么作用?它的目的是什么?

c - 为什么 __thread 必须跟在 extern 或 static 之后

php - INNER JOIN SQL/PHP - 需要单行且列上有数组

java - 使用 Jsoup 解析 HTML,出现异常