List of usage examples for weka.classifiers Evaluation crossValidateModel
public void crossValidateModel(Classifier classifier, Instances data, int numFolds, Random random) throws Exception
From source file:trabfs.machineLeaningFrameWork.core.AvaliadordeSolucao.java
public double avalia(Solucao s) { double precision = 0.0, c; try {/*w w w . ja v a 2s. co m*/ // cria vetor de indices dos atributos selecionados int[] toremove = makeIndex(s); //remove atributos nao selecionados Remove remove = new Remove(); remove.setAttributeIndicesArray(toremove); remove.setInvertSelection(true); remove.setInputFormat(p.getInstances()); Instances subproblema = Filter.useFilter(p.getInstances(), remove); subproblema.setClassIndex(subproblema.numAttributes() - 1); // classifica e pega o resultado Random rand = new Random(1); // create seeded number generator IBk clf = new IBk(K); //SimpleNaiveBayes clf = new SimpleNaiveBayes(); //NaiveBayesSimple clf = new NaiveBayesSimple(); //clf.buildClassifier(subproblema); Evaluation eval = new Evaluation(subproblema); eval.crossValidateModel(clf, subproblema, nfolds, rand); precision = (double) eval.correct() / subproblema.numInstances(); calls++; } catch (Exception ex) { Logger.getLogger(AvaliadordeSolucao.class.getName()).log(Level.SEVERE, null, ex); } s.setQuality(precision); if (precision > this.best) { this.best = precision; } evolucao.add(this.best); return s.getQuality(); }
From source file:tucil.dua.ai.TucilDuaAi.java
public static void crossValidation() throws Exception { Evaluation evaluation = new Evaluation(datas); Classifier attr_tree = new J48(); attr_tree.buildClassifier(datas);/*from w ww .j a va 2s . c o m*/ evaluation.crossValidateModel(attr_tree, datas, 10, new Random(1)); System.out.println("=====Run Information======"); System.out.println("======Classifier Model======"); System.out.println(attr_tree.toString()); System.out.println(evaluation.toSummaryString("====Stats======\n", false)); System.out.println(evaluation.toClassDetailsString("====Detailed Result=====\n")); System.out.println(evaluation.toMatrixString("======Confusion Matrix======\n")); }
From source file:tucil2ai.Tucil2AI.java
/** * * @param cls//from w ww.ja v a 2 s . co m * @param data * @param cross * @return * @throws Exception */ protected static Evaluation evalJ48(Classifier cls, Instances data, boolean cross) throws Exception { Evaluation E; E = new Evaluation(data); if (cross == false) { E.evaluateModel(cls, data); } else { E.crossValidateModel(cls, data, 10, new Random(0x100)); /*crossValidateModel*/ } return E; }
From source file:util.FeatureExtract.java
public static void createArff(String directory) { TextDirectoryLoader loader = new TextDirectoryLoader(); try {//from w w w. j a va 2 s.c om // convert the directory into a dataset loader.setDirectory(new File(directory)); Instances dataRaw = loader.getDataSet(); // apply the StringToWordVector and tf-idf weighting StringToWordVector filter = new StringToWordVector(); filter.setIDFTransform(true); filter.setInputFormat(dataRaw); Instances dataFiltered = Filter.useFilter(dataRaw, filter); // output the arff file ArffSaver saver = new ArffSaver(); saver.setInstances(dataFiltered); saver.setFile(new File(SpamFilterConfig.getArffFilePath())); saver.writeBatch(); // train with simple cart SimpleCart classifier = new SimpleCart(); classifier.buildClassifier(dataFiltered); System.out.println("\n\nClassifier model:\n\n" + classifier.toString()); // using 10 cross validation Evaluation eval = new Evaluation(dataFiltered); eval.crossValidateModel(classifier, dataFiltered, 10, new Random(1)); System.out.println("\n\nCross fold:\n\n" + eval.toSummaryString()); } catch (Exception ex) { Logger.getLogger(FeatureExtract.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:util.Weka.java
public void fijarAprendizaje(Instance casoAdecidir, double resultadoRealObservado) { casoAdecidir.setClassValue(resultadoRealObservado); casosEntrenamiento.add(casoAdecidir); for (int i = 0; i < casosEntrenamiento.numInstances() - this.maximoNumeroCasosEntrenamiento; i++) { casosEntrenamiento.delete(0); //Si hay muchos ejemplos borrar el ms antiguo }/*w w w .ja v a 2 s.com*/ try { conocimiento.buildClassifier(casosEntrenamiento); Evaluation evaluador = new Evaluation(casosEntrenamiento); evaluador.crossValidateModel(conocimiento, casosEntrenamiento, 10, new Random(1)); } catch (Exception e) { // meter el error al log } }
From source file:wekimini.learning.ModelEvaluator.java
public void evaluateAll(final List<Path> paths, final boolean isTraining, final int numFolds, PropertyChangeListener listener) { final List<Instances> data = new LinkedList<>(); for (Path p : paths) { Instances i = w.getSupervisedLearningManager().getTrainingDataForPath(p, false); data.add(i);//ww w .ja v a2 s . co m } setResults(new String[paths.size()]); if (evalWorker != null && evalWorker.getState() != SwingWorker.StateValue.DONE) { return; } evalWorker = new SwingWorker<Integer, Void>() { //trainingWorker. @Override public Integer doInBackground() { // train(); //TODO: Add status updates int progress = 0; //setProgress(progress); int numToEvaluate = 0; for (Path p : paths) { if (p.canBuild()) { numToEvaluate++; } } int numEvaluated = 0; int numErr = 0; setEvalStatus(new EvaluationStatus(numToEvaluate, numEvaluated, numErr, false)); for (int i = 0; i < paths.size(); i++) { Path p = paths.get(i); if (p.canBuild()) { try { System.out.println("Evaluating with " + numFolds); //EVALUATE HERE: TODO Instances instances = w.getSupervisedLearningManager().getTrainingDataForPath(p, false); Evaluation eval = new Evaluation(instances); Classifier c = ((LearningModelBuilder) p.getModelBuilder()).getClassifier(); if (!isTraining) { Random r = new Random(); eval.crossValidateModel(c, instances, numFolds, r); } else { Classifier c2 = Classifier.makeCopy(c); c2.buildClassifier(instances); eval.evaluateModel(c2, instances); } String result; if (p.getModelBuilder() instanceof ClassificationModelBuilder) { result = dFormat.format(eval.pctCorrect()) + "%"; //WON"T WORK FOR NN } else { result = dFormat.format(eval.errorRate()) + " (RMS)"; } if (!isTraining) { KadenzeLogging.getLogger().crossValidationComputed(w, i, numFolds, result); } else { KadenzeLogging.getLogger().trainingAccuracyComputed(w, i, result); } setResults(i, result); finishedModel(i, result); numEvaluated++; if (isCancelled()) { cancelMe(p); setResults(i, "Cancelled"); return 0; } } catch (InterruptedException ex) { cancelMe(p); setResults(i, "Cancelled"); return 0; //Not sure this will be called... } catch (Exception ex) { numErr++; Util.showPrettyErrorPane(null, "Error encountered during evaluation " + p.getCurrentModelName() + ": " + ex.getMessage()); logger.log(Level.SEVERE, ex.getMessage()); } setEvalStatus(new EvaluationStatus(numToEvaluate, numEvaluated, numErr, false)); } else { logger.log(Level.WARNING, "Could not evaluate path"); } } wasCancelled = false; hadError = evaluationStatus.numErrorsEncountered > 0; return 0; } @Override public void done() { if (isCancelled()) { EvaluationStatus t = new EvaluationStatus(evaluationStatus.numToEvaluate, evaluationStatus.numEvaluated, evaluationStatus.numErrorsEncountered, true); setEvalStatus(t); } finished(); } }; evalWorker.addPropertyChangeListener(listener); evalWorker.execute(); }