【问题标题】:Libsvm java training testing example(also in real time)Libsvm java训练测试示例(也是实时的)
【发布时间】:2013-12-25 19:37:13
【问题描述】:

任何人都可以通过提供用于培训和测试的 libsvm java 示例来帮助我。我是机器学习的新手,需要相关帮助。 @machine learner 之前提供的示例仅给出一个类结果时出错。我不想使用 weka 作为之前帖子中给出的建议。

或者你能纠正这段代码中的错误吗?它总是预测一个类的结果。(我想执行多分类)。

这个例子是“机器学习者”给出的

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

public class Test{
    public static void main(String[] args) throws Exception{

        // Preparing the SVM param
        svm_parameter param=new svm_parameter();
        param.svm_type=svm_parameter.C_SVC;
        param.kernel_type=svm_parameter.RBF;
        param.gamma=0.5;
        param.nu=0.5;
        param.cache_size=20000;
        param.C=1;
        param.eps=0.001;
        param.p=0.1;

        HashMap<Integer, HashMap<Integer, Double>> featuresTraining=new HashMap<Integer, HashMap<Integer, Double>>();
        HashMap<Integer, Integer> labelTraining=new HashMap<Integer, Integer>();
        HashMap<Integer, HashMap<Integer, Double>> featuresTesting=new HashMap<Integer, HashMap<Integer, Double>>();

        HashSet<Integer> features=new HashSet<Integer>();

        //Read in training data
        BufferedReader reader=null;
        try{
            reader=new BufferedReader(new FileReader("a1a.train"));
            String line=null;
            int lineNum=0;
            while((line=reader.readLine())!=null){
                featuresTraining.put(lineNum, new HashMap<Integer,Double>());
                String[] tokens=line.split("\\s+");
                int label=Integer.parseInt(tokens[0]);
                labelTraining.put(lineNum, label);
                for(int i=1;i<tokens.length;i++){
                    String[] fields=tokens[i].split(":");
                    int featureId=Integer.parseInt(fields[0]);
                    double featureValue=Double.parseDouble(fields[1]);
                    features.add(featureId);
                    featuresTraining.get(lineNum).put(featureId, featureValue);
                }
            lineNum++;
            }

            reader.close();
        }catch (Exception e){

        }

        //Read in test data
        try{
            reader=new BufferedReader(new FileReader("a1a.t"));
            String line=null;
            int lineNum=0;
            while((line=reader.readLine())!=null){

                featuresTesting.put(lineNum, new HashMap<Integer,Double>());
                String[] tokens=line.split("\\s+");
                for(int i=1; i<tokens.length;i++){
                    String[] fields=tokens[i].split(":");
                    int featureId=Integer.parseInt(fields[0]);
                    double featureValue=Double.parseDouble(fields[1]);
                    featuresTesting.get(lineNum).put(featureId, featureValue);
                }
            lineNum++;
            }
            reader.close();
        }catch (Exception e){

        }

        //Train the SVM model
        svm_problem prob=new svm_problem();
        int numTrainingInstances=featuresTraining.keySet().size();
        prob.l=numTrainingInstances;
        prob.y=new double[prob.l];
        prob.x=new svm_node[prob.l][];

        for(int i=0;i<numTrainingInstances;i++){
            HashMap<Integer,Double> tmp=featuresTraining.get(i);
            prob.x[i]=new svm_node[tmp.keySet().size()];
            int indx=0;
            for(Integer id:tmp.keySet()){
                svm_node node=new svm_node();
                node.index=id;
                node.value=tmp.get(id);
                prob.x[i][indx]=node;
                indx++;
            }

            prob.y[i]=labelTraining.get(i);
        }

        svm_model model=svm.svm_train(prob,param);

        for(Integer testInstance:featuresTesting.keySet()){
            HashMap<Integer, Double> tmp=new HashMap<Integer, Double>();
            int numFeatures=tmp.keySet().size();
            svm_node[] x=new svm_node[numFeatures];
            int featureIndx=0;
            for(Integer feature:tmp.keySet()){
                x[featureIndx]=new svm_node();
                x[featureIndx].index=feature;
                x[featureIndx].value=tmp.get(feature);
                featureIndx++;
            }

            double d=svm.svm_predict(model, x);

            System.out.println(testInstance+"\t"+d);
        }

    }
}

【问题讨论】:

  • 我认为您需要提供一个简短的、独立的示例来说明您的问题 (sscce.org)。您将需要发布一个最小的 java,以及说明您的问题的 a1a.train 和 a1a.t。看起来你认为你做的一切都很完美,而 LIBSVM 只是坏了。我向你保证,情况并非如此。
  • 我知道 libsvm 没有任何问题。我使用它的方式是错误的。我在读取测试文件“a1a.t”的方式出现错误。如果你在上面的代码中纠正它对我会有帮助。
  • 现在我可以进行分类,但现在如何获得每个类的置信度值。(在 java 中)
  • 你能解决你的问题吗?

标签: java machine-learning computer-vision libsvm


【解决方案1】:

您可以使用 javaML 库对数据进行分类

这是一个带有 javaML 的示例代码:

   Classifier clas = new LibSVM();
        clas.buildClassifier(data);
        Dataset dataForClassification= FileHandler.loadDataset(new File(.),            0, ",");
        /* Counters for correct and wrong predictions. */
        int correct = 0, wrong = 0;
        /* Classify all instances and check with the correct class values */
        for (Instance inst : dataForClassification) {
            Object predictedClassValue = clas.classify(inst);
            Map<Object,Double> map = clas.classDistribution(inst);
            Object realClassValue = inst.classValue();
            if (predictedClassValue.equals(realClassValue))
                correct++;
            else
                wrong++;
        }

【讨论】:

  • 如果我有 Libsvm 格式的特征向量文件,我不知道如何进行训练。请您提供更多详细信息。
【解决方案2】:

A) 没有人知道您在引用。如果您不希望人们理解您所指的内容,请提供链接。

B) 您需要参加机器学习课程。 Coursera 有一个免费的。模型的输出取决于数据本身,并且受模型参数的影响很大。模型参数受缩放影响,您通常需要搜索它们。你的代码没有包含这些——而且你已经明确表示你是机器学习的新手。通过获得必要的背景知识,您将花费数小时、数天和数周的时间来了解几分钟内可以完成的工作。

C) 用于 Java 的 LIBSVM 有很多版本,但您没有说明您使用的是哪一个。每个人的工作方式都有些不同。

【讨论】:

  • 我正在使用 libsvm 3.17。我提到的参考与我放置的代码相同。
【解决方案3】:

您似乎无法理解自己在做什么,只是到处复制代码。它可以帮助您了解基本的机器学习。例如,您可能应该从 LIBSVM(您使用的库)的作者那里阅读此 practical guide for SVM classification。您在这里得到的建议是,您可能应该参加在线机器学习入门课程,这可能会更好。

让我也给你两个重要的提示,如果你得到同一类的所有结果,这可能会节省你的时间:

  1. 您是否对数据进行规范化,使所有值都介于 0 和 1 之间 (或介于 -1 和 1 之间),线性地或使用均值和 标准差?从你的代码来看,它不像你。
  2. 您是否正在参数搜索一个好的 C 值(或 C 和 gamma in RBF内核的情况)?进行交叉验证或暂停 放?从你的代码看来,你不是。

【讨论】:

  • 我已经使用 libsvm 从命令行检查了我的数据集的准确性,并且它在没有缩放的情况下给出了很好的结果。缩放我只在特征提取时完成,现在我想在我的项目中嵌入 libsvm。我有 libsvm 格式的培训和测试文件。现在如何在 java 中使用 libsvm 进行编程是我的问题。
【解决方案4】:

这是因为你的 featuresTesting 从未使用过,HashMap&lt;Integer, Double&gt; tmp=new HashMap&lt;Integer, Double&gt;(); 应该是HashMap&lt;Integer, Double&gt; tmp=featuresTesting.get(testInstance);

【讨论】:

    猜你喜欢
    • 2013-07-01
    • 2014-06-11
    • 2012-02-27
    • 2014-06-12
    • 2019-01-17
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多