【问题标题】:SVM Predict reading datatest支持向量机预测读取数据测试
【发布时间】:2016-04-06 18:24:02
【问题描述】:

我在实现svm_predict 函数时遇到了这么大的问题。我已经训练了 svm,并准备了数据测试。这两个文件都在.txtfile.Datatest 来自 LBP(本地二进制模式),它看起来像:

-0.6448744548418511

-0.7862774302452588

1.7746263060948377


我正在将它加载到svm_predict 函数中,在我的程序编译后在我的控制台上有:

准确度 = 0.0% (0/800)(分类)

所以看起来它无法读取数据测试?

import libsvm.*;
import java.io.*;
import java.util.*;

class svm_predict {
    private static double atof(String s)
    {
        return Double.valueOf(s).doubleValue();
    }

    private static int atoi(String s)
    {
        return Integer.parseInt(s);
    }

    private static void predict(BufferedReader input, DataOutputStream output, svm_model model, int predict_probability) throws IOException
    {
        int correct = 0;
        int total = 0;
        double error = 0;
        double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;

        int svm_type=svm.svm_get_svm_type(model);
        int nr_class=svm.svm_get_nr_class(model);
        double[] prob_estimates=null;

        if(predict_probability == 1)
        {
            if(svm_type == svm_parameter.EPSILON_SVR ||
               svm_type == svm_parameter.NU_SVR)
            {
                System.out.print("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="+svm.svm_get_svr_probability(model)+"\n");
            }
            else
            {
                int[] labels=new int[nr_class];
                svm.svm_get_labels(model,labels);
                prob_estimates = new double[nr_class];
                output.writeBytes("labels");
                for(int j=0;j<nr_class;j++)
                    output.writeBytes(" "+labels[j]);
                output.writeBytes("\n");
            }
        }
        while(true)
        {
            String line = input.readLine();
            if(line == null) break;

            StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");

            double target = atof(st.nextToken());
            int m = st.countTokens()/2;
            svm_node[] x = new svm_node[m];
            for(int j=0;j<m;j++)
            {
                x[j] = new svm_node();
                x[j].index = atoi(st.nextToken());
                x[j].value = atof(st.nextToken());
            }

            double v;
            if (predict_probability==1 && (svm_type==svm_parameter.C_SVC || svm_type==svm_parameter.NU_SVC))
            {
                v = svm.svm_predict_probability(model,x,prob_estimates);
                output.writeBytes(v+" ");
                for(int j=0;j<nr_class;j++)
                    output.writeBytes(prob_estimates[j]+" ");
                output.writeBytes("\n");
            }
            else
            {
                v = svm.svm_predict(model,x);
                output.writeBytes(v+"\n");
            }

            if(v == target)
                ++correct;
            error += (v-target)*(v-target);
            sumv += v;
            sumy += target;
            sumvv += v*v;
            sumyy += target*target;
            sumvy += v*target;
            ++total;
        }
        if(svm_type == svm_parameter.EPSILON_SVR ||
           svm_type == svm_parameter.NU_SVR)
        {
            System.out.print("Mean squared error = "+error/total+" (regression)\n");
            System.out.print("Squared correlation coefficient = "+
                 ((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
                 ((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))+
                 " (regression)\n");
        }
        else
            System.out.print("Accuracy = "+(double)correct/total*100+
                 "% ("+correct+"/"+total+") (classification)\n");
    }

    private static void exit_with_help()
    {
        System.err.print("usage: svm_predict [options] test_file model_file output_file\n"
        +"options:\n"
        +"-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n");
        System.exit(1);
    }

    public static void main(String argv[]) throws IOException
    {
        int i, predict_probability=0;

        // parse options
        for(i=0;i<argv.length;i++)
        {
            if(argv[i].charAt(0) != '-') break;
            ++i;
            switch(argv[i-1].charAt(1))
            {
                case 'b':
                    predict_probability = atoi(argv[i]);
                    break;
                default:
                    System.err.print("Unknown option: " + argv[i-1] + "\n");
                    exit_with_help();
            }
        }
        if(i>=argv.length-2)
            exit_with_help();
        try 
        {
            BufferedReader input = new BufferedReader(new FileReader(argv[i]));
            DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(argv[i+2])));
            svm_model model = svm.svm_load_model(argv[i+1]);
            if(predict_probability == 1)
            {
                if(svm.svm_check_probability_model(model)==0)
                {
                    System.err.print("Model does not support probabiliy estimates\n");
                    System.exit(1);
                }
            }
            else
            {
                if(svm.svm_check_probability_model(model)!=0)
                {
                    System.out.print("Model supports probability estimates, but disabled in prediction.\n");
                }
            }
            predict(input,output,model,predict_probability);
            input.close();
            output.close();
        } 
        catch(FileNotFoundException e) 
        {
            exit_with_help();
        }
        catch(ArrayIndexOutOfBoundsException e) 
        {
            exit_with_help();
        }
    }
}

【问题讨论】:

  • 这与这些家伙提供的免费 libsvm 库有关吗? Chih-Wei Hsu、Chih-Chung Chang 和 Chih-Jen Lin ?
  • 是的,它是 libSVM 的免费实现,仍然有问题,因为我无法获得任何预测结果
  • 欢迎来到 Stack Overflow(俗称 SO)!与我看到的许多帖子相比,这是一篇不错的第一篇文章。我确实在问题正文中格式化了函数名称,因为它们是代码。此外,在您将“...”作为中断的地方,我将标记更改为“---------”,这将在帖子中显示为一行,如您所见。我做的最后一件事是格式化文件名和扩展名。我无法弄清楚的一件事是,如果您有负数或要点。编码愉快!

标签: java svm


【解决方案1】:

很难知道,因为这是一个很大的过程

确保您遵循他们的分类指南

数据应该被缩放,它现在似乎超过 1

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 2019-01-23
    • 1970-01-01
    • 2019-03-08
    • 2018-07-31
    • 2017-08-08
    • 1970-01-01
    • 2017-08-28
    • 2017-05-01
    相关资源
    最近更新 更多