Java程序通过weka调用libsvm和liblinear的方法

<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">《</span><a href="http://blog.csdn.net/Felomeng/archive/2009/10/17/4688414.aspx"><span lang="EN-US"><span style="font-family: Calibri;">Java</span></span><span style="">程序通过</span><span lang="EN-US"><span style="font-family: Calibri;">weka</span></span><span style="">调用</span><span lang="EN-US"><span style="font-family: Calibri;">libsvm</span></span></a><span style=""><a href="http://blog.csdn.net/Felomeng/archive/2009/10/17/4688414.aspx">的方法</a>》一文阐述了调用</span><span lang="EN-US"><span style="font-family: Calibri;">LibSVM</span></span><span style="">的简单方法,这里主要补充四点:</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-family: Calibri; font-size: small;">1.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">LibSVM</span></span><span style="">在</span><span lang="EN-US"><span style="font-family: Calibri;">weka3.5</span></span><span style="">版以后都集成了包装,无须再使用</span><span lang="EN-US"><span style="font-family: Calibri;">WLSVM</span></span><span style="">,所以上文中的</span><span lang="EN-US"><span style="font-family: Calibri;">WLSVM</span></span><span style="">相关的内容可以去除,而类型</span><span lang="EN-US"><span style="font-family: Calibri;">WLSVM</span></span><span style="">可以换成</span><span lang="EN-US"><span style="font-family: Calibri;">LibSVM</span></span><span style="">,需要添加包</span></span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><strong><span style='font-family: "Courier New"; color: #7f0055; font-size: 10pt;' lang="EN-US">import</span></strong><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US"> weka.classifiers.functions.LibSVM;</span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style="">无须再使用</span><strong><span style='font-family: "Courier New"; color: #7f0055; font-size: 10pt;' lang="EN-US">import</span></strong><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US"> wlsvm.WLSVM;</span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-family: Calibri; font-size: small;">2.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">LibLINEAR</span></span><span style="">占用内存小,可以在大数据集大特征量的情况下使用,调用方法与</span><span lang="EN-US"><span style="font-family: Calibri;">LibSVM</span></span><span style="">相同,不同的就是把类</span><span lang="EN-US"><span style="font-family: Calibri;">LibSVM</span></span><span style="">换成</span><span lang="EN-US"><span style="font-family: Calibri;">LibLINEAR</span></span><span style="">(同时,加载的</span><span lang="EN-US"><span style="font-family: Calibri;">jar</span></span><span style="">包为</span><span lang="EN-US"><span style="font-family: Calibri;">LibLINEAR</span></span><span style="">,加载方法见《<a href="http://blog.csdn.net/Felomeng/archive/2009/10/17/4688257.aspx">在</a></span><a href="http://blog.csdn.net/Felomeng/archive/2009/10/17/4688257.aspx"><span lang="EN-US"><span style="font-family: Calibri;">eclipse</span></span><span style="">下使用</span><span lang="EN-US"><span style="font-family: Calibri;">java</span></span><span style="">调用</span><span lang="EN-US"><span style="font-family: Calibri;">weka</span></span></a><span style="">》下载地址:</span><span lang="EN-US"><a href="http://download.csdn.net/source/1998016"><span style="font-family: Calibri; color: #0000ff;">http://download.csdn.net/source/1998016</span></a></span><span style="">)。</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-family: Calibri; font-size: small;">3.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">参数设置</span></span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style=""><span style="font-size: small;">在</span></span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">m_classifier.buildClassifier(instancesTrain); </span><span style='font-family: "Courier New"; color: #3f7f5f; font-size: 10pt;' lang="EN-US">//</span><span style="">训练</span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style=""><span style="font-size: small;">前添加:</span></span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">String[] options=weka.core.Utils.<em>splitOptions</em>(</span><span style='font-family: "Courier New"; color: #2a00ff; font-size: 10pt;' lang="EN-US">"-S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.0010 -P 0.1 -B 0"</span><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">);</span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">m_classifier.setOptions(options);</span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-family: Calibri; font-size: small;">4.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">分类器序列化与反序列化(保存与读取)</span></span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">weka</span></span><span style="">内置了序列化类,只要使用:</span></span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">weka.core.SerializationHelper.<em>write</em>(</span><span style='font-family: "Courier New"; color: #2a00ff; font-size: 10pt;' lang="EN-US">“E:\model”</span><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">, m_classifier);</span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style="font-size: small;"><span style="">就可以保存模型了,使用时只要再使用下面语句读取出来(以</span><span lang="EN-US"><span style="font-family: Calibri;">LibSVM</span></span><span style="">为例):</span></span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">Classifier classifier=(LibSVM)weka.core.SerializationHelper.read(</span><span style='font-family: "Courier New"; color: #2a00ff; font-size: 10pt;' lang="EN-US">“E:\model”</span><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US">);</span></p>
<p class="MsoListParagraph" style="text-indent: 0cm; margin: 0cm 0cm 0pt 18pt;"><span style='font-family: "Courier New"; color: black; font-size: 10pt;' lang="EN-US"></span></p>

你可能感兴趣的:(java)