Changeset 41 for trunk/CrossPare/src/de/ugoe/cs/cpdp/eval
- Timestamp:
- 09/24/15 10:59:05 (9 years ago)
- Location:
- trunk/CrossPare/src/de/ugoe/cs/cpdp/eval
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/AbstractWekaEvaluation.java
r35 r41 1 // Copyright 2015 Georg-August-Universität Göttingen, Germany 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 1 15 package de.ugoe.cs.cpdp.eval; 2 16 … … 17 31 18 32 /** 19 * Base class for the evaluation of results of classifiers compatible with the {@link Classifier} interface.20 * For each classifier, the following metrics are calculated:33 * Base class for the evaluation of results of classifiers compatible with the {@link Classifier} 34 * interface. For each classifier, the following metrics are calculated: 21 35 * <ul> 22 * <li>succHe: Success with recall>0.7, precision>0.5</li> 23 * <li>succZi: Success with recall>0.7, precision>0.7</li> 24 * <li>succG75: Success with gscore>0.75</li> 25 * <li>succG60: Success with gscore>0.6</li> 26 * <li>error</li> 27 * <li>recall</li> 28 * <li>precision</li> 29 * <li>fscore</li> 30 * <li>gscore</li> 31 * <li>AUC</li> 32 * <li>AUCEC (weighted by LOC, if applicable; 0.0 if LOC not available)</li> 33 * <li>tpr: true positive rate</li> 34 * <li>tnr: true negative rate</li> 35 * <li>tp: true positives</li> 36 * <li>fp: false positives</li> 37 * <li>tn: true negatives</li> 38 * <li>fn: false negatives</li> 39 * <li>errortrain: training error</li> 40 * <li>recalltrain: training recall</li> 41 * <li>precisiontrain: training precision</li> 42 * <li>succHetrain: training success with recall>0.7 and precision>0.5 43 * </ul> 36 * <li>succHe: Success with recall>0.7, precision>0.5</li> 37 * <li>succZi: Success with recall>0.7, precision>0.7</li> 38 * <li>succG75: Success with gscore>0.75</li> 39 * <li>succG60: Success with gscore>0.6</li> 40 * <li>error</li> 41 * <li>recall</li> 42 * <li>precision</li> 43 * <li>fscore</li> 44 * <li>gscore</li> 45 * <li>AUC</li> 46 * <li>AUCEC (weighted by LOC, if applicable; 0.0 if LOC not available)</li> 47 * <li>tpr: true positive rate</li> 48 * <li>tnr: true negative rate</li> 49 * <li>tp: true positives</li> 50 * <li>fp: false positives</li> 51 * <li>tn: true negatives</li> 52 * <li>fn: false negatives</li> 53 * <li>errortrain: training error</li> 54 * <li>recalltrain: training recall</li> 55 * <li>precisiontrain: training precision</li> 56 * <li>succHetrain: training success with recall>0.7 and precision>0.5 57 * </ul> 58 * 44 59 * @author Steffen Herbold 45 60 */ 46 61 public abstract class AbstractWekaEvaluation implements IEvaluationStrategy { 47 62 48 /** 49 * writer for the evaluation results 50 */ 51 private PrintWriter output = new PrintWriter(System.out); 52 53 private boolean outputIsSystemOut = true; 54 55 /** 56 * Creates the weka evaluator. Allows the creation of the evaluator in different ways, e.g., for cross-validation 57 * or evaluation on the test data. 58 * @param testdata test data 59 * @param classifier classifier used 60 * @return evaluator 61 */ 62 protected abstract Evaluation createEvaluator(Instances testdata, Classifier classifier); 63 64 /* 65 * (non-Javadoc) 66 * @see de.ugoe.cs.cpdp.eval.EvaluationStrategy#apply(weka.core.Instances, weka.core.Instances, java.util.List, boolean) 67 */ 68 @Override 69 public void apply(Instances testdata, Instances traindata, List<ITrainer> trainers, 70 boolean writeHeader) { 71 final List<Classifier> classifiers = new LinkedList<Classifier>(); 72 for( ITrainer trainer : trainers ) { 73 if( trainer instanceof IWekaCompatibleTrainer ) { 74 classifiers.add(((IWekaCompatibleTrainer) trainer).getClassifier()); 75 } else { 76 throw new RuntimeException("The selected evaluator only support Weka classifiers"); 77 } 78 } 79 80 if( writeHeader ) { 81 output.append("version,size_test,size_training"); 82 for( ITrainer trainer : trainers ) { 83 output.append(",succHe_" + ((IWekaCompatibleTrainer) trainer).getName()); 84 output.append(",succZi_" + ((IWekaCompatibleTrainer) trainer).getName()); 85 output.append(",succG75_" + ((IWekaCompatibleTrainer) trainer).getName()); 86 output.append(",succG60_" + ((IWekaCompatibleTrainer) trainer).getName()); 87 output.append(",error_" + ((IWekaCompatibleTrainer) trainer).getName()); 88 output.append(",recall_" + ((IWekaCompatibleTrainer) trainer).getName()); 89 output.append(",precision_" + ((IWekaCompatibleTrainer) trainer).getName()); 90 output.append(",fscore_" + ((IWekaCompatibleTrainer) trainer).getName()); 91 output.append(",gscore_" + ((IWekaCompatibleTrainer) trainer).getName()); 92 output.append(",mcc_" + ((IWekaCompatibleTrainer) trainer).getName()); 93 output.append(",auc_" + ((IWekaCompatibleTrainer) trainer).getName()); 94 output.append(",aucec_" + ((IWekaCompatibleTrainer) trainer).getName()); 95 output.append(",tpr_" + ((IWekaCompatibleTrainer) trainer).getName()); 96 output.append(",tnr_" + ((IWekaCompatibleTrainer) trainer).getName()); 97 output.append(",tp_" + ((IWekaCompatibleTrainer) trainer).getName()); 98 output.append(",fn_" + ((IWekaCompatibleTrainer) trainer).getName()); 99 output.append(",tn_" + ((IWekaCompatibleTrainer) trainer).getName()); 100 output.append(",fp_" + ((IWekaCompatibleTrainer) trainer).getName()); 101 output.append(",trainerror_" + ((IWekaCompatibleTrainer) trainer).getName()); 102 output.append(",trainrecall_" + ((IWekaCompatibleTrainer) trainer).getName()); 103 output.append(",trainprecision_" + ((IWekaCompatibleTrainer) trainer).getName()); 104 output.append(",trainsuccHe_" + ((IWekaCompatibleTrainer) trainer).getName()); 105 } 106 output.append(StringTools.ENDLINE); 107 } 108 109 output.append(testdata.relationName()); 110 output.append("," + testdata.numInstances()); 111 output.append("," + traindata.numInstances()); 112 113 Evaluation eval = null; 114 Evaluation evalTrain = null; 115 for( Classifier classifier : classifiers ) { 116 eval = createEvaluator(testdata, classifier); 117 evalTrain = createEvaluator(traindata, classifier); 118 119 double pf = eval.numFalsePositives(1)/(eval.numFalsePositives(1)+eval.numTrueNegatives(1)); 120 double gmeasure = 2*eval.recall(1)*(1.0-pf)/(eval.recall(1)+(1.0-pf)); 121 double mcc = (eval.numTruePositives(1)*eval.numTrueNegatives(1)-eval.numFalsePositives(1)*eval.numFalseNegatives(1))/Math.sqrt((eval.numTruePositives(1)+eval.numFalsePositives(1))*(eval.numTruePositives(1)+eval.numFalseNegatives(1))*(eval.numTrueNegatives(1)+eval.numFalsePositives(1))*(eval.numTrueNegatives(1)+eval.numFalseNegatives(1))); 122 double aucec = calculateReviewEffort(testdata, classifier); 123 124 if( eval.recall(1)>=0.7 && eval.precision(1) >= 0.5 ) { 125 output.append(",1"); 126 } else { 127 output.append(",0"); 128 } 129 130 if( eval.recall(1)>=0.7 && eval.precision(1) >= 0.7 ) { 131 output.append(",1"); 132 } else { 133 output.append(",0"); 134 } 135 136 if( gmeasure>0.75 ) { 137 output.append(",1"); 138 } else { 139 output.append(",0"); 140 } 141 142 if( gmeasure>0.6 ) { 143 output.append(",1"); 144 } else { 145 output.append(",0"); 146 } 147 148 output.append("," + eval.errorRate()); 149 output.append("," + eval.recall(1)); 150 output.append("," + eval.precision(1)); 151 output.append("," + eval.fMeasure(1)); 152 output.append("," + gmeasure); 153 output.append("," + mcc); 154 output.append("," + eval.areaUnderROC(1)); 155 output.append("," + aucec); 156 output.append("," + eval.truePositiveRate(1)); 157 output.append("," + eval.trueNegativeRate(1)); 158 output.append("," + eval.numTruePositives(1)); 159 output.append("," + eval.numFalseNegatives(1)); 160 output.append("," + eval.numTrueNegatives(1)); 161 output.append("," + eval.numFalsePositives(1)); 162 output.append("," + evalTrain.errorRate()); 163 output.append("," + evalTrain.recall(1)); 164 output.append("," + evalTrain.precision(1)); 165 if( evalTrain.recall(1)>=0.7 && evalTrain.precision(1) >= 0.5 ) { 166 output.append(",1"); 167 } else { 168 output.append(",0"); 169 } 170 } 171 172 output.append(StringTools.ENDLINE); 173 output.flush(); 174 } 175 176 private double calculateReviewEffort(Instances testdata, Classifier classifier) { 177 178 final Attribute loc = testdata.attribute("loc"); 179 if( loc==null ) { 180 return 0.0; 181 } 182 183 final List<Integer> bugPredicted = new ArrayList<>(); 184 final List<Integer> nobugPredicted = new ArrayList<>(); 185 double totalLoc = 0.0d; 186 int totalBugs = 0; 187 for( int i=0 ; i<testdata.numInstances() ; i++ ) { 188 try { 189 if( Double.compare(classifier.classifyInstance(testdata.instance(i)),0.0d)==0 ) { 190 nobugPredicted.add(i); 191 } else { 192 bugPredicted.add(i); 193 } 194 } catch (Exception e) { 195 throw new RuntimeException("unexpected error during the evaluation of the review effort", e); 196 } 197 if(Double.compare(testdata.instance(i).classValue(),1.0d)==0) { 198 totalBugs++; 199 } 200 totalLoc += testdata.instance(i).value(loc); 201 } 202 203 final List<Double> reviewLoc = new ArrayList<>(testdata.numInstances()); 204 final List<Double> bugsFound = new ArrayList<>(testdata.numInstances()); 205 206 double currentBugsFound = 0; 207 208 while( !bugPredicted.isEmpty() ) { 209 double minLoc = Double.MAX_VALUE; 210 int minIndex = -1; 211 for( int i=0 ; i<bugPredicted.size() ; i++ ) { 212 double currentLoc = testdata.instance(bugPredicted.get(i)).value(loc); 213 if( currentLoc<minLoc ) { 214 minIndex = i; 215 minLoc = currentLoc; 216 } 217 } 218 if( minIndex!=-1 ) { 219 reviewLoc.add(minLoc/totalLoc); 220 221 currentBugsFound += testdata.instance(bugPredicted.get(minIndex)).classValue(); 222 bugsFound.add(currentBugsFound); 223 224 bugPredicted.remove(minIndex); 225 } else { 226 throw new RuntimeException("Shouldn't happen!"); 227 } 228 } 229 230 while( !nobugPredicted.isEmpty() ) { 231 double minLoc = Double.MAX_VALUE; 232 int minIndex = -1; 233 for( int i=0 ; i<nobugPredicted.size() ; i++ ) { 234 double currentLoc = testdata.instance(nobugPredicted.get(i)).value(loc); 235 if( currentLoc<minLoc ) { 236 minIndex = i; 237 minLoc = currentLoc; 238 } 239 } 240 if( minIndex!=-1 ) { 241 reviewLoc.add(minLoc/totalLoc); 242 243 currentBugsFound += testdata.instance(nobugPredicted.get(minIndex)).classValue(); 244 bugsFound.add(currentBugsFound); 245 nobugPredicted.remove(minIndex); 246 } else { 247 throw new RuntimeException("Shouldn't happen!"); 248 } 249 } 250 251 double auc = 0.0; 252 for( int i=0 ; i<bugsFound.size() ; i++ ) { 253 auc += reviewLoc.get(i)*bugsFound.get(i)/totalBugs; 254 } 255 256 return auc; 257 } 258 259 /* 260 * (non-Javadoc) 261 * @see de.ugoe.cs.cpdp.Parameterizable#setParameter(java.lang.String) 262 */ 263 @Override 264 public void setParameter(String parameters) { 265 if( output!=null && !outputIsSystemOut ) { 266 output.close(); 267 } 268 if( "system.out".equals(parameters) || "".equals(parameters) ) { 269 output = new PrintWriter(System.out); 270 outputIsSystemOut = true; 271 } else { 272 try { 273 output = new PrintWriter(new FileOutputStream(parameters)); 274 outputIsSystemOut = false; 275 } catch (FileNotFoundException e) { 276 throw new RuntimeException(e); 277 } 278 } 279 } 63 /** 64 * writer for the evaluation results 65 */ 66 private PrintWriter output = new PrintWriter(System.out); 67 68 private boolean outputIsSystemOut = true; 69 70 /** 71 * Creates the weka evaluator. Allows the creation of the evaluator in different ways, e.g., for 72 * cross-validation or evaluation on the test data. 73 * 74 * @param testdata 75 * test data 76 * @param classifier 77 * classifier used 78 * @return evaluator 79 */ 80 protected abstract Evaluation createEvaluator(Instances testdata, Classifier classifier); 81 82 /* 83 * (non-Javadoc) 84 * 85 * @see de.ugoe.cs.cpdp.eval.EvaluationStrategy#apply(weka.core.Instances, weka.core.Instances, 86 * java.util.List, boolean) 87 */ 88 @Override 89 public void apply(Instances testdata, 90 Instances traindata, 91 List<ITrainer> trainers, 92 boolean writeHeader) 93 { 94 final List<Classifier> classifiers = new LinkedList<Classifier>(); 95 for (ITrainer trainer : trainers) { 96 if (trainer instanceof IWekaCompatibleTrainer) { 97 classifiers.add(((IWekaCompatibleTrainer) trainer).getClassifier()); 98 } 99 else { 100 throw new RuntimeException("The selected evaluator only support Weka classifiers"); 101 } 102 } 103 104 if (writeHeader) { 105 output.append("version,size_test,size_training"); 106 for (ITrainer trainer : trainers) { 107 output.append(",succHe_" + ((IWekaCompatibleTrainer) trainer).getName()); 108 output.append(",succZi_" + ((IWekaCompatibleTrainer) trainer).getName()); 109 output.append(",succG75_" + ((IWekaCompatibleTrainer) trainer).getName()); 110 output.append(",succG60_" + ((IWekaCompatibleTrainer) trainer).getName()); 111 output.append(",error_" + ((IWekaCompatibleTrainer) trainer).getName()); 112 output.append(",recall_" + ((IWekaCompatibleTrainer) trainer).getName()); 113 output.append(",precision_" + ((IWekaCompatibleTrainer) trainer).getName()); 114 output.append(",fscore_" + ((IWekaCompatibleTrainer) trainer).getName()); 115 output.append(",gscore_" + ((IWekaCompatibleTrainer) trainer).getName()); 116 output.append(",mcc_" + ((IWekaCompatibleTrainer) trainer).getName()); 117 output.append(",auc_" + ((IWekaCompatibleTrainer) trainer).getName()); 118 output.append(",aucec_" + ((IWekaCompatibleTrainer) trainer).getName()); 119 output.append(",tpr_" + ((IWekaCompatibleTrainer) trainer).getName()); 120 output.append(",tnr_" + ((IWekaCompatibleTrainer) trainer).getName()); 121 output.append(",tp_" + ((IWekaCompatibleTrainer) trainer).getName()); 122 output.append(",fn_" + ((IWekaCompatibleTrainer) trainer).getName()); 123 output.append(",tn_" + ((IWekaCompatibleTrainer) trainer).getName()); 124 output.append(",fp_" + ((IWekaCompatibleTrainer) trainer).getName()); 125 output.append(",trainerror_" + ((IWekaCompatibleTrainer) trainer).getName()); 126 output.append(",trainrecall_" + ((IWekaCompatibleTrainer) trainer).getName()); 127 output.append(",trainprecision_" + ((IWekaCompatibleTrainer) trainer).getName()); 128 output.append(",trainsuccHe_" + ((IWekaCompatibleTrainer) trainer).getName()); 129 } 130 output.append(StringTools.ENDLINE); 131 } 132 133 output.append(testdata.relationName()); 134 output.append("," + testdata.numInstances()); 135 output.append("," + traindata.numInstances()); 136 137 Evaluation eval = null; 138 Evaluation evalTrain = null; 139 for (Classifier classifier : classifiers) { 140 eval = createEvaluator(testdata, classifier); 141 evalTrain = createEvaluator(traindata, classifier); 142 143 double pf = 144 eval.numFalsePositives(1) / (eval.numFalsePositives(1) + eval.numTrueNegatives(1)); 145 double gmeasure = 2 * eval.recall(1) * (1.0 - pf) / (eval.recall(1) + (1.0 - pf)); 146 double mcc = 147 (eval.numTruePositives(1) * eval.numTrueNegatives(1) - eval.numFalsePositives(1) * 148 eval.numFalseNegatives(1)) / 149 Math.sqrt((eval.numTruePositives(1) + eval.numFalsePositives(1)) * 150 (eval.numTruePositives(1) + eval.numFalseNegatives(1)) * 151 (eval.numTrueNegatives(1) + eval.numFalsePositives(1)) * 152 (eval.numTrueNegatives(1) + eval.numFalseNegatives(1))); 153 double aucec = calculateReviewEffort(testdata, classifier); 154 155 if (eval.recall(1) >= 0.7 && eval.precision(1) >= 0.5) { 156 output.append(",1"); 157 } 158 else { 159 output.append(",0"); 160 } 161 162 if (eval.recall(1) >= 0.7 && eval.precision(1) >= 0.7) { 163 output.append(",1"); 164 } 165 else { 166 output.append(",0"); 167 } 168 169 if (gmeasure > 0.75) { 170 output.append(",1"); 171 } 172 else { 173 output.append(",0"); 174 } 175 176 if (gmeasure > 0.6) { 177 output.append(",1"); 178 } 179 else { 180 output.append(",0"); 181 } 182 183 output.append("," + eval.errorRate()); 184 output.append("," + eval.recall(1)); 185 output.append("," + eval.precision(1)); 186 output.append("," + eval.fMeasure(1)); 187 output.append("," + gmeasure); 188 output.append("," + mcc); 189 output.append("," + eval.areaUnderROC(1)); 190 output.append("," + aucec); 191 output.append("," + eval.truePositiveRate(1)); 192 output.append("," + eval.trueNegativeRate(1)); 193 output.append("," + eval.numTruePositives(1)); 194 output.append("," + eval.numFalseNegatives(1)); 195 output.append("," + eval.numTrueNegatives(1)); 196 output.append("," + eval.numFalsePositives(1)); 197 output.append("," + evalTrain.errorRate()); 198 output.append("," + evalTrain.recall(1)); 199 output.append("," + evalTrain.precision(1)); 200 if (evalTrain.recall(1) >= 0.7 && evalTrain.precision(1) >= 0.5) { 201 output.append(",1"); 202 } 203 else { 204 output.append(",0"); 205 } 206 } 207 208 output.append(StringTools.ENDLINE); 209 output.flush(); 210 } 211 212 private double calculateReviewEffort(Instances testdata, Classifier classifier) { 213 214 final Attribute loc = testdata.attribute("loc"); 215 if (loc == null) { 216 return 0.0; 217 } 218 219 final List<Integer> bugPredicted = new ArrayList<>(); 220 final List<Integer> nobugPredicted = new ArrayList<>(); 221 double totalLoc = 0.0d; 222 int totalBugs = 0; 223 for (int i = 0; i < testdata.numInstances(); i++) { 224 try { 225 if (Double.compare(classifier.classifyInstance(testdata.instance(i)), 0.0d) == 0) { 226 nobugPredicted.add(i); 227 } 228 else { 229 bugPredicted.add(i); 230 } 231 } 232 catch (Exception e) { 233 throw new RuntimeException( 234 "unexpected error during the evaluation of the review effort", 235 e); 236 } 237 if (Double.compare(testdata.instance(i).classValue(), 1.0d) == 0) { 238 totalBugs++; 239 } 240 totalLoc += testdata.instance(i).value(loc); 241 } 242 243 final List<Double> reviewLoc = new ArrayList<>(testdata.numInstances()); 244 final List<Double> bugsFound = new ArrayList<>(testdata.numInstances()); 245 246 double currentBugsFound = 0; 247 248 while (!bugPredicted.isEmpty()) { 249 double minLoc = Double.MAX_VALUE; 250 int minIndex = -1; 251 for (int i = 0; i < bugPredicted.size(); i++) { 252 double currentLoc = testdata.instance(bugPredicted.get(i)).value(loc); 253 if (currentLoc < minLoc) { 254 minIndex = i; 255 minLoc = currentLoc; 256 } 257 } 258 if (minIndex != -1) { 259 reviewLoc.add(minLoc / totalLoc); 260 261 currentBugsFound += testdata.instance(bugPredicted.get(minIndex)).classValue(); 262 bugsFound.add(currentBugsFound); 263 264 bugPredicted.remove(minIndex); 265 } 266 else { 267 throw new RuntimeException("Shouldn't happen!"); 268 } 269 } 270 271 while (!nobugPredicted.isEmpty()) { 272 double minLoc = Double.MAX_VALUE; 273 int minIndex = -1; 274 for (int i = 0; i < nobugPredicted.size(); i++) { 275 double currentLoc = testdata.instance(nobugPredicted.get(i)).value(loc); 276 if (currentLoc < minLoc) { 277 minIndex = i; 278 minLoc = currentLoc; 279 } 280 } 281 if (minIndex != -1) { 282 reviewLoc.add(minLoc / totalLoc); 283 284 currentBugsFound += testdata.instance(nobugPredicted.get(minIndex)).classValue(); 285 bugsFound.add(currentBugsFound); 286 nobugPredicted.remove(minIndex); 287 } 288 else { 289 throw new RuntimeException("Shouldn't happen!"); 290 } 291 } 292 293 double auc = 0.0; 294 for (int i = 0; i < bugsFound.size(); i++) { 295 auc += reviewLoc.get(i) * bugsFound.get(i) / totalBugs; 296 } 297 298 return auc; 299 } 300 301 /* 302 * (non-Javadoc) 303 * 304 * @see de.ugoe.cs.cpdp.Parameterizable#setParameter(java.lang.String) 305 */ 306 @Override 307 public void setParameter(String parameters) { 308 if (output != null && !outputIsSystemOut) { 309 output.close(); 310 } 311 if ("system.out".equals(parameters) || "".equals(parameters)) { 312 output = new PrintWriter(System.out); 313 outputIsSystemOut = true; 314 } 315 else { 316 try { 317 output = new PrintWriter(new FileOutputStream(parameters)); 318 outputIsSystemOut = false; 319 } 320 catch (FileNotFoundException e) { 321 throw new RuntimeException(e); 322 } 323 } 324 } 280 325 } -
trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/CVWekaEvaluation.java
r2 r41 1 // Copyright 2015 Georg-August-Universität Göttingen, Germany 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 1 15 package de.ugoe.cs.cpdp.eval; 2 16 … … 12 26 /** 13 27 * Implements the {@link AbstractWekaEvaluation} for 10-fold cross validation. 28 * 14 29 * @author Steffen Herbold 15 30 */ 16 31 public class CVWekaEvaluation extends AbstractWekaEvaluation { 17 18 /** 19 * @see de.ugoe.cs.cpdp.eval.AbstractWekaEvaluation#createEvaluator(weka.core.Instances, weka.classifiers.Classifier) 20 */ 21 @Override 22 protected Evaluation createEvaluator(Instances testdata, Classifier classifier) { 23 PrintStream errStr = System.err; 24 System.setErr(new PrintStream(new NullOutputStream())); 25 try { 26 final Evaluation eval = new Evaluation(testdata); 27 eval.crossValidateModel(classifier, testdata, 10, new Random(1)); 28 return eval; 29 } catch (Exception e) { 30 throw new RuntimeException(e); 31 } finally { 32 System.setErr(errStr); 33 } 34 } 32 33 /** 34 * @see de.ugoe.cs.cpdp.eval.AbstractWekaEvaluation#createEvaluator(weka.core.Instances, 35 * weka.classifiers.Classifier) 36 */ 37 @Override 38 protected Evaluation createEvaluator(Instances testdata, Classifier classifier) { 39 PrintStream errStr = System.err; 40 System.setErr(new PrintStream(new NullOutputStream())); 41 try { 42 final Evaluation eval = new Evaluation(testdata); 43 eval.crossValidateModel(classifier, testdata, 10, new Random(1)); 44 return eval; 45 } 46 catch (Exception e) { 47 throw new RuntimeException(e); 48 } 49 finally { 50 System.setErr(errStr); 51 } 52 } 35 53 36 54 } -
trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/IEvaluationStrategy.java
r2 r41 1 // Copyright 2015 Georg-August-Universität Göttingen, Germany 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 1 15 package de.ugoe.cs.cpdp.eval; 2 16 … … 9 23 10 24 /** 11 * Interface for evaluation strategies to evaluate the performance of classifiers. 25 * Interface for evaluation strategies to evaluate the performance of classifiers. 26 * 12 27 * @author Steffen Herbold 13 28 */ 14 29 public interface IEvaluationStrategy extends IParameterizable { 15 30 16 /** 17 * Applies the evaluation strategy. 18 * @param testdata test data for the evaluation 19 * @param traindata training data used 20 * @param trainers list of training algorithms used to train the classifiers 21 * @param writeHeader if true, a header line for the results file is written (may not be applicable) 22 */ 23 void apply(Instances testdata, Instances traindata, List<ITrainer> trainers, boolean writeHeader); 31 /** 32 * Applies the evaluation strategy. 33 * 34 * @param testdata 35 * test data for the evaluation 36 * @param traindata 37 * training data used 38 * @param trainers 39 * list of training algorithms used to train the classifiers 40 * @param writeHeader 41 * if true, a header line for the results file is written (may not be applicable) 42 */ 43 void apply(Instances testdata, Instances traindata, List<ITrainer> trainers, boolean writeHeader); 24 44 } -
trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/NormalWekaEvaluation.java
r2 r41 1 // Copyright 2015 Georg-August-Universität Göttingen, Germany 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 1 15 package de.ugoe.cs.cpdp.eval; 2 16 … … 7 21 /** 8 22 * Implements the {@link AbstractWekaEvaluation} for evaluation on the test data. 23 * 9 24 * @author Steffen Herbold 10 * 25 * 11 26 */ 12 27 public class NormalWekaEvaluation extends AbstractWekaEvaluation { 13 28 14 /** 15 * @see de.ugoe.cs.cpdp.eval.AbstractWekaEvaluation#createEvaluator(weka.core.Instances, weka.classifiers.Classifier) 16 */ 17 @Override 18 protected Evaluation createEvaluator(Instances testdata, Classifier classifier) { 19 try { 20 final Evaluation eval = new Evaluation(testdata); 21 eval.evaluateModel(classifier, testdata); 22 return eval; 23 } catch (Exception e) { 24 throw new RuntimeException(e); 25 } 26 } 29 /** 30 * @see de.ugoe.cs.cpdp.eval.AbstractWekaEvaluation#createEvaluator(weka.core.Instances, 31 * weka.classifiers.Classifier) 32 */ 33 @Override 34 protected Evaluation createEvaluator(Instances testdata, Classifier classifier) { 35 try { 36 final Evaluation eval = new Evaluation(testdata); 37 eval.evaluateModel(classifier, testdata); 38 return eval; 39 } 40 catch (Exception e) { 41 throw new RuntimeException(e); 42 } 43 } 27 44 }
Note: See TracChangeset
for help on using the changeset viewer.