码迷,mamicode.com
首页 > 其他好文 > 详细

weka 的代码调用

时间:2016-04-08 11:38:58      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

package yuce;

import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

public class testClassification {

    public static void main(String[] args) {
        try {
            File inputfile = new File("E:\\Develop/Weka-3-6/data/weather.numeric.arff");
            ArffLoader loader = new ArffLoader();
            loader.setFile(inputfile);
             
            Instances insTrain = loader.getDataSet();
            insTrain.setClassIndex(insTrain.numAttributes()-1);
             
            inputfile = new File("E:\\Develop/Weka-3-6/data/weather.numeric.arff");
            loader.setFile(inputfile);
            Instances insTest = loader.getDataSet();
            insTest.setClassIndex(insTest.numAttributes()-1);
             
            double sum = insTest.numInstances();
            int right = 0;
            Classifier clas = new J48();
            //Classifier clas = new weka.classifiers.bayes.BayesNet();
            clas.buildClassifier(insTrain);
             
            for(int i = 0; i < sum; i++) {
                if(clas.classifyInstance(insTest.instance(i)) == insTest.instance(i).classValue()) {
                    right++;
                }
                System.out.println(clas.classifyInstance(insTest.instance(i))+" : "+insTest.instance(i).classValue());
                System.out.println("classIndex:"+insTest.instance(i).classIndex());
            }
            System.out.println("分类准确率:"+right/sum);
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

 

 

data数据集:

@relation contact-lenses

@attribute age             {young, pre-presbyopic, presbyopic}
@attribute spectacle-prescrip    {myope, hypermetrope}
@attribute astigmatism        {no, yes}
@attribute tear-prod-rate    {reduced, normal}
@attribute contact-lenses    {soft, hard, none}

@data
%
% 24 instances
%
young,myope,no,reduced,none
young,myope,no,normal,soft
young,myope,yes,reduced,none
young,myope,yes,normal,hard
young,hypermetrope,no,reduced,none
young,hypermetrope,no,normal,soft
young,hypermetrope,yes,reduced,none
young,hypermetrope,yes,normal,hard
pre-presbyopic,myope,no,reduced,none
pre-presbyopic,myope,no,normal,soft
pre-presbyopic,myope,yes,reduced,none
pre-presbyopic,myope,yes,normal,hard
pre-presbyopic,hypermetrope,no,reduced,none
pre-presbyopic,hypermetrope,no,normal,soft
pre-presbyopic,hypermetrope,yes,reduced,none
pre-presbyopic,hypermetrope,yes,normal,none
presbyopic,myope,no,reduced,none
presbyopic,myope,no,normal,none
presbyopic,myope,yes,reduced,none
presbyopic,myope,yes,normal,hard
presbyopic,hypermetrope,no,reduced,none
presbyopic,hypermetrope,no,normal,soft
presbyopic,hypermetrope,yes,reduced,none
presbyopic,hypermetrope,yes,normal,none

 

weka 的代码调用

标签:

原文地址:http://www.cnblogs.com/rongyux/p/5367191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!