List of usage examples for weka.classifiers Evaluation Evaluation
public Evaluation(Instances data) throws Exception
From source file:asap.NLPSystem.java
private String crossValidate(int seed, int folds, String modelOutputFile) { PerformanceCounters.startTimer("cross-validation"); PerformanceCounters.startTimer("cross-validation init"); AbstractClassifier abstractClassifier = (AbstractClassifier) classifier; // randomize data Random rand = new Random(seed); Instances randData = new Instances(trainingSet); randData.randomize(rand);/*from ww w . j av a 2s .com*/ if (randData.classAttribute().isNominal()) { randData.stratify(folds); } // perform cross-validation and add predictions Evaluation eval; try { eval = new Evaluation(randData); } catch (Exception ex) { Logger.getLogger(NLPSystem.class.getName()).log(Level.SEVERE, null, ex); return "Error creating evaluation instance for given data!"; } List<Thread> foldThreads = (List<Thread>) Collections.synchronizedList(new LinkedList<Thread>()); List<FoldSet> foldSets = (List<FoldSet>) Collections.synchronizedList(new LinkedList<FoldSet>()); for (int n = 0; n < folds; n++) { try { foldSets.add(new FoldSet(randData.trainCV(folds, n), randData.testCV(folds, n), AbstractClassifier.makeCopy(abstractClassifier))); } catch (Exception ex) { Logger.getLogger(NLPSystem.class.getName()).log(Level.SEVERE, null, ex); } if (n < Config.getNumThreads() - 1) { Thread foldThread = new Thread(new CrossValidationFoldThread(n, foldSets, eval)); foldThreads.add(foldThread); } } PerformanceCounters.stopTimer("cross-validation init"); PerformanceCounters.startTimer("cross-validation folds+train"); if (Config.getNumThreads() > 1) { for (Thread foldThread : foldThreads) { foldThread.start(); } } else { new CrossValidationFoldThread(0, foldSets, eval).run(); } for (Thread foldThread : foldThreads) { while (foldThread.isAlive()) { try { foldThread.join(); } catch (InterruptedException ex) { Logger.getLogger(NLPSystem.class.getName()).log(Level.SEVERE, null, ex); } } } PerformanceCounters.stopTimer("cross-validation folds+train"); PerformanceCounters.startTimer("cross-validation post"); // evaluation for output: String out = String.format( "\n=== Setup ===\nClassifier: %s %s\n" + "Dataset: %s\nFolds: %s\nSeed: %s\n\n%s\n", abstractClassifier.getClass().getName(), Utils.joinOptions(abstractClassifier.getOptions()), trainingSet.relationName(), folds, seed, eval.toSummaryString(String.format("=== %s-fold Cross-validation ===", folds), false)); try { crossValidationPearsonsCorrelation = eval.correlationCoefficient(); } catch (Exception ex) { Logger.getLogger(NLPSystem.class.getName()).log(Level.SEVERE, null, ex); } if (modelOutputFile != null) { if (!modelOutputFile.isEmpty()) { try { SerializationHelper.write(modelOutputFile, abstractClassifier); } catch (Exception ex) { Logger.getLogger(NLPSystem.class.getName()).log(Level.SEVERE, null, ex); } } } classifierBuiltWithCrossValidation = true; PerformanceCounters.stopTimer("cross-validation post"); PerformanceCounters.stopTimer("cross-validation"); return out; }
From source file:asap.NLPSystem.java
private void evaluateModel(boolean printEvaluation) { // checkInstancesFeatures(evaluationSet); PerformanceCounters.startTimer("evaluateModel"); System.out.println("Evaluating model..."); AbstractClassifier abstractClassifier = (AbstractClassifier) classifier; try {/*from ww w.j av a 2s . c o m*/ // evaluate classifier and print some statistics Evaluation eval = new Evaluation(evaluationSet); evaluationPredictions = eval.evaluateModel(abstractClassifier, evaluationSet); if (printEvaluation) { System.out.println("\tstats for model:" + abstractClassifier.getClass().getName() + " " + Utils.joinOptions(abstractClassifier.getOptions())); System.out.println(eval.toSummaryString()); } evaluationPearsonsCorrelation = eval.correlationCoefficient(); evaluated = true; } catch (Exception ex) { Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("\tevaluation done."); PerformanceCounters.stopTimer("evaluateModel"); }
From source file:asap.PostProcess.java
private static double[] evaluateModel(AbstractClassifier cl, Instances data, boolean printEvaluation) { PerformanceCounters.startTimer("evaluateModel"); System.out.println("Evaluating model..."); double[] predictions = null; try {//from w w w. j a v a2 s .c o m // evaluate classifier and print some statistics Evaluation eval = new Evaluation(data); predictions = eval.evaluateModel(cl, data); if (printEvaluation) { System.out.println( "\tstats for model:" + cl.getClass().getName() + " " + Utils.joinOptions(cl.getOptions())); System.out.println(eval.toSummaryString()); } } catch (Exception ex) { Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("\tevaluation done."); PerformanceCounters.stopTimer("evaluateModel"); return predictions; }
From source file:assign00.ExperimentShell.java
/** * @param args the command line arguments *///ww w .j a v a2 s .c o m public static void main(String[] args) throws Exception { DataSource source = new DataSource(file); Instances dataSet = source.getDataSet(); //Set up data dataSet.setClassIndex(dataSet.numAttributes() - 1); dataSet.randomize(new Random(1)); //determine sizes int trainingSize = (int) Math.round(dataSet.numInstances() * .7); int testSize = dataSet.numInstances() - trainingSize; Instances training = new Instances(dataSet, 0, trainingSize); Instances test = new Instances(dataSet, trainingSize, testSize); Standardize standardizedData = new Standardize(); standardizedData.setInputFormat(training); Instances newTest = Filter.useFilter(test, standardizedData); Instances newTraining = Filter.useFilter(training, standardizedData); NeuralNetworkClassifier NWC = new NeuralNetworkClassifier(); NWC.buildClassifier(newTraining); Evaluation eval = new Evaluation(newTraining); eval.evaluateModel(NWC, newTest); System.out.println(eval.toSummaryString("\nResults\n======\n", false)); }
From source file:at.aictopic1.sentimentanalysis.machinelearning.impl.TwitterClassifer.java
public void trainModel() { Instances trainingData = loadTrainingData(); System.out.println("Class attribute: " + trainingData.classAttribute().toString()); // Partition dataset into training and test sets RemovePercentage filter = new RemovePercentage(); filter.setPercentage(10);// www . j av a2s .c o m Instances testData = null; // Split in training and testdata try { filter.setInputFormat(trainingData); testData = Filter.useFilter(trainingData, filter); } catch (Exception ex) { //Logger.getLogger(Trainer.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Error getting testData: " + ex.toString()); } // Train the classifier Classifier model = (Classifier) new NaiveBayes(); try { // Save the model to fil // serialize model weka.core.SerializationHelper.write(modelDir + algorithm + ".model", model); } catch (Exception ex) { Logger.getLogger(TwitterClassifer.class.getName()).log(Level.SEVERE, null, ex); } // Set the local model this.trainedModel = model; try { model.buildClassifier(trainingData); } catch (Exception ex) { //Logger.getLogger(Trainer.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Error training model: " + ex.toString()); } try { // Evaluate model Evaluation test = new Evaluation(trainingData); test.evaluateModel(model, testData); System.out.println(test.toSummaryString()); } catch (Exception ex) { //Logger.getLogger(Trainer.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Error evaluating model: " + ex.toString()); } }
From source file:au.edu.usyd.it.yangpy.sampling.BPSO.java
License:Open Source License
/** * this method evaluate a classifier with * the sampled data and internal test data * /* w w w. jav a 2 s. c o m*/ * @param c classifier * @param train sampled set * @param test internal test set * @return evaluation results */ public double classify(Classifier c, Instances train, Instances test) { double AUC = 0; double FM = 0; double GM = 0; try { c.buildClassifier(train); // evaluate classifier Evaluation eval = new Evaluation(train); eval.evaluateModel(c, test); AUC = eval.areaUnderROC(1); FM = eval.fMeasure(1); GM = eval.truePositiveRate(0); GM *= eval.truePositiveRate(1); GM = Math.sqrt(GM); } catch (IOException ioe) { ioe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } double mean = (AUC + FM + GM) / 3; if (verbose == true) { System.out.print("AUC: " + dec.format(AUC) + " "); System.out.print("FM: " + dec.format(FM) + " "); System.out.println("GM: " + dec.format(GM)); System.out.println(" \\ | / "); System.out.println(" Mean: " + dec.format(mean)); } return mean; }
From source file:au.edu.usyd.it.yangpy.snp.Ensemble.java
License:Open Source License
public double classify(Classifier c, int cId) throws Exception { // train the classifier with training data c.buildClassifier(train);/*from w ww .java 2s . c o m*/ // get the predict value and predict distribution from each test instances for (int i = 0; i < test.numInstances(); i++) { predictDistribution[cId][i] = c.distributionForInstance(test.instance(i)); predictValue[cId][i] = c.classifyInstance(test.instance(i)); } // of course, get the AUC for each classifier Evaluation eval = new Evaluation(train); eval.evaluateModel(c, test); return eval.areaUnderROC(1) * 100; }
From source file:binarizer.LayoutAnalysis.java
public double crossValidation(String arffFile) throws Exception { DataSource source = new DataSource(arffFile); Instances trainingData = source.getDataSet(); if (trainingData.classIndex() == -1) trainingData.setClassIndex(trainingData.numAttributes() - 1); NaiveBayes nb = new NaiveBayes(); nb.setUseSupervisedDiscretization(true); Evaluation evaluation = new Evaluation(trainingData); evaluation.crossValidateModel(nb, trainingData, 10, new Random(1)); System.out.println(evaluation.toSummaryString()); return evaluation.errorRate(); }
From source file:boostingPL.boosting.AdaBoost.java
License:Open Source License
private double weightError(int t) throws Exception { // evaluate all instances Evaluation eval = new Evaluation(insts); eval.evaluateModel(classifiers[t], insts); return eval.errorRate(); }
From source file:boostingPL.boosting.AdaBoost.java
License:Open Source License
public static void main(String[] args) throws Exception { java.io.File inputFile = new java.io.File( "/home/aax/xpShareSpace/dataset/single-class/+winered/winequality-red.datatrain1.arff"); ArffLoader atf = new ArffLoader(); atf.setFile(inputFile);/*from ww w . j ava2 s .c om*/ Instances training = atf.getDataSet(); training.setClassIndex(training.numAttributes() - 1); AdaBoost adaBoost = new AdaBoost(training, 100); for (int t = 0; t < 100; t++) { adaBoost.run(t); } java.io.File inputFilet = new java.io.File( "/home/aax/xpShareSpace/dataset/single-class/+winered/winequality-red.datatest1.arff"); ArffLoader atft = new ArffLoader(); atft.setFile(inputFilet); Instances testing = atft.getDataSet(); testing.setClassIndex(testing.numAttributes() - 1); Evaluation eval = new Evaluation(testing); for (Instance inst : testing) { eval.evaluateModelOnceAndRecordPrediction(adaBoost, inst); } System.out.println(eval.toSummaryString()); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); /* int right = 0; for (int i = 0; i < testing.numInstances(); i++) { Instance inst = testing.instance(i); if (adaBoost.classifyInstance(inst) == inst.classValue()) { right++; } } System.out.println(right); System.out.println((double)right/training.numInstances()); */ }