Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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
00028 float64_t tp = 0.0;
00029 int32_t i;
00030
00031
00032 int32_t pos_count=0;
00033
00034
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
00041 int32_t* idxs = SG_MALLOC(int32_t, length);
00042 for(i=0; i<length; i++)
00043 idxs[i] = i;
00044
00045
00046 CMath::qsort_backward_index(labels,idxs,length);
00047
00048
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
00055 for (i=0; i<length; i++)
00056 {
00057 if (ground_truth->get_label(i) > 0)
00058 pos_count++;
00059 }
00060
00061
00062 ASSERT(pos_count>0);
00063
00064
00065 for (i=0; i<length; i++)
00066 {
00067
00068 if (ground_truth->get_label(idxs[i]) > 0)
00069 tp += 1.0;
00070
00071
00072 m_PRC_graph[2*i] = tp/float64_t(i+1);
00073
00074 m_PRC_graph[2*i+1] = tp/float64_t(pos_count);
00075 }
00076
00077
00078 m_auPRC = CMath::area_under_curve(m_PRC_graph,length,true);
00079
00080
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