哪个Weka和LibSVM .jar文件在Java代码中用于SVM分类 [英] Which Weka and LibSVM .jar files to use in Java code for SVM classification

查看:152
本文介绍了哪个Weka和LibSVM .jar文件在Java代码中用于SVM分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用Weka Explorer运行一些训练数据,使用带有线性内核的SVM测试数据,一切都很好。

If I use Weka Explorer to run some training data against testing data using SVM with a linear kernel, everything is fine.

但我需要以编程方式执行此操作我自己的Java和我当前的代码如下所示:

But I need to do this programmatically in my own Java and my current code looks like this:

Instances train = new Instances (...);
train.setClassIndex(train.numAttributes() - 1);
Instances test = new Instances (...) + 
ClassificationType classificationType = ClassificationTypeDAO.get(6);       
LibSVM libsvm = new LibSVM();
String options = (classificationType.getParameters());
String[] optionsArray = options.split(" ");                  
libsvm.setOptions(optionsArray);
String[] pars = libsvm.getOptions();     
Evaluation eval = new Evaluation(train);
libsvm.buildClassifier(train);       
eval.evaluateModel(libsvm, test);

System.out.println(eval.toSummaryString("\nResults\n======\n", false)); 

然而,在线上抛出异常:

However, an exception is being thrown at line:

eval.evaluateModel(libsvm,test);

尽管多次尝试 try ... catch 这个代码周围的块,发生的异常只是报告为 null (这真的很有帮助)跟踪下面。

And despite numerous attempts at try...catch blocks around this code, the exception occurring is simply reported as null (which is really helpful) as per full stack trace below.

我不相信这个问题是由于我自己的代码,因为其他分类器已成功运行它。我正在研究这个问题的原因是环境的理论。但在哪里和什么?我正在使用Tomcat通过NetBeans 8运行我的应用程序,并在最近版本的 weka.jar LibSVM.jar 中应用程序的 .lib 文件夹。

I don't believe this issue is due to my own code because other classifiers have run successfully with it. I am working on the theory that the cause of the problem is environmental. But where and what? I am running my application through NetBeans 8 using Tomcat and have recent versions of weka.jar and LibSVM.jar in the application's .lib folder.

但是我需要 libsvm.jar 由下载提供:

But do I need libsvm.jar as provided by the download from:

http://www.csie.ntu.edu.tw/~cjlin/libsvm/

如果是后者的话,如何在Windows中解决 LibSVM.jar libsvm.jar 被视为同一文件的命名冲突?

If the latter is the case, how can I resolve naming conflicts in Windows where LibSVM.jar and libsvm.jar are treated as the same file?

过去几个小时我真的很困惑。我尝试将 LibSVM.jar libsvm.jar 文件添加到 .lib 文件夹,将它们重命名,将它们放入新定义的 CLASSPATH 中,但没有任何作用。

This has been really confusing me for the last few hours. I have tried adding both LibSVM.jar and libsvm.jar files into the .lib folder, renaming them both, putting them into a newly defined CLASSPATH, but nothing works.

Java异常的完整堆栈跟踪是:

The full stack trace for the Java exception is:


null
weka.classifiers.functions.LibSVM.distributionForInstance(LibSVM) .java:1489)
weka.classifiers.Evaluation.evaluationForSingleInstance(Evaluation.java:1560)
weka.classifiers.Evaluation.evaluateModelOnceAndRecordPrediction(Evaluation.java:1597)
weka.classifiers.Evaluation .evaluateModel(Evaluation.java:1477)
visualRSS.test.Weka_LibSVM_Test.classify(Weka_LibSVM_Test.java:48)
visualRSS.initialisation.TestProgram_Context_Listener.contextInitialized(TestProgram_Context_Listener.java:29)
org .apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
org.apache.catalina.core.StandardContext.start(Standard Context.java:4467)
org.apache.catalina.core.StandardContext.reload(StandardContext.java:3228)
org.apache.catalina.manager.ManagerServlet.reload(ManagerServlet.java:943)
org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:361)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet。 http.HttpServlet.service(HttpServlet.java:717)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
org.apache.catalina.core.ApplicationFilterChain.doFilter( ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:558)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org。 apache.catalina.valves.Err orReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter。 java:298)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint $ Worker.run(AprEndpoint.java:1555)

null weka.classifiers.functions.LibSVM.distributionForInstance(LibSVM.java:1489) weka.classifiers.Evaluation.evaluationForSingleInstance(Evaluation.java:1560) weka.classifiers.Evaluation.evaluateModelOnceAndRecordPrediction(Evaluation.java:1597) weka.classifiers.Evaluation.evaluateModel(Evaluation.java:1477) visualRSS.test.Weka_LibSVM_Test.classify(Weka_LibSVM_Test.java:48) visualRSS.initialisation.TestProgram_Context_Listener.contextInitialized(TestProgram_Context_Listener.java:29) org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972) org.apache.catalina.core.StandardContext.start(StandardContext.java:4467) org.apache.catalina.core.StandardContext.reload(StandardContext.java:3228) org.apache.catalina.manager.ManagerServlet.reload(ManagerServlet.java:943) org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:361) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:558) org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859) org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)


推荐答案

我的测试代码的问题与Weka以编程方式运行LibSVM所需的 .jar 文件有关。

The problem with my test code was environmental to do with the .jar files needed for Weka to programmatically run LibSVM.

如果我的代码是:

public static void classify() {      
    try {            
        Instances train = new Instances (...);            
        train.setClassIndex(train.numAttributes() - 1);         
        Instances test = new Instances (...);            
        test.setClassIndex(test.numAttributes() - 1);                      
        ClassificationType classificationType = ClassificationTypeDAO.get(6);  // 6 is SVM.        
        LibSVM classifier = new LibSVM();
        String options = (classificationType.getParameters());
        String[] optionsArray = options.split(" ");                          
        classifier.setOptions(optionsArray);        
        classifier.buildClassifier(train);        
        Evaluation eval = new Evaluation(train);
        eval.evaluateModel(classifier, test);
        System.out.println(eval.toSummaryString("\nResults\n======\n", false));       
    } 
    catch (Exception ex) {            
        Misc_Utils.printStackTrace(ex);
    }                       
}

我发现我需要放置 weka.jar (来自Weka)和 libsvm.jar (来自 .lib中的edu.tw/~cjlin/libsvm/\"rel =nofollow> http://www.csie.ntu.edu.tw/~cjlin/libsvm/ 文件夹。但由于Windows中的命名冲突,我将文件 LibSVM.jar (从Weka)重命名为 LibSVM_Weka .jar 并将其添加到 .lib 文件夹中。

I found that I needed to place weka.jar (from Weka) and libsvm.jar (from http://www.csie.ntu.edu.tw/~cjlin/libsvm/ in the application's .lib folder. But because of the naming clash in Windows, I renamed the file LibSVM.jar (from Weka) to LibSVM_Weka.jar and added it to the .lib folder.

运行程序I现在有一些结果与Weka的资源管理器相匹配,使用的关键字频率在5类数据中分布不均。

Running the program I now have results which match Weka's Explorer using keyword frequencies distributed unevenly across 5 categories of data.

这篇关于哪个Weka和LibSVM .jar文件在Java代码中用于SVM分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆