9float mse(
const VectorXf& y,
const VectorXf& yhat, VectorXf& loss,
10 const vector<float>& class_weights)
12 loss = (yhat - y).array().pow(2);
17VectorXf
log_loss(
const VectorXf& y,
const VectorXf& predict_proba,
18 const vector<float>& class_weights)
20 float eps = pow(10,-10);
24 loss.resize(y.rows());
25 for (
unsigned i = 0; i < y.rows(); ++i)
27 if (predict_proba(i) < eps || 1 - predict_proba(i) < eps)
29 loss(i) = -(y(i)*log(eps) + (1-y(i))*log(1-eps));
31 loss(i) = -(y(i)*log(predict_proba(i)) + (1-y(i))*log(1-predict_proba(i)));
34 std::runtime_error(
"loss(i)= " +
to_string(loss(i))
35 +
". y = " +
to_string(y(i)) +
", predict_proba(i) = "
44 const VectorXf& predict_proba, VectorXf& loss,
45 const vector<float>& class_weights)
47 loss =
log_loss(y,predict_proba,class_weights);
49 if (!class_weights.empty())
51 float sum_weights = 0;
54 VectorXf weighted_loss;
55 weighted_loss.resize(y.rows());
56 for (
unsigned i = 0; i < y.rows(); ++i)
58 weighted_loss(i) = loss(i) * class_weights.at(y(i));
59 sum_weights += class_weights.at(y(i));
64 return weighted_loss.sum() / sum_weights;
72 const VectorXf& predict_proba, VectorXf& loss,
73 const vector<float>& class_weights )
75 VectorXi yhat = (predict_proba.array() > 0.5).cast<
int>();
78 loss = (yhat.array() != y.cast<
int>().array()).cast<
float>();
81 if (!class_weights.empty()) {
82 for (
int i = 0; i < y.rows(); ++i) {
83 loss(i) *= class_weights.at(y(i));
88 return 1.0 - loss.mean();
93 const VectorXf& predict_proba, VectorXf& loss,
94 const vector<float>& class_weights )
96 VectorXi yhat = (predict_proba.array() > 0.5).cast<
int>();
98 loss = (yhat.array() != y.cast<
int>().array()).cast<
float>();
105 int num_instances = y.rows();
106 for (
int i = 0; i < num_instances; ++i) {
110 if (yhat(i) == 1.0 && y(i) == 1.0) TP += weight;
111 else if (yhat(i) == 1.0 && y(i) == 0.0) FP += weight;
112 else if (yhat(i) == 0.0 && y(i) == 0.0) TN += weight;
116 float eps = pow(10,-10);
118 float TPR = (TP + eps) / (TP + FN + eps);
119 float TNR = (TN + eps) / (TN + FP + eps);
121 return (TPR + TNR) / 2.0;
126 const vector<float>& class_weights) {
130 float eps = pow(10,-10);
133 int num_instances = y.rows();
136 vector<int>
argsort(num_instances);
140 return predict_proba(i) > predict_proba(j);
144 if (!class_weights.empty())
145 for (
int i = 0; i < y.size(); i++) {
146 ysum += y(i) * class_weights.at(y(i));
152 VectorXf precision(num_instances);
153 VectorXf recall(num_instances);
155 float true_positives = 0;
156 float false_positives = 0;
157 float positives = ysum;
161 for (
int i = 0; i < num_instances; ++i) {
164 float weight = class_weights.empty() ? 1.0f : class_weights.at(y(index));
166 if (y(index) > 0.5) {
167 true_positives += weight;
170 false_positives += weight;
173 int relevant = true_positives+false_positives;
175 precision(i) = relevant==0.0 ? 0.0 : true_positives/relevant;
176 recall(i) = ysum==0.0 ? 1.0 : true_positives/ysum;
180 float average_precision = 0.0;
181 loss.resize(num_instances);
183 for (
int i = 0; i < num_instances; ++i) {
185 average_precision += (recall(i) - recall(i-1)) * precision(i);
188 float p = predict_proba(index);
192 if (p < eps || 1 - p < eps)
193 loss(index) = -(y(index)*log(eps) + (1-y(index))*log(1-eps));
195 loss(index) = -(y(index)*log(p) + (1-y(index))*log(1-p));
198 return average_precision;
203 const vector<float>& class_weights)
206 VectorXf loss = VectorXf::Zero(y.rows());
254 const ArrayXXf& predict_proba, VectorXf& loss,
255 const vector<float>& class_weights)
265 const ArrayXXf& predict_proba, VectorXf& loss,
266 const vector<float>& class_weights )
float multi_zero_one_loss(const VectorXf &y, const ArrayXXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
Accuracy for multi-classification.
float zero_one_loss(const VectorXf &y, const VectorXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
Accuracy for binary classification.
float mean_log_loss(const VectorXf &y, const VectorXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
log loss
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.
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.
float mse(const VectorXf &y, const VectorXf &yhat, VectorXf &loss, const vector< float > &class_weights)
mean squared error
VectorXf multi_log_loss(const VectorXf &y, const ArrayXXf &predict_proba, const vector< float > &class_weights)
Calculates the multinomial log loss between the predicted probabilities and the true labels.
VectorXf log_loss(const VectorXf &y, const VectorXf &predict_proba, const vector< float > &class_weights)
Calculates the log loss between the predicted probabilities and the true labels.
float bal_zero_one_loss(const VectorXf &y, const VectorXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
Balanced accuracy for binary classification.
vector< size_t > argsort(const vector< T > &v, bool ascending=true)
return indices that sort a vector
string to_string(const T &value)
template function to convert objects to string for logging
< nsga2 selection operator for getting the front