在eclipse下使用java调用weka

<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt; line-height: 150%; text-align: left;" align="left"><span style="" lang="EN-US">weka</span><span style="">是很好用的机器学习库,这里就不</span><span style="" lang="EN-US"><a href="http://blog.csdn.net/Felomeng/archive/2009/10/17/4687061.aspx"><span style="" lang="EN-US"><span lang="EN-US"><span style="color: #0000ff;"><span style="text-decoration: underline;">详细介绍</span></span></span></span></a></span><span style="">了。</span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt; line-height: 150%; text-align: left;" align="left"><span style="">言归正传,要使用程序方式使用<span lang="EN-US">weka</span>,步骤如下:</span><span style="" lang="EN-US"></span></p>
<p class="MsoListParagraph" style="" align="left"><span style="" lang="EN-US"><span style="">一、</span></span><span style="">在<span lang="EN-US">eclipse</span>里新建一个<span lang="EN-US">java project</span>: </span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="" align="left"><span style="" lang="EN-US"><span style="">1.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style="">建立工程:单击菜单中<span lang="EN-US">file-&gt;new-&gt;java project</span>,在弹出对话框的<span lang="EN-US">project name</span>中起任意一个名字,此处假设是<span lang="EN-US">wekaTest</span>。单击<span lang="EN-US">Finish</span>按钮(在对话框底部)。 </span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="" align="left"><span style="" lang="EN-US"><span style="">2.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style="">建立<span lang="EN-US">package</span>:在<span lang="EN-US">package Explorer</span>中找到刚才新建的工程,在其上右键<span lang="EN-US">-&gt;New-&gt;package</span>。在<span lang="EN-US">Name</span>文本框里面输入名称,此处假设为<span lang="EN-US">Test</span>。单击<span lang="EN-US">Finish</span>按钮。 </span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="" align="left"><span style="" lang="EN-US"><span style="">3.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style="">建立程序文件:在刚才新建的<span lang="EN-US">package</span>上面右键<span lang="EN-US">-&gt;New-&gt;class</span>,选中<span lang="EN-US">public static void main(String[] args)</span>多选框,单击<span lang="EN-US">Finish</span>。</span><span style="" lang="EN-US"></span></p>
<p class="MsoListParagraph" style="" align="left"><span style="" lang="EN-US"><span style="">二、</span></span><span style="">在该工程中添加<span lang="EN-US">weka</span>的引用: </span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="" align="left"><span style="" lang="EN-US"><span style="">1.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style="" lang="EN-US">package Explorer</span><span style="">中工程名上右键,选择弹出菜单最后一项<span lang="EN-US">properties-&gt;</span>在左面选中<span lang="EN-US">java Build Path-&gt;</span>在右面的<span lang="EN-US">Library</span>页面<span lang="EN-US">-&gt;</span>单击<span lang="EN-US">Add External JARs…-&gt;</span>浏览<span lang="EN-US">weka</span>所在目录,将<span lang="EN-US">weka.jar</span>添加进来,然后单击<span lang="EN-US">ok</span>。</span><span style=""> <span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="" align="left"><span style="" lang="EN-US"><span style="">2.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style="">在<span lang="EN-US">package Explorer</span>中在双击<span lang="EN-US">Test</span>文件,然后在<span lang="EN-US">package wekaTest;</span>一句下面添加四句代码:</span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">import</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> java.io.File;</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">import</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> weka.classifiers.Classifier;</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">import</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> weka.classifiers.trees.J48;</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">import</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> weka.core.Instances;</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt; line-height: 150%; text-align: left;" align="left"><strong><span style='font-size: 10pt; color: #7f0055; line-height: 150%; font-family: "Courier New";' lang="EN-US">import</span></strong><span style='font-size: 10pt; color: black; line-height: 150%; font-family: "Courier New";' lang="EN-US"> weka.core.converters.ArffLoader;</span><span style="" lang="EN-US"> </span></p>
<p class="MsoListParagraph" style="" align="left"><span style="" lang="EN-US"><span style="">三、</span></span><span style="">在程序中添加<span lang="EN-US">weka</span>调用代码: </span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt; text-indent: 1cm; line-height: 150%; text-align: left;" align="left"><span style="">将以下代码添加到<span lang="EN-US">Main</span>函数中(在<span lang="EN-US">// TODO Auto-generated method stub</span>下面):</span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>Classifier m_classifier = </span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">new</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> J48();</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>File inputFile = </span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">new</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> File(</span><span style='font-size: 10pt; color: #2a00ff; font-family: "Courier New";' lang="EN-US">"D:\\Program Files\\Weka-3-6\\data\\cpu.with.vendor.arff"</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">);</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">训练语料文件</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>ArffLoader atf = </span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">new</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> ArffLoader();<span style=""> </span></span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>atf.setFile(inputFile);</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span><span style=""> </span><span style=""></span><span style=""> </span>Instances instancesTrain = atf.getDataSet(); </span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">// </span><span style="">读入训练文件</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US"><span style=""> </span></span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span><span style=""> </span><span style=""></span><span style=""> </span>inputFile = </span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">new</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> File(</span><span style='font-size: 10pt; color: #2a00ff; font-family: "Courier New";' lang="EN-US">"D:\\Program Files\\Weka-3-6\\data\\cpu.with.vendor.arff"</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">);</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">测试语料文件</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span><span style=""> </span><span style=""></span><span style=""> </span>atf.setFile(inputFile);<span style=""> </span></span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>Instances instancesTest = atf.getDataSet(); </span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">// </span><span style="">读入测试文件</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>instancesTest.setClassIndex(0); </span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">设置分类属性所在行号(第一行为</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">0</span><span style="">号),</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">instancesTest.numAttributes()</span><span style="">可以取得属性总数</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span></span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">double</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> sum = instancesTest.numInstances(),</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">测试语料实例数</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>right = 0.0f;</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span><span style=""> </span><span style=""></span><span style=""> </span>instancesTrain.setClassIndex(0); </span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span><span style=""> </span><span style=""></span><span style=""> </span>m_classifier.buildClassifier(instancesTrain); </span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">训练</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US"><span style=""> </span><span style=""> </span><span style=""></span></span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span></span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">for</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">(</span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">int</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>i = 0;i&lt;sum;i++)</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">测试分类结果</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>{</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span></span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">if</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">(m_classifier.classifyInstance(instancesTest.instance(i))==instancesTest.instance(i).classValue())</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">如果预测值和答案值相等(测试语料中的分类列提供的须为正确答案,结果才有意义)</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>{</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span><span style=""> </span>right++;</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//</span><span style="">正确值加</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">1</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>}</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style=""> </span>}</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt; line-height: 150%; text-align: left;" align="left"><span style='font-size: 10pt; color: black; line-height: 150%; font-family: "Courier New";' lang="EN-US"><span style=""> </span>System.</span><em><span style='font-size: 10pt; color: #0000c0; line-height: 150%; font-family: "Courier New";' lang="EN-US">out</span></em><span style='font-size: 10pt; color: black; line-height: 150%; font-family: "Courier New";' lang="EN-US">.println(</span><span style='font-size: 10pt; color: #2a00ff; line-height: 150%; font-family: "Courier New";' lang="EN-US">"J48 classification precision:"</span><span style='font-size: 10pt; color: black; line-height: 150%; font-family: "Courier New";' lang="EN-US">+(right/sum));</span><span style="" lang="EN-US"></span></p>
<p class="MsoListParagraph" style="" align="left"><span style="" lang="EN-US"><span style="">四、</span></span><span style="">运行一下试试。   </span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>

你可能感兴趣的:(在eclipse下使用java调用weka)