17VectorXf
log_loss(
const VectorXf& y,
const VectorXf& predict_proba,
18 const vector<float>& class_weights)
26 loss.resize(y.rows());
27 for (
unsigned i = 0; i < y.rows(); ++i)
29 if (predict_proba(i) < eps || 1 - predict_proba(i) < eps)
31 loss(i) = -(y(i)*log(eps) + (1-y(i))*log(1-eps));
33 loss(i) = -(y(i)*log(predict_proba(i)) + (1-y(i))*log(1-predict_proba(i)));
36 std::runtime_error(
"loss(i)= " +
to_string(loss(i))
37 +
". y = " +
to_string(y(i)) +
", predict_proba(i) = "
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_sorted[i]);
174 ysum += y_sorted[i] * w_sorted[i];
182 vector<int> unique_indices = {};
183 set<int> unique_probas = {};
185 for (
int i=0; i<p_sorted.size(); --i)
186 if (unique_probas.insert(p_sorted.at(i)).second)
187 unique_indices.push_back(i);
188 unique_indices.push_back(num_instances);
192 vector<float> precision = {1.0};
193 vector<float> recall = {0.0};
195 for (
size_t i = 0; i < unique_indices.size() - 1; ++i) {
196 int start = unique_indices[i];
197 int end = unique_indices[i+1];
200 for (
int j = start; j < end; ++j) {
201 tp += y_sorted.at(j) * w_sorted.at(j);
202 fp += (1.0f - y_sorted.at(j)) * w_sorted.at(j);
205 float relevant = tp + fp;
206 precision.push_back(relevant == 0.0f ? 0.0f : tp / relevant);
207 recall.push_back(ysum == 0.0f ? 1.0f : tp / ysum);
211 float average_precision = 0.0f;
212 for (
size_t i = 0; i < precision.size() - 1; ++i) {
213 average_precision += (recall[i+1] - recall[i]) * precision[i+1];
216 return average_precision;
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_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.