List of usage examples for weka.classifiers Evaluation Evaluation
public Evaluation(Instances data) throws Exception
From source file:meddle.TrainModelByDomainOS.java
License:Open Source License
/** * Do evalution on trained classifier/model, including the summary, false * positive/negative rate, AUC, running time * * @param j48//www .jav a 2s .c o m * - the trained classifier * @param domain * - the domain name */ public static MetaEvaluationMeasures doEvaluation(Classifier classifier, String domainOS, Instances tras, MetaEvaluationMeasures mem) { try { Evaluation evaluation = new Evaluation(tras); evaluation.crossValidateModel(classifier, tras, 10, new Random(1)); mem.numInstance = evaluation.numInstances(); double M = evaluation.numTruePositives(1) + evaluation.numFalseNegatives(1); mem.numPositive = (int) M; mem.AUC = evaluation.areaUnderROC(1); mem.numCorrectlyClassified = (int) evaluation.correct(); mem.accuracy = 1.0 * mem.numCorrectlyClassified / mem.numInstance; mem.falseNegativeRate = evaluation.falseNegativeRate(1); mem.falsePositiveRate = evaluation.falsePositiveRate(1); mem.fMeasure = evaluation.fMeasure(1); double[][] cmMatrix = evaluation.confusionMatrix(); mem.confusionMatrix = cmMatrix; mem.TP = evaluation.numTruePositives(1); mem.TN = evaluation.numTrueNegatives(1); mem.FP = evaluation.numFalsePositives(1); mem.FN = evaluation.numFalseNegatives(1); } catch (Exception e) { e.printStackTrace(); } return mem; }
From source file:miRdup.WekaModule.java
License:Open Source License
public static void trainModel(File arff, String keyword) { dec.setMaximumFractionDigits(3);//from w w w . java 2 s .c om System.out.println("\nTraining model on file " + arff); try { // load data DataSource source = new DataSource(arff.toString()); Instances data = source.getDataSet(); if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } PrintWriter pwout = new PrintWriter(new FileWriter(keyword + Main.modelExtension + "Output")); PrintWriter pwroc = new PrintWriter(new FileWriter(keyword + Main.modelExtension + "roc.arff")); //remove ID row Remove rm = new Remove(); rm.setAttributeIndices("1"); FilteredClassifier fc = new FilteredClassifier(); fc.setFilter(rm); // // train model svm // weka.classifiers.functions.LibSVM model = new weka.classifiers.functions.LibSVM(); // model.setOptions(weka.core.Utils.splitOptions("-S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.0010 -P 0.1 -B")); // train model MultilayerPerceptron // weka.classifiers.functions.MultilayerPerceptron model = new weka.classifiers.functions.MultilayerPerceptron(); // model.setOptions(weka.core.Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a")); // train model Adaboost on RIPPER // weka.classifiers.meta.AdaBoostM1 model = new weka.classifiers.meta.AdaBoostM1(); // model.setOptions(weka.core.Utils.splitOptions("weka.classifiers.meta.AdaBoostM1 -P 100 -S 1 -I 10 -W weka.classifiers.rules.JRip -- -F 10 -N 2.0 -O 5 -S 1")); // train model Adaboost on FURIA // weka.classifiers.meta.AdaBoostM1 model = new weka.classifiers.meta.AdaBoostM1(); // model.setOptions(weka.core.Utils.splitOptions("weka.classifiers.meta.AdaBoostM1 -P 100 -S 1 -I 10 -W weka.classifiers.rules.FURIA -- -F 10 -N 2.0 -O 5 -S 1 -p 0 -s 0")); //train model Adaboot on J48 trees // weka.classifiers.meta.AdaBoostM1 model = new weka.classifiers.meta.AdaBoostM1(); // model.setOptions( // weka.core.Utils.splitOptions( // "-P 100 -S 1 -I 10 -W weka.classifiers.trees.J48 -- -C 0.25 -M 2")); //train model Adaboot on Random Forest trees weka.classifiers.meta.AdaBoostM1 model = new weka.classifiers.meta.AdaBoostM1(); model.setOptions(weka.core.Utils .splitOptions("-P 100 -S 1 -I 10 -W weka.classifiers.trees.RandomForest -- -I 50 -K 0 -S 1")); if (Main.debug) { System.out.print("Model options: " + model.getClass().getName().trim() + " "); } System.out.print(model.getClass() + " "); for (String s : model.getOptions()) { System.out.print(s + " "); } pwout.print("Model options: " + model.getClass().getName().trim() + " "); for (String s : model.getOptions()) { pwout.print(s + " "); } //build model // model.buildClassifier(data); fc.setClassifier(model); fc.buildClassifier(data); // cross validation 10 times on the model Evaluation eval = new Evaluation(data); //eval.crossValidateModel(model, data, 10, new Random(1)); StringBuffer sb = new StringBuffer(); eval.crossValidateModel(fc, data, 10, new Random(1), sb, new Range("first,last"), false); //System.out.println(sb); pwout.println(sb); pwout.flush(); // output pwout.println("\n" + eval.toSummaryString()); System.out.println(eval.toSummaryString()); pwout.println(eval.toClassDetailsString()); System.out.println(eval.toClassDetailsString()); //calculate importants values String ev[] = eval.toClassDetailsString().split("\n"); String ptmp[] = ev[3].trim().split(" "); String ntmp[] = ev[4].trim().split(" "); String avgtmp[] = ev[5].trim().split(" "); ArrayList<String> p = new ArrayList<String>(); ArrayList<String> n = new ArrayList<String>(); ArrayList<String> avg = new ArrayList<String>(); for (String s : ptmp) { if (!s.trim().isEmpty()) { p.add(s); } } for (String s : ntmp) { if (!s.trim().isEmpty()) { n.add(s); } } for (String s : avgtmp) { if (!s.trim().isEmpty()) { avg.add(s); } } double tp = Double.parseDouble(p.get(0)); double fp = Double.parseDouble(p.get(1)); double tn = Double.parseDouble(n.get(0)); double fn = Double.parseDouble(n.get(1)); double auc = Double.parseDouble(avg.get(7)); pwout.println("\nTP=" + tp + "\nFP=" + fp + "\nTN=" + tn + "\nFN=" + fn); System.out.println("\nTP=" + tp + "\nFP=" + fp + "\nTN=" + tn + "\nFN=" + fn); //specificity, sensitivity, Mathew's correlation, Prediction accuracy double sp = ((tn) / (tn + fp)); double se = ((tp) / (tp + fn)); double acc = ((tp + tn) / (tp + tn + fp + fn)); double mcc = ((tp * tn) - (fp * fn)) / Math.sqrt((tp + fp) * (tn + fn) * (tp + fn) * tn + fp); String output = "\nse=" + dec.format(se).replace(",", ".") + "\nsp=" + dec.format(sp).replace(",", ".") + "\nACC=" + dec.format(acc).replace(",", ".") + "\nMCC=" + dec.format(mcc).replace(",", ".") + "\nAUC=" + dec.format(auc).replace(",", "."); pwout.println(output); System.out.println(output); pwout.println(eval.toMatrixString()); System.out.println(eval.toMatrixString()); pwout.flush(); pwout.close(); //Saving model System.out.println("Model saved: " + keyword + Main.modelExtension); weka.core.SerializationHelper.write(keyword + Main.modelExtension, fc.getClassifier() /*model*/); // get curve ThresholdCurve tc = new ThresholdCurve(); int classIndex = 0; Instances result = tc.getCurve(eval.predictions(), classIndex); pwroc.print(result.toString()); pwroc.flush(); pwroc.close(); // draw curve //rocCurve(eval); } catch (Exception e) { e.printStackTrace(); } }
From source file:miRdup.WekaModule.java
License:Open Source License
public static void testModel(File testarff, String predictionsFile, String classifier, boolean predictMiRNA) { System.out.println("Testing model on " + predictionsFile + " adapted in " + testarff + ". Submitted to model " + classifier); try {/*from w w w .j av a 2 s . c o m*/ //add predictions sequences to object ArrayList<MirnaObject> alobj = new ArrayList<MirnaObject>(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(predictionsFile + ".folded")); } catch (FileNotFoundException fileNotFoundException) { br = new BufferedReader(new FileReader(predictionsFile)); } BufferedReader br2 = new BufferedReader(new FileReader(testarff)); String line2 = br2.readLine(); while (!line2.startsWith("@data")) { line2 = br2.readLine(); } String line = " "; int cpt = 0; while (br.ready()) { line = br.readLine(); line2 = br2.readLine(); String[] tab = line.split("\t"); MirnaObject m = new MirnaObject(); m.setArff(line2); m.setId(cpt++); m.setIdName(tab[0]); m.setMatureSequence(tab[1]); m.setPrecursorSequence(tab[2]); m.setStructure(tab[3]); alobj.add(m); } br.close(); br2.close(); // load data DataSource source = new DataSource(testarff.toString()); Instances data = source.getDataSet(); if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } //remove ID row data.deleteAttributeAt(0); //load model Classifier model = (Classifier) weka.core.SerializationHelper.read(classifier); // evaluate dataset on the model Evaluation eval = new Evaluation(data); eval.evaluateModel(model, data); FastVector fv = eval.predictions(); // output PrintWriter pw = new PrintWriter(new FileWriter(predictionsFile + "." + classifier + ".miRdup.txt")); PrintWriter pwt = new PrintWriter( new FileWriter(predictionsFile + "." + classifier + ".miRdup.tab.txt")); PrintWriter pwout = new PrintWriter( new FileWriter(predictionsFile + "." + classifier + ".miRdupOutput.txt")); for (int i = 0; i < fv.size(); i++) { //System.out.println(fv.elementAt(i).toString()); String[] tab = fv.elementAt(i).toString().split(" "); int actual = Integer.valueOf(tab[1].substring(0, 1)); int predicted = Integer.valueOf(tab[2].substring(0, 1)); double score = 0.0; boolean validated = false; if (actual == predicted) { //case validated int s = tab[4].length(); try { score = Double.valueOf(tab[4]); //score = Double.valueOf(tab[4].substring(0, s - 1)); } catch (NumberFormatException numberFormatException) { score = 0.0; } validated = true; } else {// case not validated int s = tab[5].length(); try { score = Double.valueOf(tab[5]); //score = Double.valueOf(tab[5].substring(0, s - 1)); } catch (NumberFormatException numberFormatException) { score = 0.0; } validated = false; } MirnaObject m = alobj.get(i); m.setActual(actual); m.setPredicted(predicted); m.setScore(score); m.setValidated(validated); m.setNeedPrediction(predictMiRNA); String predictionMiRNA = ""; if (predictMiRNA && validated == false) { predictionMiRNA = miRdupPredictor.Predictor.predictionBySequence(m.getPrecursorSequence(), classifier, classifier + ".miRdupPrediction.txt"); try { m.setPredictedmiRNA(predictionMiRNA.split(",")[0]); m.setPredictedmiRNAstar(predictionMiRNA.split(",")[1]); } catch (Exception e) { m.setPredictedmiRNA(predictionMiRNA); m.setPredictedmiRNAstar(predictionMiRNA); } } pw.println(m.toStringFullPredictions()); pwt.println(m.toStringPredictions()); if (i % 100 == 0) { pw.flush(); pwt.flush(); } } //System.out.println(eval.toSummaryString("\nSummary results of predictions\n======\n", false)); String[] out = eval.toSummaryString("\nSummary results of predictions\n======\n", false).split("\n"); String info = out[0] + "\n" + out[1] + "\n" + out[2] + "\n" + out[4] + "\n" + out[5] + "\n" + out[6] + "\n" + out[7] + "\n" + out[11] + "\n"; System.out.println(info); //System.out.println("Predicted position of the miRNA by miRdup:"+predictionMiRNA); pwout.println( "File " + predictionsFile + " adapted in " + testarff + " submitted to model " + classifier); pwout.println(info); pw.flush(); pw.close(); pwt.flush(); pwt.close(); pwout.flush(); pwout.close(); System.out.println("Results in " + predictionsFile + "." + classifier + ".miRdup.txt"); // draw curve //rocCurve(eval); } catch (Exception e) { e.printStackTrace(); } }
From source file:miRdup.WekaModule.java
License:Open Source License
public static String testModel(File testarff, String classifier) { // System.out.println("Testing model on "+testarff+". Submitted to model "+classifier); try {/*from www . j ava 2 s .c o m*/ // load data DataSource source = new DataSource(testarff.toString()); Instances data = source.getDataSet(); if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } //load model Classifier model = (Classifier) weka.core.SerializationHelper.read(classifier); // evaluate dataset on the model Evaluation eval = new Evaluation(data); eval.evaluateModel(model, data); FastVector fv = eval.predictions(); //calculate importants values String ev[] = eval.toClassDetailsString().split("\n"); String p = ev[3].trim(); String n = ev[4].trim(); double tp = Double.parseDouble(p.substring(0, 6).trim()); double fp = 0; try { fp = Double.parseDouble(p.substring(11, 16).trim()); } catch (Exception exception) { fp = Double.parseDouble(p.substring(7, 16).trim()); } double tn = Double.parseDouble(n.substring(0, 6).trim()); double fn = 0; try { fn = Double.parseDouble(n.substring(11, 16).trim()); } catch (Exception exception) { fn = Double.parseDouble(n.substring(7, 16).trim()); } //System.out.println("\nTP="+tp+"\nFP="+fp+"\nTN="+tn+"\nFN="+fn); //specificity, sensitivity, Mathew's correlation, Prediction accuracy double sp = ((tn) / (tn + fp)); double se = ((tp) / (tp + fn)); double acc = ((tp + tn) / (tp + tn + fp + fn)); double mcc = ((tp * tn) - (fp * fn)) / Math.sqrt((tp + fp) * (tn + fn) * (tp + fn) * tn + fp); // System.out.println("\nse="+se+"\nsp="+sp+"\nACC="+dec.format(acc).replace(",", ".")+"\nMCC="+dec.format(mcc).replace(",", ".")); // System.out.println(eval.toMatrixString()); String out = dec.format(acc).replace(",", "."); System.out.println(out); return out; } catch (Exception e) { e.printStackTrace(); return ""; } }
From source file:ml.ann.MainDriver.java
public static void testModel() { System.out.println("## Pilih bahan testing"); System.out.println("## 1. Uji dengan data dari masukan training"); System.out.println("## 2. Uji dengan data data masukan baru"); System.out.print("## > "); int choice = (new Scanner(System.in)).nextInt(); if (choice == 1) { try {/*from w w w .j av a 2 s .co m*/ Evaluation eval = new Evaluation(train); if (cv10) { eval.crossValidateModel(model, test, 10, new Random(1)); } else { eval.evaluateModel(model, test); } System.out.println(eval.toSummaryString()); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); } catch (Exception E) { E.printStackTrace(); } } else if (choice == 2) { try { loadTestData(); Evaluation eval = new Evaluation(train); if (cv10) { eval.crossValidateModel(model, test, 10, new Random(1)); } else { eval.evaluateModel(model, test); } System.out.println(eval.toSummaryString()); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); } catch (Exception E) { E.printStackTrace(); } } }
From source file:ml.ann.MainPTR.java
public static void main(String[] args) throws FileNotFoundException, IOException, Exception { boolean randomWeight; double weightawal = 0.0; double learningRate = 0.0001; double threshold = 0.00; double momentum = 0.00; int maxEpoch = 100000; int nCrossValidate = 2; m_nominalToBinaryFilter = new NominalToBinary(); m_normalize = new Normalize(); Scanner in = new Scanner(System.in); System.out.println("Lokasi file: "); String filepath = in.nextLine(); filepath = "test-arffs/iris.arff"; System.out.println("--- Algoritma ---"); System.out.println("1. Perceptron Training Rule"); System.out.println("2. Delta Rule Incremental"); System.out.println("3. Delta Rule Batch"); System.out.println("Pilihan Algoritma (1/2/3) : "); int choice = in.nextInt(); String temp = in.nextLine();// ww w .j a v a2s. c om System.out.println("Apakah Anda ingin memasukkan nilai weight awal? (YES/NO)"); String isRandom = in.nextLine(); System.out.println("Apakah Anda ingin memasukkan konfigurasi? (YES/NO)"); String config = in.nextLine(); if (config.equalsIgnoreCase("yes")) { System.out.print("Masukkan nilai learning rate: "); learningRate = in.nextDouble(); System.out.print("Masukkan nilai threshold: "); threshold = in.nextDouble(); System.out.print("Masukkan nilai momentum: "); momentum = in.nextDouble(); System.out.print("Masukkan jumlah epoch: "); threshold = in.nextInt(); System.out.print("Masukkan jumlah folds untuk crossvalidate: "); nCrossValidate = in.nextInt(); } randomWeight = isRandom.equalsIgnoreCase("yes"); if (randomWeight) { System.out.print("Masukkan nilai weight awal: "); weightawal = Double.valueOf(in.nextLine()); } //print config if (isRandom.equalsIgnoreCase("yes")) { System.out.print("isRandom | "); } else { System.out.print("Weight " + weightawal + " | "); } System.out.print("L.rate " + learningRate + " | "); System.out.print("Max Epoch " + maxEpoch + " | "); System.out.print("Threshold " + threshold + " | "); System.out.print("Momentum " + momentum + " | "); System.out.print("Folds " + nCrossValidate + " | "); System.out.println(); FileReader trainreader = new FileReader(filepath); Instances train = new Instances(trainreader); train.setClassIndex(train.numAttributes() - 1); m_nominalToBinaryFilter.setInputFormat(train); train = new Instances(Filter.useFilter(train, m_nominalToBinaryFilter)); m_normalize.setInputFormat(train); train = new Instances(Filter.useFilter(train, m_normalize)); MultiClassPTR tempMulti = new MultiClassPTR(choice, randomWeight, learningRate, maxEpoch, threshold); tempMulti.buildClassifier(train); Evaluation eval = new Evaluation(new Instances(train)); eval.evaluateModel(tempMulti, train); System.out.println(eval.toSummaryString()); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); }
From source file:mlpoc.MLPOC.java
/** * uses the meta-classifier//from w w w . j av a2s .com */ protected static void useClassifier(Instances data) throws Exception { System.out.println("\n1. Meta-classfier"); AttributeSelectedClassifier classifier = new AttributeSelectedClassifier(); CfsSubsetEval eval = new CfsSubsetEval(); GreedyStepwise search = new GreedyStepwise(); search.setSearchBackwards(true); J48 base = new J48(); classifier.setClassifier(base); classifier.setEvaluator(eval); classifier.setSearch(search); Evaluation evaluation = new Evaluation(data); evaluation.crossValidateModel(classifier, data, 10, new Random(1)); System.out.println(evaluation.toSummaryString()); }
From source file:mlpoc.MLPOC.java
public static Evaluation crossValidate(String filename) { Evaluation eval = null;//from ww w.j av a2 s .c o m try { BufferedReader br = new BufferedReader(new FileReader(filename)); // loads data and set class index Instances data = new Instances(br); br.close(); /*File csv=new File(filename); CSVLoader loader = new CSVLoader(); loader.setSource(csv); Instances data = loader.getDataSet();*/ data.setClassIndex(data.numAttributes() - 1); // classifier String[] tmpOptions; String classname = "weka.classifiers.trees.J48 -C 0.25"; tmpOptions = classname.split(" "); classname = "weka.classifiers.trees.J48"; tmpOptions[0] = ""; Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions); // other options int seed = 2; int folds = 10; // randomize data Random rand = new Random(seed); Instances randData = new Instances(data); randData.randomize(rand); if (randData.classAttribute().isNominal()) randData.stratify(folds); // perform cross-validation eval = new Evaluation(randData); for (int n = 0; n < folds; n++) { Instances train = randData.trainCV(folds, n); Instances test = randData.testCV(folds, n); // the above code is used by the StratifiedRemoveFolds filter, the // code below by the Explorer/Experimenter: // Instances train = randData.trainCV(folds, n, rand); // build and evaluate classifier Classifier clsCopy = Classifier.makeCopy(cls); clsCopy.buildClassifier(train); eval.evaluateModel(clsCopy, test); } // output evaluation System.out.println(); System.out.println("=== Setup ==="); System.out .println("Classifier: " + cls.getClass().getName() + " " + Utils.joinOptions(cls.getOptions())); System.out.println("Dataset: " + data.relationName()); System.out.println("Folds: " + folds); System.out.println("Seed: " + seed); System.out.println(); System.out.println(eval.toSummaryString("Summary for testing", true)); System.out.println("Correctly Classified Instances: " + eval.correct()); System.out.println("Percentage of Correctly Classified Instances: " + eval.pctCorrect()); System.out.println("InCorrectly Classified Instances: " + eval.incorrect()); System.out.println("Percentage of InCorrectly Classified Instances: " + eval.pctIncorrect()); } catch (Exception ex) { System.err.println(ex.getMessage()); } return eval; }
From source file:model.clasification.klasifikacijaIstanca.java
public static void main(String[] args) throws Exception { // load data//from w w w . j av a2 s . c o m DataSource loader = new DataSource(fileName); Instances data = loader.getDataSet(); data.setClassIndex(data.numAttributes() - 1); // Create the Naive Bayes Classifier NaiveBayes bayesClsf = new NaiveBayes(); bayesClsf.buildClassifier(data); // output generated model // System.out.println(bayesClsf); // Test the model with the original set Evaluation eval = new Evaluation(data); eval.evaluateModel(bayesClsf, data); // Print the result as in Weka explorer String strSummary = eval.toSummaryString(); // System.out.println("=== Evaluation on training set ==="); // System.out.println("=== Summary ==="); // System.out.println(strSummary); // Get the confusion matrix System.out.println(eval.toMatrixString()); }
From source file:my.randomforestui.RandomForestUI.java
public static double doRandomForest(Instances training, Instances testing) throws Exception { double accuracy; //inisialisasi random forest String[] options = new String[1]; // set tree random forest unpruned tree options[0] = "-U"; // new instance of tree RandomForest tree = new RandomForest(); // set the options tree.setOptions(options);/*from ww w .ja v a2 s . c om*/ // build classifier using training data tree.buildClassifier(training); Evaluation eval = new Evaluation(testing); eval.evaluateModel(tree, testing); //System.out.println((eval.correct()/56)*100); accuracy = (eval.correct() / 56) * 100; return accuracy; }