List of usage examples for weka.classifiers Evaluation toClassDetailsString
public String toClassDetailsString() throws Exception
From source file:FlexDMThread.java
License:Open Source License
public void run() { try {//from w w w.j a v a 2 s . co m //Get the data from the source FlexDM.getMainData.acquire(); Instances data = dataset.getSource().getDataSet(); FlexDM.getMainData.release(); //Set class attribute if undefined if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } //Process hyperparameters for classifier String temp = ""; for (int i = 0; i < classifier.getNumParams(); i++) { temp += classifier.getParameter(i).getName(); temp += " "; if (classifier.getParameter(i).getValue() != null) { temp += classifier.getParameter(i).getValue(); temp += " "; } } String[] options = weka.core.Utils.splitOptions(temp); //Print to console- experiment is starting if (temp.equals("")) { //no parameters temp = "results_no_parameters"; try { System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1) + " with no parameters"); } catch (Exception e) { System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName() + " with no parameters"); } } else { //parameters try { System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1) + " with parameters " + temp); } catch (Exception e) { System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName() + " with parameters " + temp); } } //Create classifier, setting parameters weka.classifiers.Classifier x = createObject(classifier.getName()); x.setOptions(options); x.buildClassifier(data); //Process the test selection String[] tempTest = dataset.getTest().split("\\s"); //Create evaluation object for training and testing classifiers Evaluation eval = new Evaluation(data); StringBuffer predictions = new StringBuffer(); //Train and evaluate classifier if (tempTest[0].equals("testset")) { //specified test file //Build classifier x.buildClassifier(data); //Open test file, load data //DataSource testFile = new DataSource(dataset.getTest().substring(7).trim()); // Instances testSet = testFile.getDataSet(); FlexDM.getTestData.acquire(); Instances testSet = dataset.getTestFile().getDataSet(); FlexDM.getTestData.release(); //Set class attribute if undefined if (testSet.classIndex() == -1) { testSet.setClassIndex(testSet.numAttributes() - 1); } //Evaluate model Object[] array = { predictions, new Range(), new Boolean(true) }; eval.evaluateModel(x, testSet, array); } else if (tempTest[0].equals("xval")) { //Cross validation //Build classifier x.buildClassifier(data); //Cross validate eval.crossValidateModel(x, data, Integer.parseInt(tempTest[1]), new Random(1), predictions, new Range(), true); } else if (tempTest[0].equals("leavexval")) { //Leave one out cross validation //Build classifier x.buildClassifier(data); //Cross validate eval.crossValidateModel(x, data, data.numInstances() - 1, new Random(1), predictions, new Range(), true); } else if (tempTest[0].equals("percent")) { //Percentage split of single data set //Set training and test sizes from percentage int trainSize = (int) Math.round(data.numInstances() * Double.parseDouble(tempTest[1])); int testSize = data.numInstances() - trainSize; //Load specified data Instances train = new Instances(data, 0, trainSize); Instances testSet = new Instances(data, trainSize, testSize); //Build classifier x.buildClassifier(train); //Train and evaluate model Object[] array = { predictions, new Range(), new Boolean(true) }; eval.evaluateModel(x, testSet, array); } else { //Evaluate on training data //Test and evaluate model Object[] array = { predictions, new Range(), new Boolean(true) }; eval.evaluateModel(x, data, array); } //create datafile for results String filename = dataset.getDir() + "/" + classifier.getDirName() + "/" + temp + ".txt"; PrintWriter writer = new PrintWriter(filename, "UTF-8"); //Print classifier, dataset, parameters info to file try { writer.println("CLASSIFIER: " + classifier.getName() + "\n DATASET: " + dataset.getName() + "\n PARAMETERS: " + temp); } catch (Exception e) { writer.println("CLASSIFIER: " + classifier.getName() + "\n DATASET: " + dataset.getName() + "\n PARAMETERS: " + temp); } //Add evaluation string to file writer.println(eval.toSummaryString()); //Process result options if (checkResults("stats")) { //Classifier statistics writer.println(eval.toClassDetailsString()); } if (checkResults("model")) { //The model writer.println(x.toString()); } if (checkResults("matrix")) { //Confusion matrix writer.println(eval.toMatrixString()); } if (checkResults("entropy")) { //Entropy statistics //Set options req'd to get the entropy stats String[] opt = new String[4]; opt[0] = "-t"; opt[1] = dataset.getName(); opt[2] = "-k"; opt[3] = "-v"; //Evaluate model String entropy = Evaluation.evaluateModel(x, opt); //Grab the relevant info from the results, print to file entropy = entropy.substring(entropy.indexOf("=== Stratified cross-validation ===") + 35, entropy.indexOf("=== Confusion Matrix ===")); writer.println("=== Entropy Statistics ==="); writer.println(entropy); } if (checkResults("predictions")) { //The models predictions writer.println("=== Predictions ===\n"); if (!dataset.getTest().contains("xval")) { //print header of predictions table if req'd writer.println(" inst# actual predicted error distribution ()"); } writer.println(predictions.toString()); //print predictions to file } writer.close(); //Summary file is semaphore controlled to ensure quality try { //get a permit //grab the summary file, write the classifiers details to it FlexDM.writeFile.acquire(); PrintWriter p = new PrintWriter(new FileWriter(summary, true)); if (temp.equals("results_no_parameters")) { //change output based on parameters temp = temp.substring(8); } //write percent correct, classifier name, dataset name to summary file p.write(dataset.getName() + ", " + classifier.getName() + ", " + temp + ", " + eval.correct() + ", " + eval.incorrect() + ", " + eval.unclassified() + ", " + eval.pctCorrect() + ", " + eval.pctIncorrect() + ", " + eval.pctUnclassified() + ", " + eval.kappa() + ", " + eval.meanAbsoluteError() + ", " + eval.rootMeanSquaredError() + ", " + eval.relativeAbsoluteError() + ", " + eval.rootRelativeSquaredError() + ", " + eval.SFPriorEntropy() + ", " + eval.SFSchemeEntropy() + ", " + eval.SFEntropyGain() + ", " + eval.SFMeanPriorEntropy() + ", " + eval.SFMeanSchemeEntropy() + ", " + eval.SFMeanEntropyGain() + ", " + eval.KBInformation() + ", " + eval.KBMeanInformation() + ", " + eval.KBRelativeInformation() + ", " + eval.weightedTruePositiveRate() + ", " + eval.weightedFalsePositiveRate() + ", " + eval.weightedTrueNegativeRate() + ", " + eval.weightedFalseNegativeRate() + ", " + eval.weightedPrecision() + ", " + eval.weightedRecall() + ", " + eval.weightedFMeasure() + ", " + eval.weightedAreaUnderROC() + "\n"); p.close(); //release semaphore FlexDM.writeFile.release(); } catch (InterruptedException e) { //bad things happened System.err.println("FATAL ERROR OCCURRED: Classifier: " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName()); } //output we have successfully finished processing classifier if (temp.equals("no_parameters")) { //no parameters try { System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1) + " with no parameters"); } catch (Exception e) { System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName() + " with no parameters"); } } else { //with parameters try { System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1) + " with parameters " + temp); } catch (Exception e) { System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName() + " with parameters " + temp); } } try { //get a permit //grab the log file, write the classifiers details to it FlexDM.writeLog.acquire(); PrintWriter p = new PrintWriter(new FileWriter(log, true)); Date date = new Date(); Format formatter = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss"); //formatter.format(date) if (temp.equals("results_no_parameters")) { //change output based on parameters temp = temp.substring(8); } //write details to log file p.write(dataset.getName() + ", " + dataset.getTest() + ", \"" + dataset.getResult_string() + "\", " + classifier.getName() + ", " + temp + ", " + formatter.format(date) + "\n"); p.close(); //release semaphore FlexDM.writeLog.release(); } catch (InterruptedException e) { //bad things happened System.err.println("FATAL ERROR OCCURRED: Classifier: " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName()); } s.release(); } catch (Exception e) { //an error occurred System.err.println("FATAL ERROR OCCURRED: " + e.toString() + "\nClassifier: " + cNum + " - " + classifier.getName() + " on dataset " + dataset.getName()); s.release(); } }
From source file:CrossValidationMultipleRuns.java
License:Open Source License
/** * Performs the cross-validation. See Javadoc of class for information * on command-line parameters./* w w w .j ava 2s.com*/ * * @param args the command-line parameters * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { // loads data and set class index Instances data = DataSource.read(Utils.getOption("t", args)); String clsIndex = Utils.getOption("c", args); if (clsIndex.length() == 0) clsIndex = "last"; if (clsIndex.equals("first")) data.setClassIndex(0); else if (clsIndex.equals("last")) data.setClassIndex(data.numAttributes() - 1); else data.setClassIndex(Integer.parseInt(clsIndex) - 1); // classifier String[] tmpOptions; String classname; tmpOptions = Utils.splitOptions(Utils.getOption("W", args)); classname = tmpOptions[0]; tmpOptions[0] = ""; Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions); // other options int runs = Integer.parseInt(Utils.getOption("r", args)); int folds = Integer.parseInt(Utils.getOption("x", args)); // perform cross-validation for (int i = 0; i < runs; i++) { // randomize data int seed = i + 1; Random rand = new Random(seed); Instances randData = new Instances(data); randData.randomize(rand); //if (randData.classAttribute().isNominal()) // randData.stratify(folds); Evaluation eval = new Evaluation(randData); StringBuilder optionsString = new StringBuilder(); for (String s : cls.getOptions()) { optionsString.append(s); optionsString.append(" "); } // output evaluation System.out.println(); System.out.println("=== Setup run " + (i + 1) + " ==="); System.out.println("Classifier: " + optionsString.toString()); System.out.println("Dataset: " + data.relationName()); System.out.println("Folds: " + folds); System.out.println("Seed: " + seed); System.out.println(); for (int n = 0; n < folds; n++) { Instances train = randData.trainCV(folds, n); Instances test = randData.testCV(folds, n); // build and evaluate classifier Classifier clsCopy = Classifier.makeCopy(cls); clsCopy.buildClassifier(train); eval.evaluateModel(clsCopy, test); System.out.println(eval.toClassDetailsString()); } System.out.println( eval.toSummaryString("=== " + folds + "-fold Cross-validation run " + (i + 1) + " ===", false)); } }
From source file:adams.flow.transformer.WekaEvaluationSummary.java
License:Open Source License
/** * Executes the flow item./* w ww . j a va 2s . c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Evaluation eval; StringBuilder buffer; boolean prolog; String[] comment; result = null; if (m_InputToken.getPayload() instanceof WekaEvaluationContainer) eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); else eval = (Evaluation) m_InputToken.getPayload(); buffer = new StringBuilder(); prolog = false; // comments if (m_Comment.getValue().length() > 0) { comment = m_Comment.getValue().split("\n"); if (comment.length == 1) { buffer.append("Comment: " + m_Comment + "\n"); } else { buffer.append("Comment:\n"); for (String line : comment) buffer.append(line + "\n"); } prolog = true; } // relation name if (m_OutputRelationName) { buffer.append("Relation: " + eval.getHeader().relationName() + "\n"); prolog = true; } // separator if (prolog) buffer.append("\n"); // summary if (m_TitleSummary.isEmpty()) buffer.append(eval.toSummaryString(m_ComplexityStatistics)); else buffer.append(eval.toSummaryString(Utils.unbackQuoteChars(m_TitleSummary), m_ComplexityStatistics)); // confusion matrix if (m_ConfusionMatrix) { try { buffer.append("\n\n"); if (m_TitleMatrix.isEmpty()) buffer.append(eval.toMatrixString()); else buffer.append(eval.toMatrixString(Utils.unbackQuoteChars(m_TitleMatrix))); } catch (Exception e) { result = handleException("Failed to generate confusion matrix: ", e); } } // class details if (m_ClassDetails) { try { buffer.append("\n\n"); if (m_TitleClassDetails.isEmpty()) buffer.append(eval.toClassDetailsString()); else buffer.append(eval.toClassDetailsString(Utils.unbackQuoteChars(m_TitleClassDetails))); } catch (Exception e) { result = handleException("Failed to generate class details: ", e); } } m_OutputToken = new Token(buffer.toString()); return result; }
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 w ww . jav a 2 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()); */ }
From source file:boostingPL.boosting.SAMME.java
License:Open Source License
public static void main(String[] args) throws Exception { java.io.File inputFile = new java.io.File(args[0]); ArffLoader atf = new ArffLoader(); atf.setFile(inputFile);//from w w w. j av a2 s. co m Instances training = atf.getDataSet(); training.setClassIndex(training.numAttributes() - 1); //Instances testing = new Instances(training); int iterationNum = 100; SAMME samme = new SAMME(training, iterationNum); for (int t = 0; t < iterationNum; t++) { samme.run(t); } java.io.File inputFilet = new java.io.File(args[1]); 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(samme, inst); } System.out.println(eval.toSummaryString()); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); }
From source file:clases.Resultados.java
public static void imprimirResultados(Evaluation evaluador) { try {//from w ww .j a va2 s . c om System.out.println("=================================================="); System.out.println("Las figuras de mrito del clasificador ptimo son:"); System.out.println("=================================================="); System.out.println(evaluador.toSummaryString()); System.out.println(evaluador.toClassDetailsString()); System.out.println(evaluador.toMatrixString()); } catch (Exception ex) { System.out.println("Error al mostrar los resultados: " + ex); } }
From source file:com.deafgoat.ml.prognosticator.AppClassifier.java
License:Apache License
/** * Evaluates model performance on test instances * //w w w. j a va2 s . c om * @throws Exception * If model can not be evaluated. */ public void evaluate() throws Exception { readModel(); _logger.info("Classifying with " + _config._classifier); Evaluation eval = new Evaluation(_testInstances); eval.evaluateModel(_cls, _testInstances); _logger.info("\n" + eval.toSummaryString()); try { _logger.info("\n" + eval.toClassDetailsString()); } catch (Exception e) { _logger.info("Can not create class details" + _config._classifier); } try { _logger.info("\n" + _eval.toMatrixString()); } catch (Exception e) { _logger.info( "Can not create confusion matrix for " + _config._classifier + " using " + _config._classValue); } }
From source file:com.deafgoat.ml.prognosticator.AppClassifier.java
License:Apache License
/** * Write results to mongoDB/* w ww . jav a 2 s . c o m*/ * * @param eval * The evaluation object holding data. * @throws Exception */ public void writeToMongoDB(Evaluation eval) throws Exception { MongoResult mongoResult = new MongoResult(_config._host, _config._port, _config._db, _config._modelCollection); mongoResult.writeExperiment(_config._relation, "summary", eval.toSummaryString()); try { mongoResult.writeExperiment(_config._relation, "class detail", eval.toClassDetailsString()); } catch (Exception e) { _logger.error("Can not create class details" + _config._classifier); } try { mongoResult.writeExperiment(_config._relation, "confusion matrix", eval.toMatrixString()); } catch (Exception e) { _logger.error("Can not create confusion matrix for " + _config._classifier); } mongoResult.close(); }
From source file:com.ivanrf.smsspam.SpamClassifier.java
License:Apache License
public static void evaluate(int wordsToKeep, String tokenizerOp, boolean useAttributeSelection, String classifierOp, boolean boosting, JTextArea log) { try {/*from ww w . jav a2s .co m*/ long start = System.currentTimeMillis(); String modelName = getModelName(wordsToKeep, tokenizerOp, useAttributeSelection, classifierOp, boosting); showEstimatedTime(false, modelName, log); Instances trainData = loadDataset("SMSSpamCollection.arff", log); trainData.setClassIndex(0); FilteredClassifier classifier = initFilterClassifier(wordsToKeep, tokenizerOp, useAttributeSelection, classifierOp, boosting); publishEstado("=== Performing cross-validation ===", log); Evaluation eval = new Evaluation(trainData); // eval.evaluateModel(classifier, trainData); eval.crossValidateModel(classifier, trainData, 10, new Random(1)); publishEstado(eval.toSummaryString(), log); publishEstado(eval.toClassDetailsString(), log); publishEstado(eval.toMatrixString(), log); publishEstado("=== Evaluation finished ===", log); publishEstado("Elapsed time: " + Utils.getDateHsMinSegString(System.currentTimeMillis() - start), log); } catch (Exception e) { e.printStackTrace(); publishEstado("Error found when evaluating", log); } }
From source file:cyber009.main.UDALNeuralNetwork.java
public static void main(String[] args) { UDALNeuralNetwork udal = new UDALNeuralNetwork(0.014013); Statistics statis = new Statistics(udal.v); long timeStart = 0, timeEnd = 0; for (int f = 2; f <= 2; f++) { udal.initUDAL(4, 5000);/*from w w w. j a v a 2 s . co m*/ udal.activeLearning(0, 5000); udal.arraytoInstances(); udal.ann.weightReset(); timeStart = System.currentTimeMillis(); MultilayerPerceptron wekaNN = new MultilayerPerceptron(); wekaNN.setAutoBuild(true); //wekaNN.setGUI(true); try { wekaNN.buildClassifier(udal.dataSet); Evaluation eval = new Evaluation(udal.dataSet); System.out.println(wekaNN.toString()); eval.crossValidateModel(wekaNN, udal.dataSet, 4999, new Random(System.currentTimeMillis())); System.out.println(wekaNN.toString()); System.out.println(eval.toClassDetailsString()); // udal.ann.gradientDescent(10000L, 3, 100); // for (Double target : udal.v.CLASSES) { // statis.calMVMuSigma(target); // System.out.println(udal.v.N_DATA_IN_CLASS.get(target)); // System.out.println(statis.mu.get(target)); // System.out.println(statis.sigma.get(target)); // } // for(int d=0; d<udal.v.D; d++) { // if(udal.v.LABEL[d] == false) { // double [][] val = new double[udal.v.N-1][1]; // for(int n=1; n<udal.v.N; n++) { // val[n-1][0] = udal.v.X[d][n]; //// System.out.print(udal.v.X[d][n] + " "); //// System.out.println(val[n-1][0]); // } // Matrix mVal = new Matrix(val); // double pp = 0.0D; // for (Double target : udal.v.CLASSES) { // //System.out.println("-----------------------\nClass:"+ target); // pp += statis.posteriorDistribution(target, mVal); // System.out.println("conditional: Entropy: "+ // statis.conditionalEntropy(target, mVal, d)); // } // System.out.print("Sum posterior:"+ pp+ " for "+new Matrix(val).transpose()); // // } // } // System.out.println("-----------------------"); // timeEnd = System.currentTimeMillis(); // System.out.println("feature #:"+udal.v.N+" time:("+ (timeEnd - timeStart) +")"); // udal.v.showResult(); // } catch (Exception ex) { Logger.getLogger(UDALNeuralNetwork.class.getName()).log(Level.SEVERE, null, ex); } } }