Cosan  1.0
Data Analytics Library
CosanMetric.h
Go to the documentation of this file.
1 /*
2  Created by Jiahe Chen
3 */
4 
5 #ifndef COSAN_METRIC_H
6 #define COSAN_METRIC_H
7 
8 // TODO: change after integrate with module
9 
10 // import from other lib
11 #include<string>
12 #include<exception>
13 #include<Eigen/Dense>
14 
15 // import from Cosan
16 #include <cosan/utils/ArgCheck.h>
17 #include <cosan/utils/Exceptions.h>
18 #include <cosan/data/CosanData.h>
20 namespace Cosan {
21  /*
22  Base class for metric function
23  */
24 // template<typename NumericType,
25 // typename = typename std::enable_if<std::is_arithmetic<NumericType>::value,NumericType>::type>
26 
27  /**
28  * @brief CosanMetric class for metric functionality.
29  * @param[in] yPredict data for prediction
30  * @param[in] yTrue data for truth
31  * @details Initialize metric class by
32  * @code CosanMetric(const CosanMatrix <NumericType> &yPredict, const CosanMatrix <NumericType> &yTrue) @endcode
33  * Get the metric error by
34  * @code GetError(const CosanMatrix <NumericType> &yPredict, const CosanMatrix <NumericType> &yTrue) @endcode
35  **/
36  template<Numeric NumericType>
37  class CosanMetric : public Evaluation {
38  public:
39  // Delete the default constructor.
41 
42  /*
43  Default constructor
44  */
46  setAttr(yPredict, yTrue);
47  };
48 
49  // returns the error rate
50  virtual NumericType
52  return 0;
53  };
54 
55  /*
56  Set the attributes, if use the default constructor
57  */
58  void setAttr(const CosanMatrix <NumericType> &yPredict, const CosanMatrix <NumericType> &yTrue) {
59  // check if the input matrices have the same size
60  if (!SameSize(yPredict, yTrue)) {
61  throw DiffSize;
62  }
63  // check the shape of the input
64  if (!LabelShape(yPredict)) {
65  throw InvalidLabelShape;
66  }
67  GetError(yPredict, yTrue);
68  }
69 
71  };
72 
73 
74 // /*
75 // count the number of errors in a prediction
76 // Parameters:
77 // yTrue: a refrence to a CosanMatrix object; the real labels
78 // with a shape of (#_of_samples, 1)
79 // yPredict: a refrence to a CosanMatrix object; the predicted labels
80 // with a shape of (#_of_samples, 1)
81 // threshold: double; threshold for error
82 // Output of GetError:
83 // result: NumericType; number of mismatch between predicted
84 // and real labels
85 // */
86 // template<typename NumericType,
87 // typename = typename std::enable_if<std::is_arithmetic<NumericType>::value,NumericType>::type>
88 // class NumOfError: public CosanMetric<NumericType>
89 // {
90 // NumericType Mthreshold;
91 //
92 // public:
93 // NumOfError(CosanMatrix<NumericType>& y1, CosanMatrix<NumericType>& y2, NumericType threshold): CosanMetric<NumericType>(y1, y2)
94 // {
95 // Mthreshold{threshold};
96 // error = GetError();
97 // }
98 //
99 // NumericType GetError()
100 // {
101 // return ((yTrue - yPredict) > Mthreshold).count();
102 // }
103 // };
104 
105 // template<typename NumericType,
106 // typename = typename std::enable_if<std::is_arithmetic<NumericType>::value, NumericType>::type>
107  /**
108  * @brief Mean absolute error
109  * @details https://scikit-learn.org/stable/modules/model_evaluation.html#mean-absolute-error
110  **/
111  template<Numeric NumericType>
112  class MeanAbsError : public CosanMetric<NumericType> {
113  public:
115 
117  : CosanMetric<NumericType>(yPredict, yTrue) {}
118 
120  GetError(const CosanMatrix <NumericType> &yPredict, const CosanMatrix <NumericType> &yTrue) override {
121  this->error = (yPredict - yTrue).array().abs().sum() / yPredict.rows();
122  return this->error;
123  }
124  };
125 
126 
127  /**
128  * @brief Mean squared error
129  * @details https://scikit-learn.org/stable/modules/model_evaluation.html#mean-squared-error
130  **/
131  template<Numeric NumericType>
132  class MeanSquareError : public CosanMetric<NumericType> {
133  public:
135 
137  : CosanMetric<NumericType>(yPredict, yTrue) {}
138 
140  GetError(const CosanMatrix <NumericType> &yPredict, const CosanMatrix <NumericType> &yTrue) override {
141  this->error = (yTrue - yPredict).squaredNorm() / yTrue.rows();
142  return this->error;
143  }
144  };
145 
146 
147  /**
148  * @brief R2 square
149  * @details https://scikit-learn.org/stable/modules/model_evaluation.html#r2-score-the-coefficient-of-determination
150  **/
151  template<Numeric NumericType>
152  class R2Score : public CosanMetric<NumericType> {
153  public:
155 
157  : CosanMetric<NumericType>(yPredict, yTrue) {}
158 
160  GetError(const CosanMatrix <NumericType> &yPredict, const CosanMatrix <NumericType> &yTrue) override {
161  // yTrueMean = Constant(yTrue.rows(), yTrue.cols(). yTrue.mean());
162  this->error = 1 - (yTrue - yPredict).squaredNorm() / (yTrue.array() - yTrue.mean()).matrix().squaredNorm();
163  return this->error;
164  }
165  };
166  /**
167  * @brief maximum error
168  * @details https://scikit-learn.org/stable/modules/model_evaluation.html#max-error
169  **/
170  template<Numeric NumericType>
171  class MaxError : public CosanMetric<NumericType> {
172  public:
174 
176  : CosanMetric<NumericType>(yPredict, yTrue) {}
177 
179  GetError(const CosanMatrix <NumericType> &yPredict, const CosanMatrix <NumericType> &yTrue) override {
180 // yTrueMean = Constant(yTrue.rows(), yTrue.cols(). yTrue.mean());
181  this->error = (yTrue - yPredict).array().abs().maxCoeff();
182  return this->error;
183  }
184  };
185 
186 // template<typename NumericType,
187 // typename = typename std::enable_if<std::is_arithmetic<NumericType>::value,NumericType>::type>
188 // class LpNormError: public CosanMetric<NumericType>
189 // {
190 // public:
191 // LpNormError()=delete;s
192 // LpNormError(NumericType inputp ):CosanMetric<NumericType>(){ p = inputp;}
193 // LpNormError(const CosanMatrix<NumericType>& yPredict,const CosanMatrix<NumericType>& yTrue): CosanMetric<NumericType>(yPredict, yTrue){}
194 // NumericType GetError(const CosanMatrix<NumericType>& yPredict,const CosanMatrix<NumericType>& yTrue) override
195 // {
196 // switch(p){
197 // case (NumericType) 1:
198 // this->error = (yTrue-yPredict).template lpNorm<1>();
199 // break;
200 // case (NumericType) 2:
201 // this->error = (yTrue-yPredict).template lpNorm<2>();
202 // break;
203 // case std::numeric_limits<NumericType>::max():
204 // this->error = (yTrue-yPredict).template lpNorm<Eigen::Infinity>();
205 // break;
206 // default:
207 // this->error = std::pow((yTrue-yPredict).cwiseAbs().array().pow(p).sum(), 1.0/p);
208 // }
209 // return this->error;
210 // }
211 // NumericType Getp(){return p;}
212 // private:
213 // NumericType p;
214 // };
215 
216 
217 }
218 #endif
CosanData.h
Cosan::LabelShape
bool LabelShape(const CosanMatrix< NumericType > &m)
Definition: ArgCheck.h:46
Cosan
Definition: CosanBO.h:29
Cosan::CosanMetric::CosanMetric
CosanMetric()
Definition: CosanMetric.h:40
Cosan::CosanMetric::error
NumericType error
Definition: CosanMetric.h:70
NumericType
double NumericType
Definition: onehotencodingTest.cpp:20
Cosan::InvalidLabelShape
Cosan::InvalidLabelShapeException InvalidLabelShape
Cosan::MaxError::GetError
NumericType GetError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue) override
Definition: CosanMetric.h:179
Cosan::MeanSquareError
Mean squared error.
Definition: CosanMetric.h:132
ArgCheck.h
evaluation.h
Cosan::CosanMetric::CosanMetric
CosanMetric(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue)
Definition: CosanMetric.h:45
Cosan::Evaluation
Definition: evaluation.h:11
Cosan::R2Score::R2Score
R2Score(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue)
Definition: CosanMetric.h:156
Cosan::CosanMatrix
Eigen::Matrix< NumericType, Eigen::Dynamic, Eigen::Dynamic > CosanMatrix
Definition: CosanBO.h:37
Cosan::MeanSquareError::GetError
NumericType GetError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue) override
Definition: CosanMetric.h:140
Cosan::MeanSquareError::MeanSquareError
MeanSquareError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue)
Definition: CosanMetric.h:136
Cosan::CosanMetric::setAttr
void setAttr(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue)
Definition: CosanMetric.h:58
Cosan::MeanAbsError::GetError
NumericType GetError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue) override
Definition: CosanMetric.h:120
Cosan::R2Score::GetError
NumericType GetError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue) override
Definition: CosanMetric.h:160
Cosan::R2Score
R2 square.
Definition: CosanMetric.h:152
Cosan::MaxError::MaxError
MaxError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue)
Definition: CosanMetric.h:175
Cosan::MeanAbsError::MeanAbsError
MeanAbsError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue)
Definition: CosanMetric.h:116
Cosan::SameSize
bool SameSize(const CosanMatrix< NumericType > &m1, const CosanMatrix< NumericType > &m2)
Definition: ArgCheck.h:29
Cosan::MeanAbsError::MeanAbsError
MeanAbsError()
Definition: CosanMetric.h:114
Cosan::DiffSize
Cosan::DiffSizeException DiffSize
Exceptions.h
Cosan::R2Score::R2Score
R2Score()
Definition: CosanMetric.h:154
Cosan::MeanSquareError::MeanSquareError
MeanSquareError()
Definition: CosanMetric.h:134
Cosan::MeanAbsError
Mean absolute error.
Definition: CosanMetric.h:112
Cosan::MaxError::MaxError
MaxError()
Definition: CosanMetric.h:173
Cosan::CosanMetric::GetError
virtual NumericType GetError(const CosanMatrix< NumericType > &yPredict, const CosanMatrix< NumericType > &yTrue)
Definition: CosanMetric.h:51
Cosan::CosanMetric
CosanMetric class for metric functionality.
Definition: CosanMetric.h:37
Cosan::MaxError
maximum error
Definition: CosanMetric.h:171