Skip to content

Commit

Permalink
introduce MeanAbsoluteError
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Nov 5, 2011
1 parent 59c976a commit 857311c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/modular/Evaluation.i
Expand Up @@ -13,6 +13,7 @@
%rename(BinaryClassEvaluation) CBinaryClassEvaluation;
%rename(ContingencyTableEvaluation) CContingencyTableEvaluation;
%rename(MulticlassAccuracy) CMulticlassAccuracy;
%rename(MeanAbsoluteError) CMeanAbsoluteError;
%rename(MeanSquaredError) CMeanSquaredError;
%rename(ROCEvaluation) CROCEvaluation;
%rename(PRCEvaluation) CPRCEvaluation;
Expand All @@ -34,6 +35,7 @@
%include <shogun/evaluation/BinaryClassEvaluation.h>
%include <shogun/evaluation/ContingencyTableEvaluation.h>
%include <shogun/evaluation/MulticlassAccuracy.h>
%include <shogun/evaluation/MeanAbsoluteError.h>
%include <shogun/evaluation/MeanSquaredError.h>
%include <shogun/evaluation/ROCEvaluation.h>
%include <shogun/evaluation/PRCEvaluation.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Evaluation_includes.i
Expand Up @@ -4,6 +4,7 @@
#include <shogun/evaluation/BinaryClassEvaluation.h>
#include <shogun/evaluation/ContingencyTableEvaluation.h>
#include <shogun/evaluation/MulticlassAccuracy.h>
#include <shogun/evaluation/MeanAbsoluteError.h>
#include <shogun/evaluation/MeanSquaredError.h>
#include <shogun/evaluation/ROCEvaluation.h>
#include <shogun/evaluation/PRCEvaluation.h>
Expand Down
25 changes: 25 additions & 0 deletions src/shogun/evaluation/MeanAbsoluteError.cpp
@@ -0,0 +1,25 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2011 Soeren Sonnenburg, Sergey Lisitsyn
*/

#include <shogun/evaluation/MeanAbsoluteError.h>
#include <shogun/features/Labels.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

float64_t CMeanAbsoluteError::evaluate(CLabels* predicted, CLabels* ground_truth)
{
ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels());
int32_t length = predicted->get_num_labels();
float64_t mae = 0.0;
for (int32_t i=0; i<length; i++)
mae += CMath::abs(predicted->get_label(i) - ground_truth->get_label(i));
mae /= length;
return mae;
}
59 changes: 59 additions & 0 deletions src/shogun/evaluation/MeanAbsoluteError.h
@@ -0,0 +1,59 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2011 Soeren Sonnenburg, Sergey Lisitsyn
*/

#ifndef MEANABSOLUTEERROR_H_
#define MEANABSOLUTEERROR_H_

#include <shogun/evaluation/Evaluation.h>
#include <shogun/features/Labels.h>

namespace shogun
{

class CLabels;

/** @brief Class MeanAbsoluteError
* used to compute an error of regression model.
*
* Formally, for real labels \f$ L,R, |L|=|R|\f$ mean absolute
* error (MAE) is estimated as
*
* \f[
* \frac{1}{|L|} \sum_{i=1}^{|L|} |L_i - R_i|
* \f]
*
*/
class CMeanAbsoluteError: public CEvaluation
{
public:
/** constructor */
CMeanAbsoluteError() : CEvaluation() {};

/** destructor */
virtual ~CMeanAbsoluteError() {};

/** evaluate mean absolute error
* @param predicted labels for evaluating
* @param ground_truth labels assumed to be correct
* @return mean absolute error
*/
virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth);

inline EEvaluationDirection get_evaluation_direction()
{
return ED_MINIMIZE;
}

/** get name */
virtual inline const char* get_name() const { return "MeanAbsoluteError"; }
};

}

#endif /* MEANABSOLUTEERROR_H_ */

0 comments on commit 857311c

Please sign in to comment.