46 const VectorXf& predict_proba, VectorXf& loss,
47 const vector<float>& class_weights)
49 loss =
log_loss(y,predict_proba,class_weights);
51 if (!class_weights.empty())
53 float sum_weights = 0;
56 VectorXf weighted_loss;
57 weighted_loss.resize(y.rows());
58 for (
unsigned i = 0; i < y.rows(); ++i)
60 weighted_loss(i) = loss(i) * class_weights.at(y(i));
61 sum_weights += class_weights.at(y(i));
66 return weighted_loss.sum() / sum_weights;
74 const VectorXf& predict_proba, VectorXf& loss,
75 const vector<float>& class_weights )
77 VectorXi yhat = (predict_proba.array() > 0.5).cast<
int>();
80 loss = (yhat.array() != y.cast<
int>().array()).cast<
float>();
84 if (!class_weights.empty()) {
85 for (
int i = 0; i < y.rows(); ++i) {
86 loss(i) *= class_weights.at(y(i));
87 scale += class_weights.at(y(i));
92 scale =
static_cast<float>(loss.size());
96 return 1.0 - (loss.sum() / scale);
101 const VectorXf& predict_proba, VectorXf& loss,
102 const vector<float>& class_weights )
104 VectorXi yhat = (predict_proba.array() > 0.5).cast<
int>();
106 loss = (yhat.array() != y.cast<
int>().array()).cast<
float>();
113 int num_instances = y.rows();
114 for (
int i = 0; i < num_instances; ++i) {
118 if (yhat(i) == 1.0 && y(i) == 1.0) TP += weight;
119 else if (yhat(i) == 1.0 && y(i) == 0.0) FP += weight;
120 else if (yhat(i) == 0.0 && y(i) == 0.0) TN += weight;
126 float TPR = (TP + eps) / (TP + FN + eps);
127 float TNR = (TN + eps) / (TN + FP + eps);
129 return (TPR + TNR) / 2.0;
134 const vector<float>& class_weights) {
141 int num_instances = y.size();
144 loss.resize(num_instances);
145 for (
int i = 0; i < num_instances; ++i) {
146 float p = predict_proba(i);
150 if (p < eps || 1 - p < eps)
151 loss(i) = -(y(i)*log(eps) + (1-y(i))*log(1-eps));
153 loss(i) = -(y(i)*log(p) + (1-y(i))*log(1-p));
157 vector<int> order(num_instances);
158 iota(order.begin(), order.end(), 0);
159 stable_sort(order.begin(), order.end(), [&](
int i,
int j) {
160 return predict_proba(i) > predict_proba(j);
164 vector<float> y_sorted(num_instances);
165 vector<float> p_sorted(num_instances);
166 vector<float> w_sorted(num_instances);
167 for (
int i = 0; i < num_instances; ++i) {
170 y_sorted[i] = y(idx);
171 p_sorted[i] = predict_proba(idx);
172 w_sorted[i] = class_weights.empty() ? 1.0f : class_weights.at(y(idx));
174 ysum += y_sorted[i] * w_sorted[i];
185 if (abs(p_sorted.back() - p_sorted.front()) <= eps) {
187 float total_weight = std::accumulate(w_sorted.begin(), w_sorted.end(), 0.0f);
191 return total_weight == 0.0f ? 0.0f : ysum / total_weight;
195 vector<int> unique_indices = {};
196 set<int> unique_probas = {};
198 for (
int i=0; i<p_sorted.size(); ++i)
199 if (unique_probas.insert(p_sorted.at(i)).second)
200 unique_indices.push_back(i);
202 unique_indices.push_back(num_instances);
206 vector<float> precision = {1.0};
207 vector<float> recall = {0.0};
209 for (
size_t i = 0; i < unique_indices.size() - 1; ++i) {
210 int start = unique_indices[i];
211 int end = unique_indices[i+1];
214 for (
int j = start; j < end; ++j) {
215 tp += y_sorted.at(j) * w_sorted.at(j);
216 fp += (1.0f - y_sorted.at(j)) * w_sorted.at(j);
218 float relevant = tp + fp;
219 precision.push_back(relevant == 0.0f ? 0.0f : tp / relevant);
220 recall.push_back(ysum == 0.0f ? 1.0f : tp / ysum);
225 float average_precision = 0.0f;
226 for (
size_t i = 0; i < num_instances; ++i) {
227 average_precision += (recall[i+1] - recall[i]) * precision[i+1];
230 return average_precision;
float zero_one_loss(const VectorXf &y, const VectorXf &predict_proba, VectorXf &loss, const vector< float > &class_weights)
Accuracy for binary classification.
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.
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.