From 6c346e910d3ac6f388c8c7b81489aa439d0f0e17 Mon Sep 17 00:00:00 2001 From: Guolin Ke Date: Mon, 28 Jan 2019 16:14:43 +0800 Subject: [PATCH] fix more edge cases in mape --- src/objective/regression_objective.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/objective/regression_objective.hpp b/src/objective/regression_objective.hpp index 99ae96e9f8d0..07d1a7bf62b3 100644 --- a/src/objective/regression_objective.hpp +++ b/src/objective/regression_objective.hpp @@ -9,6 +9,7 @@ namespace LightGBM { #define PercentileFun(T, data_reader, cnt_data, alpha) {\ + if (cnt_data <= 1) { return data_reader(0); }\ std::vector ref_data(cnt_data);\ for (data_size_t i = 0; i < cnt_data; ++i) {\ ref_data[i] = data_reader(i);\ @@ -36,6 +37,7 @@ namespace LightGBM { }\ #define WeightedPercentileFun(T, data_reader, weight_reader, cnt_data, alpha) {\ + if (cnt_data <= 1) { return data_reader(0); }\ std::vector sorted_idx(cnt_data);\ for (data_size_t i = 0; i < cnt_data; ++i) {\ sorted_idx[i] = i;\ @@ -48,6 +50,7 @@ namespace LightGBM { }\ double threshold = weighted_cdf[cnt_data - 1] * alpha;\ size_t pos = std::upper_bound(weighted_cdf.begin(), weighted_cdf.end(), threshold) - weighted_cdf.begin();\ + pos = std::min(pos, static_cast(cnt_data -1));\ if (pos == 0 || pos == static_cast(cnt_data - 1)) {\ return data_reader(sorted_idx[pos]);\ }\