天天看點

weka 交叉驗證

調用weka實作交叉驗證,并搭載圖形界面

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.evaluation.ThresholdCurve;
import weka.core.Instances;
import weka.core.Utils;
import weka.gui.visualize.Plot2D;
import weka.gui.visualize.PlotData2D;
import weka.gui.visualize.ThresholdVisualizePanel;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Random;

public class ROC {
    public static void main(String[] args) throws Exception {
        String filePath = "d:/data/segment-challenge.arff";
        BufferedReader buf = new BufferedReader(new FileReader(new File(filePath)));
        Instances instance = new Instances(buf);
        //擷取屬性
        instance.setClassIndex(instance.numAttributes() - );
        //擷取分類器
        Classifier classifier = new NaiveBayes();
        //評價分類模型
        Evaluation evaluation = new Evaluation(instance);
        //交叉驗證  分類器,資料執行個體,交叉數目,
        evaluation.crossValidateModel(classifier, instance, , new Random());

        ThresholdCurve tc = new ThresholdCurve();
        //classIndex是類作為positive的索引
        int classIndex = ;
        Instances instances = tc.getCurve(evaluation.predictions(), classIndex);
        System.out.println("Roc curve" + evaluation.areaUnderROC(classIndex));

        //擷取TP,Fp
        int tpIndex = instances.attribute(ThresholdCurve.TP_RATE_NAME).index();
        int fpIndex = instances.attribute(ThresholdCurve.TP_RATE_NAME).index();

        double[] tpRate = instances.attributeToDoubleArray(tpIndex);
        double[] fpRate = instances.attributeToDoubleArray(fpIndex);

//      System.out.println(Arrays.toString(tpRate)+Arrays.toString(fpRate));
        for (double tp : tpRate) {
            System.out.println(tp);
        }
        for (double fp : fpRate) {
            System.out.println(fp);
        }
        //使用instances對象顯示ROC曲面
        ThresholdVisualizePanel tvp = new ThresholdVisualizePanel();
        tvp.setROCString("(Area under ROC=" +
                Utils.doubleToString(tc.getROCArea(instances), ) + ")");
        tvp.setName(instances.relationName());
        PlotData2D pd = new PlotData2D(instances);
        tvp.addPlot(pd);

        String plotName = tvp.getName();
        final javax.swing.JFrame jf = new javax.swing.JFrame(
                "WeKa classifier visualize:" + plotName);
        jf.setSize(, );
        jf.getContentPane().setLayout(new BorderLayout());
        jf.getContentPane().add(tvp, BorderLayout.CENTER);
        jf.addWindowFocusListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
                jf.dispose();
            }
        });
        jf.setVisible(true);
    }

}
           

運作結果

weka 交叉驗證