Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
scorer.h
Go to the documentation of this file.
1#ifndef SCORER_H
2#define SCORER_H
3
4#include "metrics.h"
5#include "../util/error.h"
6#include "../types.h"
7
8// code to evaluate GP programs.
9namespace Brush{
10
11using namespace Pop;
12
13namespace Eval{
14
15
16template <ProgramType P>
17class Scorer
18{
19
20using RetType =
21 typename std::conditional_t<P == PT::Regressor, ArrayXf,
22 std::conditional_t<P == PT::Representer, ArrayXXf, ArrayXf
23 >>;
24
25typedef float (*funcPointer)(const VectorXf&,
26 const VectorXf&,
27 VectorXf&,
28 const vector<float>&);
29public:
30 // map the string into a function to be called when calculating the score
31 std::map<string, funcPointer> score_hash;
32 string scorer;
33
34 // TODO: add more scores, include them here, add to score_hash
35 Scorer(string scorer="mse") {
36 score_hash["mse"] = &mse;
37
38 this->set_scorer(scorer);
39 };
40
41 void set_scorer(string scorer){ this->scorer = scorer; };
42 string get_scorer(){return this->scorer; };
43
44 /* void set_scorer(string scorer); */
45 float score(const VectorXf& y_true, const VectorXf& y_pred,
46 VectorXf& loss, const vector<float>& w)
47 {
48 // loss is an array passed by reference to store each prediction (used in lexicase)
49 // weights are used to give more or less importance for a given sample.
50 // Every scorer must have the same function signature, but arent required to use all info
51
52 if ( score_hash.find(this->scorer) == score_hash.end() )
53 {
54 HANDLE_ERROR_THROW("Scoring function '" + this->scorer + "' not defined");
55 return 0.0;
56 }
57 else
58 {
59 return score_hash.at(this->scorer)(y_true, y_pred, loss, w);
60 }
61 };
62
63 float score(Individual<P>& ind, Dataset& data,
64 VectorXf& loss, const Parameters& params)
65 {
66 RetType y_pred = ind.predict(data);
67 return score(data.y, y_pred, loss, params.class_weights);
68 }
69};
70
71
72// TODO: improve this so we dont have a lot of different declarations
73template <ProgramType P>
74 requires( P == PT::BinaryClassifier)
76{
77
78using RetType = ArrayXf;
79
80typedef float (*funcPointer)(const VectorXf&,
81 const VectorXf&,
82 VectorXf&,
83 const vector<float>&);
84public:
85 // map the string into a function to be called when calculating the score
86 std::map<string, funcPointer> score_hash;
87 string scorer;
88
89 Scorer(string scorer="log") {
90 score_hash["log"] = &mean_log_loss;
91 score_hash["average_precision_score"] = &average_precision_score;
92
93 this->set_scorer(scorer);
94 };
95
96 void set_scorer(string scorer){ this->scorer = scorer; };
97 string get_scorer(){return this->scorer; };
98
99 /* void set_scorer(string scorer); */
100 float score(const VectorXf& y_true, const VectorXf& y_pred,
101 VectorXf& loss, const vector<float>& w)
102 {
103 if ( score_hash.find(this->scorer) == score_hash.end() )
104 {
105 // not found
106 HANDLE_ERROR_THROW("Scoring function '" + this->scorer
107 + "' not defined");
108 return 0.0;
109 }
110 else
111 {
112 // found
113 return score_hash.at(this->scorer)(y_true, y_pred, loss, w);
114 }
115 };
116
117 float score(Individual<P>& ind, Dataset& data,
118 VectorXf& loss, const Parameters& params)
119 {
120 RetType y_pred = ind.predict_proba(data); // .template cast<float>();
121 return score(data.y, y_pred, loss, params.class_weights);
122 }
123};
124
125template <ProgramType P>
126 requires(P == PT::MulticlassClassifier)
127class Scorer<P>
128{
129
130using RetType = ArrayXXf;
131
132typedef float (*funcPointer)(const VectorXf&,
133 const ArrayXXf&,
134 VectorXf&,
135 const vector<float>&);
136public:
137 // map the string into a function to be called when calculating the score
138 std::map<string, funcPointer> score_hash;
139 string scorer;
140
141 Scorer(string scorer="multi_log") {
142 score_hash["multi_log"] = &mean_multi_log_loss;
143
144 this->set_scorer(scorer);
145 };
146
147 void set_scorer(string scorer){ this->scorer = scorer; };
148 string get_scorer(){return this->scorer; };
149
150 /* void set_scorer(string scorer); */
151 float score(const VectorXf& y_true, const ArrayXXf& y_pred,
152 VectorXf& loss, const vector<float>& w)
153 {
154 // loss is an array passed by reference to store each prediction (used in lexicase)
155 // weights are used to give more or less importance for a given sample.
156 // Every scorer must have the same function signature, but arent required to use all info
157
158 if ( score_hash.find(this->scorer) == score_hash.end() )
159 {
160 // not found
161 HANDLE_ERROR_THROW("Scoring function '" + this->scorer
162 + "' not defined");
163 return 0.0;
164 }
165 else
166 {
167 // found
168 return score_hash.at(this->scorer)(y_true, y_pred, loss, w);
169 }
170 };
171
172 float score(Individual<P>& ind, Dataset& data,
173 VectorXf& loss, const Parameters& params)
174 {
175 RetType y_pred = ind.predict_proba(data); // .template cast<float>();
176 return score(data.y, y_pred, loss, params.class_weights);
177 }
178};
179
180}
181}
182#endif
void bind_engine(py::module &m, string name)
void set_scorer(string scorer)
Definition scorer.h:96
float score(Individual< P > &ind, Dataset &data, VectorXf &loss, const Parameters &params)
Definition scorer.h:117
float score(const VectorXf &y_true, const ArrayXXf &y_pred, VectorXf &loss, const vector< float > &w)
Definition scorer.h:151
Scorer(string scorer="multi_log")
Definition scorer.h:141
Scorer(string scorer="log")
Definition scorer.h:89
std::map< string, funcPointer > score_hash
Definition scorer.h:86
float score(const VectorXf &y_true, const VectorXf &y_pred, VectorXf &loss, const vector< float > &w)
Definition scorer.h:100
string get_scorer()
Definition scorer.h:42
void set_scorer(string scorer)
Definition scorer.h:41
float score(Individual< P > &ind, Dataset &data, VectorXf &loss, const Parameters &params)
Definition scorer.h:63
typename std::conditional_t< P==PT::Regressor, ArrayXf, std::conditional_t< P==PT::Representer, ArrayXXf, ArrayXf > > RetType
Definition scorer.h:20
Scorer(string scorer="mse")
Definition scorer.h:35
std::map< string, funcPointer > score_hash
Definition scorer.h:31
float score(const VectorXf &y_true, const VectorXf &y_pred, VectorXf &loss, const vector< float > &w)
Definition scorer.h:45
float(* funcPointer)(const VectorXf &, const VectorXf &, VectorXf &, const vector< float > &)
Definition scorer.h:25
#define HANDLE_ERROR_THROW(err)
Definition error.h:27
float mean_log_loss(const VectorXf &y, const VectorXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
log loss
Definition metrics.cpp:51
float mean_multi_log_loss(const VectorXf &y, const ArrayXXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
Calculates the mean multinomial log loss between the predicted probabilities and the true labels.
Definition metrics.cpp:169
float average_precision_score(const VectorXf &y, const VectorXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
Calculates the average precision score between the predicted probabilities and the true labels.
Definition metrics.cpp:59
float mse(const VectorXf &y, const VectorXf &yhat, VectorXf &loss, const vector< float > &class_weights)
mean squared error
Definition metrics.cpp:9
< nsga2 selection operator for getting the front
Definition data.cpp:12
Namespace containing scoring functions for evaluation metrics.
vector< float > class_weights
weights for each class
Definition params.h:66