// imports import weka.classifiers.Evaluation import weka.classifiers.bayes.NaiveBayes import weka.classifiers.evaluation.ThresholdCurve import weka.core.converters.ConverterUtils.DataSource import java.util.Random import org.jfree.data.xy.DefaultXYDataset import org.jfree.chart.ChartFactory import org.jfree.chart.plot.PlotOrientation import org.jfree.chart.ChartPanel import javax.swing.JFrame // load data env = System.getenv() data = DataSource.read(env["MOOC_DATA"] + File.separator + "balance-scale.arff") data.setClassIndex(data.numAttributes() - 1) // configure classifier cls = new NaiveBayes() // cross-validate classifier evl = new Evaluation(data) evl.crossValidateModel(cls, data, 10, new Random(1)) // create plot data plotdata = new DefaultXYDataset() data.classAttribute().numValues().times { tc = new ThresholdCurve() curve = tc.getCurve(evl.predictions(), it) fpr = curve.attributeToDoubleArray(curve.attribute("False Positive Rate").index()) tpr = curve.attributeToDoubleArray(curve.attribute("True Positive Rate").index()) plotdata.addSeries( data.classAttribute().value(it) + " [AUC: " + ThresholdCurve.getROCArea(curve) + "]", [fpr, tpr] as double[][]) } // create plot plot = ChartFactory.createXYLineChart( "ROC - NB on " + data.relationName(), "False Positive Rate", "True Positive Rate", plotdata, PlotOrientation.VERTICAL, true, true, true) // display plot frame = new JFrame() frame.setTitle("Weka") frame.setSize(800, 600) frame.setLocationRelativeTo(null) frame.getContentPane().add(new ChartPanel(plot)) frame.setVisible(true)