1#ifndef LIGHTGBM_METRIC_BINARY_METRIC_HPP_
2#define LIGHTGBM_METRIC_BINARY_METRIC_HPP_
4#include <LightGBM/metric.h>
6#include <LightGBM/utils/log.h>
7#include <LightGBM/utils/common.h>
19template<
typename Po
intWiseLossCalculator>
29 name_.emplace_back(PointWiseLossCalculator::Name());
33 label_ = metadata.
label();
38 if (weights_ ==
nullptr) {
39 sum_weights_ =
static_cast<double>(num_data_);
43 sum_weights_ += weights_[i];
48 const std::vector<std::string>& GetName()
const override {
52 double factor_to_bigger_better()
const override {
57 double sum_loss = 0.0f;
58 if (objective ==
nullptr) {
59 if (weights_ ==
nullptr) {
60 #pragma omp parallel for schedule(static) reduction(+:sum_loss)
63 sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], score[i]);
66 #pragma omp parallel for schedule(static) reduction(+:sum_loss)
69 sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], score[i]) * weights_[i];
73 if (weights_ ==
nullptr) {
74 #pragma omp parallel for schedule(static) reduction(+:sum_loss)
77 objective->ConvertOutput(&score[i], &prob);
79 sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], prob);
82 #pragma omp parallel for schedule(static) reduction(+:sum_loss)
85 objective->ConvertOutput(&score[i], &prob);
87 sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], prob) * weights_[i];
91 double loss = sum_loss / sum_weights_;
92 return std::vector<double>(1, loss);
105 std::vector<std::string> name_;
115 inline static double LossOnPoint(
label_t label,
double prob) {
117 if (1.0f - prob > kEpsilon) {
118 return -std::log(1.0f - prob);
121 if (prob > kEpsilon) {
122 return -std::log(prob);
125 return -std::log(kEpsilon);
128 inline static const char* Name() {
129 return "binary_logloss";
139 inline static double LossOnPoint(
label_t label,
double prob) {
147 inline static const char* Name() {
148 return "binary_error";
163 const std::vector<std::string>& GetName()
const override {
167 double factor_to_bigger_better()
const override {
172 name_.emplace_back(
"auc");
174 num_data_ = num_data;
176 label_ = metadata.
label();
180 if (weights_ ==
nullptr) {
181 sum_weights_ =
static_cast<double>(num_data_);
185 sum_weights_ += weights_[i];
192 std::vector<data_size_t> sorted_idx;
194 sorted_idx.emplace_back(i);
196 Common::ParallelSort(sorted_idx.begin(), sorted_idx.end(), [score](
data_size_t a,
data_size_t b) {return score[a] > score[b]; });
198 double cur_pos = 0.0f;
200 double sum_pos = 0.0f;
204 double cur_neg = 0.0f;
205 double threshold = score[sorted_idx[0]];
206 if (weights_ ==
nullptr) {
208 const label_t cur_label = label_[sorted_idx[i]];
209 const double cur_score = score[sorted_idx[i]];
211 if (cur_score != threshold) {
212 threshold = cur_score;
214 accum += cur_neg*(cur_pos * 0.5f + sum_pos);
217 cur_neg = cur_pos = 0.0f;
219 cur_neg += (cur_label <= 0);
220 cur_pos += (cur_label > 0);
224 const label_t cur_label = label_[sorted_idx[i]];
225 const double cur_score = score[sorted_idx[i]];
226 const label_t cur_weight = weights_[sorted_idx[i]];
228 if (cur_score != threshold) {
229 threshold = cur_score;
231 accum += cur_neg*(cur_pos * 0.5f + sum_pos);
234 cur_neg = cur_pos = 0.0f;
236 cur_neg += (cur_label <= 0)*cur_weight;
237 cur_pos += (cur_label > 0)*cur_weight;
240 accum += cur_neg*(cur_pos * 0.5f + sum_pos);
243 if (sum_pos > 0.0f && sum_pos != sum_weights_) {
244 auc = accum / (sum_pos *(sum_weights_ - sum_pos));
246 return std::vector<double>(1, auc);
259 std::vector<std::string> name_;
Auc Metric for binary classification task.
Definition binary_metric.hpp:155
void Init(const Metadata &metadata, data_size_t num_data) override
Initialize.
Definition binary_metric.hpp:171
std::vector< double > Eval(const double *score, const ObjectiveFunction *) const override
Calcaluting and printing metric result.
Definition binary_metric.hpp:190
Error rate metric for binary classification task.
Definition binary_metric.hpp:135
Log loss metric for binary classification task.
Definition binary_metric.hpp:111
Metric for binary classification task. Use static class "PointWiseLossCalculator" to calculate loss p...
Definition binary_metric.hpp:20
std::vector< double > Eval(const double *score, const ObjectiveFunction *objective) const override
Calcaluting and printing metric result.
Definition binary_metric.hpp:56
void Init(const Metadata &metadata, data_size_t num_data) override
Initialize.
Definition binary_metric.hpp:28
The interface of metric. Metric is used to calculate metric result.
Definition metric.h:20
The interface of Objective Function.
Definition objective_function.h:13
desc and descl2 fields must be written in reStructuredText format
Definition application.h:10
float label_t
Type of metadata, include weight and label.
Definition meta.h:33
int32_t data_size_t
Type of data size, it is better to use signed type.
Definition meta.h:14