From: <jen...@us...> - 2010-09-24 12:49:03
|
Revision: 2336 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=2336&view=rev Author: jenslehmann Date: 2010-09-24 12:48:56 +0000 (Fri, 24 Sep 2010) Log Message: ----------- cross validation slightly extended; pred. acc. approximation for class learning Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java trunk/scripts/src/main/java/org/dllearner/scripts/CrossValidation.java Modified: trunk/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java 2010-09-24 12:08:03 UTC (rev 2335) +++ trunk/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java 2010-09-24 12:48:56 UTC (rev 2336) @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Random; @@ -463,6 +464,62 @@ return heuristic.equals(HeuristicType.FMEASURE) ? getFMeasure(recall, precision) : Heuristics.getAScore(recall, precision, coverageFactor); + } else if(heuristic.equals(HeuristicType.FMEASURE)) { + int maxNotCovered = (int) Math.ceil(noise*classInstances.size()); + + int notCoveredPos = 0; +// int notCoveredNeg = 0; + + int posClassifiedAsPos = 0; + int negClassifiedAsNeg = 0; + + int nrOfPosChecks = 0; + int nrOfNegChecks = 0; + + // special case: we test positive and negative examples in turn + Iterator<Individual> itPos = classInstances.iterator(); + Iterator<Individual> itNeg = superClassInstances.iterator(); + + do { + // in each loop we pick 0 or 1 positives and 0 or 1 negative + // and classify it + + if(itPos.hasNext()) { + Individual posExample = itPos.next(); +// System.out.println(posExample); + + if(reasoner.hasType(description, posExample)) { + posClassifiedAsPos++; + } else { + notCoveredPos++; + } + nrOfPosChecks++; + + // take noise into account + if(notCoveredPos > maxNotCovered) { + return -1; + } + } + + if(itNeg.hasNext()) { + Individual negExample = itNeg.next(); + if(!reasoner.hasType(description, negExample)) { + negClassifiedAsNeg++; + } + nrOfNegChecks++; + } + + // compute how accurate our current approximation is and return if it is sufficiently accurate + double approx[] = Heuristics.getPredAccApproximation(classInstances.size(), superClassInstances.size(), 1, nrOfPosChecks, posClassifiedAsPos, nrOfNegChecks, negClassifiedAsNeg); + if(approx[1]<approxDelta) { +// System.out.println(approx[0]); + return approx[0]; + } + + } while(itPos.hasNext() || itNeg.hasNext()); + + double ret = Heuristics.getPredictiveAccuracy(classInstances.size(), superClassInstances.size(), posClassifiedAsPos, negClassifiedAsNeg, 1); + return ret; } else { throw new Error("Approximation for " + heuristic + " not implemented."); } Modified: trunk/scripts/src/main/java/org/dllearner/scripts/CrossValidation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/CrossValidation.java 2010-09-24 12:08:03 UTC (rev 2335) +++ trunk/scripts/src/main/java/org/dllearner/scripts/CrossValidation.java 2010-09-24 12:48:56 UTC (rev 2336) @@ -41,9 +41,9 @@ import org.dllearner.core.ReasonerComponent; import org.dllearner.core.owl.Description; import org.dllearner.core.owl.Individual; +import org.dllearner.learningproblems.Heuristics; import org.dllearner.learningproblems.PosNegLP; import org.dllearner.learningproblems.PosOnlyLP; -import org.dllearner.parser.ParseException; import org.dllearner.utilities.Helper; import org.dllearner.utilities.datastructures.Datastructures; import org.dllearner.utilities.statistics.Stat; @@ -64,6 +64,8 @@ private Stat accuracy = new Stat(); private Stat length = new Stat(); private Stat accuracyTraining = new Stat(); + private Stat fMeasure = new Stat(); + private Stat fMeasureTraining = new Stat(); public static void main(String[] args) { File file = new File(args[0]); @@ -261,6 +263,17 @@ double currAccuracy = 100*((double)correctExamples/(testSetsPos.get(currFold).size()+ testSetsNeg.get(currFold).size())); accuracy.addNumber(currAccuracy); + // calculate training F-Score + int negAsPosTraining = rs.hasType(concept, trainingSetsNeg.get(currFold)).size(); + double precisionTraining = trainingCorrectPosClassified + negAsPosTraining == 0 ? 0 : trainingCorrectPosClassified / (double) (trainingCorrectPosClassified + negAsPosTraining); + double recallTraining = trainingCorrectPosClassified / (double) trainingSetsPos.get(currFold).size(); + fMeasureTraining.addNumber(100*Heuristics.getFScore(recallTraining, precisionTraining)); + // calculate test F-Score + int negAsPos = rs.hasType(concept, testSetsNeg.get(currFold)).size(); + double precision = correctPosClassified + negAsPos == 0 ? 0 : correctPosClassified / (double) (correctPosClassified + negAsPos); + double recall = correctPosClassified / (double) testSetsPos.get(currFold).size(); +// System.out.println(precision);System.out.println(recall); + fMeasure.addNumber(100*Heuristics.getFScore(recall, precision)); length.addNumber(concept.getLength()); @@ -282,8 +295,11 @@ System.out.println("Finished " + folds + "-folds cross-validation on " + file + "."); System.out.println("runtime: " + statOutput(df, runtime, "s")); System.out.println("length: " + statOutput(df, length, "")); - System.out.println("accuracy: " + statOutput(df, accuracy, "%")); - System.out.println("accuracy on training set: " + statOutput(df, accuracyTraining, "%")); + System.out.println("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%")); + System.out.println("F-Measure: " + statOutput(df, fMeasure, "%")); + System.out.println("predictive accuracy on training set: " + statOutput(df, accuracyTraining, "%")); + System.out.println("predictive accuracy: " + statOutput(df, accuracy, "%")); + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |