001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.experiment.evaluation.classification.analysers.roc; 031 032import gov.sandia.cognition.statistics.method.ReceiverOperatingCharacteristic; 033import gov.sandia.cognition.util.DefaultPair; 034import gov.sandia.cognition.util.Pair; 035 036import java.util.ArrayList; 037import java.util.HashMap; 038import java.util.HashSet; 039import java.util.List; 040import java.util.Map; 041import java.util.Set; 042 043import org.openimaj.experiment.evaluation.classification.ClassificationAnalyser; 044import org.openimaj.experiment.evaluation.classification.ClassificationResult; 045 046/** 047 * A {@link ClassificationAnalyser} capable of producing 048 * a Receiver Operating Characteristic curve and associated 049 * statistics. 050 * 051 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 052 * 053 * @param <CLASS> Type of classes 054 * @param <OBJECT> Type of objects 055 */ 056public class ROCAnalyser< 057 OBJECT, 058 CLASS> 059implements ClassificationAnalyser< 060 ROCResult<CLASS>, 061 CLASS, 062 OBJECT> 063{ 064 065 @Override 066 public ROCResult<CLASS> analyse(Map<OBJECT, ClassificationResult<CLASS>> predicted, Map<OBJECT, Set<CLASS>> actual) { 067 //get all the classes 068 Set<CLASS> allClasses = new HashSet<CLASS>(); 069 for (OBJECT o : predicted.keySet()) { 070 allClasses.addAll(actual.get(o)); 071 } 072 073 //for each class compute a ROC curve 074 Map<CLASS, ReceiverOperatingCharacteristic> output = new HashMap<CLASS, ReceiverOperatingCharacteristic>(); 075 for (CLASS clz : allClasses) { 076 List<Pair<Boolean, Double>> data = new ArrayList<Pair<Boolean, Double>>(); 077 078 for (OBJECT o : predicted.keySet()) { 079 if (predicted.get(o) != null) { 080 double score = predicted.get(o).getConfidence(clz); 081 boolean objIsClass = actual.get(o).contains(clz); 082 083 data.add(new DefaultPair<Boolean, Double>(objIsClass, score)); 084 } else { 085 data.add(new DefaultPair<Boolean, Double>(false, 1.0)); 086 } 087 } 088 089 output.put(clz, ReceiverOperatingCharacteristic.createFromTargetEstimatePairs(data)); 090 } 091 092 return new ROCResult<CLASS>(output); 093 } 094 095}