PRCEvaluation.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2011 Sergey Lisitsyn
00008  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
00009  */
00010 
00011 #include <shogun/evaluation/PRCEvaluation.h>
00012 #include <shogun/mathematics/Math.h>
00013 
00014 using namespace shogun;
00015 
00016 CPRCEvaluation::~CPRCEvaluation()
00017 {
00018     SG_FREE(m_PRC_graph);
00019 }
00020 
00021 float64_t CPRCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth)
00022 {
00023     ASSERT(predicted && ground_truth);
00024     ASSERT(predicted->get_num_labels()==ground_truth->get_num_labels());
00025     ASSERT(ground_truth->is_two_class_labeling());
00026 
00027     // number of true positive examples
00028     float64_t tp = 0.0;
00029     int32_t i;
00030 
00031     // total number of positive labels in predicted
00032     int32_t pos_count=0;
00033 
00034     // initialize number of labels and labels
00035     SGVector<float64_t> orig_labels = predicted->get_labels();
00036     int32_t length = orig_labels.vlen;
00037     float64_t* labels = CMath::clone_vector(orig_labels.vector, length);
00038     orig_labels.free_vector();
00039 
00040     // get indexes for sort
00041     int32_t* idxs = SG_MALLOC(int32_t, length);
00042     for(i=0; i<length; i++)
00043         idxs[i] = i;
00044 
00045     // sort indexes by labels ascending
00046     CMath::qsort_backward_index(labels,idxs,length);
00047 
00048     // clean and initialize graph and auPRC
00049     SG_FREE(labels);
00050     SG_FREE(m_PRC_graph);
00051     m_PRC_graph = SG_MALLOC(float64_t, length*2);
00052     m_auPRC = 0.0;
00053 
00054     // get total numbers of positive and negative labels
00055     for (i=0; i<length; i++)
00056     {
00057         if (ground_truth->get_label(i) > 0)
00058             pos_count++;
00059     }
00060 
00061     // assure number of positive examples is >0
00062     ASSERT(pos_count>0);
00063 
00064     // create PRC curve
00065     for (i=0; i<length; i++)
00066     {
00067         // update number of true positive examples
00068         if (ground_truth->get_label(idxs[i]) > 0)
00069             tp += 1.0;
00070 
00071         // precision (x)
00072         m_PRC_graph[2*i] = tp/float64_t(i+1);
00073         // recall (y)
00074         m_PRC_graph[2*i+1] = tp/float64_t(pos_count);
00075     }
00076 
00077     // calc auRPC using area under curve
00078     m_auPRC = CMath::area_under_curve(m_PRC_graph,length,true);
00079 
00080     // set PRC length and computed indicator
00081     m_PRC_length = length;
00082     m_computed = true;
00083 
00084     return m_auPRC;
00085 }
00086 
00087 SGMatrix<float64_t> CPRCEvaluation::get_PRC()
00088 {
00089     if (!m_computed)
00090         SG_ERROR("Uninitialized, please call evaluate first");
00091 
00092     ASSERT(m_PRC_graph);
00093 
00094     return SGMatrix<float64_t>(m_PRC_graph,2,m_PRC_length);
00095 }
00096 
00097 float64_t CPRCEvaluation::get_auPRC()
00098 {
00099     if (!m_computed)
00100             SG_ERROR("Uninitialized, please call evaluate first");
00101 
00102     return m_auPRC;
00103 }
00104 
00105 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation