机器学习和生物信息学实验室联盟
标题:
eclipse中Java程序调用weka
[打印本页]
作者:
zouquan
时间:
2011-3-7 20:42
标题:
eclipse中Java程序调用weka
要使用程序方式使用weka,步骤如下:
一、在eclipse里新建一个java project:
1. 建立工程:单击菜单中file->new->java project,在弹出对话框的project name中起任意一个名字,此处假设是wekaTest。单击Finish按钮(在对话框底部)。
2. 建立package:在package Explorer中找到刚才新建的工程,在其上右键->New->package。在Name文本框里面输入名称,此处假设为Test。单击Finish按钮。
3. 建立程序文件:在刚才新建的package上面右键->New->class,选中public static void main(String[] args)多选框,单击Finish。
二、在该工程中添加weka的引用:
1.
package Explorer中工程名上右键,选择弹出菜单最后一项properties->在左面选中java Build Path->在右面的Library页面->单击Add External JARs…->浏览weka所在目录,将weka.jar添加进来,然后单击ok。
2. 在package Explorer中在双击Test文件,然后在package wekaTest;一句下面添加四句代码:
import java.io.File;
import weka.classifiers.Classifier;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
三、在程序中添加weka调用代码:
将以下代码添加到Main函数中(在// TODO Auto-generated method stub下面):
Classifier m_classifier = new J48();
File inputFile = new File("D:\\Program Files\\Weka-3-6\\data\\cpu.with.vendor.arff");//训练语料文件
ArffLoader atf = new ArffLoader();
atf.setFile(inputFile);
Instances instancesTrain = atf.getDataSet(); // 读入训练文件
inputFile = new File("D:\\Program Files\\Weka-3-6\\data\\cpu.with.vendor.arff");//测试语料文件
atf.setFile(inputFile);
Instances instancesTest = atf.getDataSet(); // 读入测试文件
instancesTest.setClassIndex(0); //设置分类属性所在行号(第一行为0号),instancesTest.numAttributes()可以取得属性总数
double sum = instancesTest.numInstances(),//测试语料实例数
right = 0.0f;
instancesTrain.setClassIndex(0);
m_classifier.buildClassifier(instancesTrain); //训练
for(int i = 0;i<sum;i++)//测试分类结果
{
if(m_classifier.classifyInstance(instancesTest.instance(i))==instancesTest.instance(i).classValue())//如果预测值和答案值相等(测试语料中的分类列提供的须为正确答案,结果才有意义)
{
right++;//正确值加1
}
}
System.out.println("J48 classification precision:"+(right/sum));
四、运行一下试试。
作者:
endual
时间:
2012-9-27 17:01
package com.endual.paper.service.impls;
import weka.attributeSelection.*;
import weka.core.*;
import weka.core.converters.ConverterUtils.*;
import weka.classifiers.*;
import weka.classifiers.meta.*;
import weka.classifiers.trees.*;
import weka.filters.*;
import java.util.*;
/**
* performs attribute selection using CfsSubsetEval and GreedyStepwise
* (backwards) and trains J48 with that. Needs 3.5.5 or higher to compile.
*
* @author FracPete (fracpete at waikato dot ac dot nz)
*/
public class AttributeSelectionTest {
/**
* uses the meta-classifier
*/
protected static void useClassifier(Instances data) throws Exception {
System.out.println("\n1. Meta-classfier");
AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();
CfsSubsetEval eval = new CfsSubsetEval();
GreedyStepwise search = new GreedyStepwise();
search.setSearchBackwards(true);
J48 base = new J48();
classifier.setClassifier(base);
classifier.setEvaluator(eval);
classifier.setSearch(search);
Evaluation evaluation = new Evaluation(data);
evaluation.crossValidateModel(classifier, data, 10, new Random(1));
System.out.println(evaluation.toSummaryString());
}
/**
* uses the filter
*/
protected static void useFilter(Instances data) throws Exception {
System.out.println("\n2. Filter");
weka.filters.supervised.attribute.AttributeSelection filter = new weka.filters.supervised.attribute.AttributeSelection();
CfsSubsetEval eval = new CfsSubsetEval();
GreedyStepwise search = new GreedyStepwise();
search.setSearchBackwards(true);
filter.setEvaluator(eval);
filter.setSearch(search);
filter.setInputFormat(data);
Instances newData = Filter.useFilter(data, filter);
System.out.println(newData);
}
/**
* uses the low level approach
*/
protected static void useLowLevel(Instances data) throws Exception {
System.out.println("\n3. Low-level");
AttributeSelection attsel = new AttributeSelection();
CfsSubsetEval eval = new CfsSubsetEval();
GreedyStepwise search = new GreedyStepwise();
search.setSearchBackwards(true);
attsel.setEvaluator(eval);
attsel.setSearch(search);
attsel.SelectAttributes(data);
int[] indices = attsel.selectedAttributes();
System.out.println("selected attribute indices (starting with 0):\n" + Utils.arrayToString(indices));
}
/**
* takes a dataset as first argument
*
* @param args the commandline arguments
* @throws Exception if something goes wrong
*/
public static void main(String[] args) throws Exception {
args = new String[1] ;
args[0] = "weka_data\\data\\src_data\\paper.arff" ; //导入文件
// load data
System.out.println("\n0. Loading data");
DataSource source = new DataSource(args[0]);
Instances data = source.getDataSet();
if (data.classIndex() == -1)
data.setClassIndex(data.numAttributes() - 1);
// 1. meta-classifier
useClassifier(data);
// 2. filter
useFilter(data);
// 3. low-level
useLowLevel(data);
}
}
复制代码
我这个是网上抄来的,有分类,过滤属性,降低维度的调用代码,仅供参考。
欢迎光临 机器学习和生物信息学实验室联盟 (http://123.57.240.48/)
Powered by Discuz! X3.2