Weka分类---J48

利用iris数据集用weka自带的J48训练分类器

详细见程序注释

package wekaTest;

import weka.classifiers.meta.FilteredClassifier;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

public class test001 {
    public static void main(String[] args) throws Exception{
//       训练集文件名    iris   
        String Filename ="D://Program Files//Weka-3-6//data//iris.arff";
        DataSource source = new DataSource(Filename);
        Instances train = source.getDataSet();
//      System.out.println(train);
        Instances test = train;
//       设置类别标签所在的列  
//      iris数据最后一类是类别
//      cpu.with.vendor数据第一类是类别
        train.setClassIndex(train.numAttributes() - 1);
//       关系名字
        System.out.println(train.relationName());
//       属性名 
        for(int index = 0;index<train.numAttributes();index++)
            System.out.println(train.attribute(index));
//      类别的个数
        System.out.println(train.classIndex()-1);
//      输出类别标签
        for(int i=0;i< train.classIndex()-1;i++)
            System.out.println(train.classAttribute().value(i));
        J48 j48 = new J48();
        j48.setUnpruned(true);
        FilteredClassifier fc = new FilteredClassifier();

        fc.setClassifier(j48);
        fc.buildClassifier(train);
        int num = test.numInstances();
//      System.out.println("run");
        int right = 0;
        for( int i=0;i< num ;i++){
//          第i个样本数据 
//          System.out.print(test.instance(i)+"\t");
//          对第i个样本进行预测的第几个类别 
            double pred = fc.classifyInstance(test.instance(i));
//          实际类别
            String actual = test.classAttribute().value((int) test.instance(i).classValue());
//          预测类别
            String predicted = test.classAttribute().value((int) pred);

            if( actual == predicted){
                right++;
            }else{
                System.out.print("pred:" + pred);
                System.out.print("ID: " + i);
                System.out.print(", actual: " + actual);
                System.out.println(", predicted: " + predicted);
            }
        }
        System.out.println("正确率:"+1.0*right/num);
    }
}

输出结果

预测错误
pred:2.0ID: 70, actual: Iris-versicolor, predicted: Iris-virginica
pred:2.0ID: 83, actual: Iris-versicolor, predicted: Iris-virginica
pred:1.0ID: 106, actual: Iris-virginica, predicted: Iris-versicolor
pred:1.0ID: 129, actual: Iris-virginica, predicted: Iris-versicolor
pred:1.0ID: 133, actual: Iris-virginica, predicted: Iris-versicolor
正确率:0.9666666666666667

当然上面是训练集建模预测自己的

参考链接

你可能感兴趣的:(weka)