metric.h 3.42 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef LIGHTGBM_METRIC_H_
#define LIGHTGBM_METRIC_H_

#include <LightGBM/meta.h>
#include <LightGBM/config.h>
#include <LightGBM/dataset.h>

#include <vector>

namespace LightGBM {

/*!
* \brief The interface of metric.
14
*        Metric is used to calculate metric result
Guolin Ke's avatar
Guolin Ke committed
15
16
17
18
19
20
21
22
23
24
25
26
*/
class Metric {
public:
  /*! \brief virtual destructor */
  virtual ~Metric() {}

  /*!
  * \brief Initialize
  * \param test_name Specific name for this metric, will output on log
  * \param metadata Label data
  * \param num_data Number of data
  */
Guolin Ke's avatar
Guolin Ke committed
27
  virtual void Init(const Metadata& metadata, data_size_t num_data) = 0;
Guolin Ke's avatar
Guolin Ke committed
28

Guolin Ke's avatar
Guolin Ke committed
29
  virtual const std::vector<std::string>& GetName() const = 0;
30

31
  virtual double factor_to_bigger_better() const = 0;
Guolin Ke's avatar
Guolin Ke committed
32
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
33
  * \brief Calcaluting and printing metric result
Guolin Ke's avatar
Guolin Ke committed
34
35
  * \param score Current prediction score
  */
36
  virtual std::vector<double> Eval(const double* score) const = 0;
Guolin Ke's avatar
Guolin Ke committed
37

Guolin Ke's avatar
Guolin Ke committed
38
39
40
41
42
43
  Metric() = default;
  /*! \brief Disable copy */
  Metric& operator=(const Metric&) = delete;
  /*! \brief Disable copy */
  Metric(const Metric&) = delete;

Guolin Ke's avatar
Guolin Ke committed
44
45
46
47
48
  /*!
  * \brief Create object of metrics
  * \param type Specific type of metric
  * \param config Config for metric
  */
49
  LIGHTGBM_EXPORT static Metric* CreateMetric(const std::string& type, const MetricConfig& config);
wxchan's avatar
wxchan committed
50

Guolin Ke's avatar
Guolin Ke committed
51
52
53
54
55
56
57
58
59
60
61
};

/*!
* \brief Static class, used to calculate DCG score
*/
class DCGCalculator {
public:
  /*!
  * \brief Initial logic
  * \param label_gain Gain for labels, default is 2^i - 1
  */
62
  static void Init(std::vector<double> label_gain);
Guolin Ke's avatar
Guolin Ke committed
63
64
65

  /*!
  * \brief Calculate the DCG score at position k
Qiwei Ye's avatar
Qiwei Ye committed
66
  * \param k The position to evaluate
Guolin Ke's avatar
Guolin Ke committed
67
68
69
70
71
  * \param label Pointer of label
  * \param score Pointer of score
  * \param num_data Number of data
  * \return The DCG score
  */
72
73
  static double CalDCGAtK(data_size_t k, const float* label,
    const double* score, data_size_t num_data);
Guolin Ke's avatar
Guolin Ke committed
74
75
76

  /*!
  * \brief Calculate the DCG score at multi position
Qiwei Ye's avatar
Qiwei Ye committed
77
  * \param ks The positions to evaluate
Guolin Ke's avatar
Guolin Ke committed
78
79
80
81
82
83
  * \param label Pointer of label
  * \param score Pointer of score
  * \param num_data Number of data
  * \param out Output result
  */
  static void CalDCG(const std::vector<data_size_t>& ks,
84
85
    const float* label, const double* score,
    data_size_t num_data, std::vector<double>* out);
Guolin Ke's avatar
Guolin Ke committed
86
87
88
89
90
91
92
93

  /*!
  * \brief Calculate the Max DCG score at position k
  * \param k The position want to eval at
  * \param label Pointer of label
  * \param num_data Number of data
  * \return The max DCG score
  */
94
  static double CalMaxDCGAtK(data_size_t k,
Guolin Ke's avatar
Guolin Ke committed
95
96
97
98
99
100
101
102
103
104
    const float* label, data_size_t num_data);

  /*!
  * \brief Calculate the Max DCG score at multi position
  * \param ks The positions want to eval at
  * \param label Pointer of label
  * \param num_data Number of data
  * \param out Output result
  */
  static void CalMaxDCG(const std::vector<data_size_t>& ks,
105
    const float* label, data_size_t num_data, std::vector<double>* out);
Guolin Ke's avatar
Guolin Ke committed
106
107
108
109
110
111

  /*!
  * \brief Get discount score of position k
  * \param k The position
  * \return The discount of this position
  */
112
  inline static double GetDiscount(data_size_t k) { return discount_[k]; }
Guolin Ke's avatar
Guolin Ke committed
113
114
115
116
117

private:
  /*! \brief True if inited, avoid init multi times */
  static bool is_inited_;
  /*! \brief store gains for different label */
118
  static std::vector<double> label_gain_;
Guolin Ke's avatar
Guolin Ke committed
119
  /*! \brief store discount score for different position */
120
  static std::vector<double> discount_;
Guolin Ke's avatar
Guolin Ke committed
121
122
123
124
125
126
127
128
  /*! \brief max position for eval */
  static const data_size_t kMaxPosition;
};


}  // namespace LightGBM


Guolin Ke's avatar
Guolin Ke committed
129
#endif   // LightGBM_METRIC_H_