Add quantile metric. (#8761)
This commit is contained in:
30
tests/cpp/common/test_quantile_utils.cc
Normal file
30
tests/cpp/common/test_quantile_utils.cc
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright 2023 by XGBoost contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "../../../src/common/quantile_loss_utils.h"
|
||||
#include "xgboost/base.h" // Args
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
TEST(QuantileLossParam, Basic) {
|
||||
QuantileLossParam param;
|
||||
auto& ref = param.quantile_alpha.Get();
|
||||
|
||||
param.UpdateAllowUnknown(Args{{"quantile_alpha", "0.3"}});
|
||||
ASSERT_EQ(ref.size(), 1);
|
||||
ASSERT_NEAR(ref[0], 0.3, kRtEps);
|
||||
|
||||
param.UpdateAllowUnknown(Args{{"quantile_alpha", "[0.3, 0.6]"}});
|
||||
ASSERT_EQ(param.quantile_alpha.Get().size(), 2);
|
||||
ASSERT_NEAR(ref[0], 0.3, kRtEps);
|
||||
ASSERT_NEAR(ref[1], 0.6, kRtEps);
|
||||
|
||||
param.UpdateAllowUnknown(Args{{"quantile_alpha", "(0.6, 0.3)"}});
|
||||
ASSERT_EQ(param.quantile_alpha.Get().size(), 2);
|
||||
ASSERT_NEAR(ref[0], 0.6, kRtEps);
|
||||
ASSERT_NEAR(ref[1], 0.3, kRtEps);
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2018-2022 by XGBoost contributors
|
||||
/**
|
||||
* Copyright 2018-2023 by XGBoost contributors
|
||||
*/
|
||||
#include <xgboost/json.h>
|
||||
#include <xgboost/metric.h>
|
||||
@@ -311,5 +311,36 @@ TEST(Metric, DeclareUnifiedTest(MultiRMSE)) {
|
||||
ASSERT_FLOAT_EQ(ret, loss);
|
||||
ASSERT_FLOAT_EQ(ret, loss_w);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(Quantile)) {
|
||||
auto ctx = xgboost::CreateEmptyGenericParam(GPUIDX);
|
||||
std::unique_ptr<Metric> metric{Metric::Create("quantile", &ctx)};
|
||||
|
||||
HostDeviceVector<float> predts{0.1f, 0.9f, 0.1f, 0.9f};
|
||||
std::vector<float> labels{0.5f, 0.5f, 0.9f, 0.1f};
|
||||
std::vector<float> weights{0.2f, 0.4f,0.6f, 0.8f};
|
||||
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.0]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels, weights), 0.400f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.2]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels, weights), 0.376f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.4]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels, weights), 0.352f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.8]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels, weights), 0.304f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[1.0]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels, weights), 0.28f, 0.001f);
|
||||
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.0]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels), 0.3f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.2]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels), 0.3f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.4]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels), 0.3f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[0.8]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels), 0.3f, 0.001f);
|
||||
metric->Configure(Args{{"quantile_alpha", "[1.0]"}});
|
||||
EXPECT_NEAR(GetMetricEval(metric.get(), predts, labels), 0.3f, 0.001f);
|
||||
}
|
||||
} // namespace metric
|
||||
} // namespace xgboost
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from xgboost.testing.metrics import check_quantile_error
|
||||
|
||||
import xgboost
|
||||
from xgboost import testing as tm
|
||||
|
||||
sys.path.append("tests/python")
|
||||
import test_eval_metrics as test_em # noqa
|
||||
@@ -58,3 +60,7 @@ class TestGPUEvalMetrics:
|
||||
|
||||
def test_pr_auc_ltr(self):
|
||||
self.cpu_test.run_pr_auc_ltr("gpu_hist")
|
||||
|
||||
@pytest.mark.skipif(**tm.no_sklearn())
|
||||
def test_quantile_error(self) -> None:
|
||||
check_quantile_error("gpu_hist")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
from xgboost.testing.metrics import check_quantile_error
|
||||
|
||||
import xgboost as xgb
|
||||
from xgboost import testing as tm
|
||||
@@ -306,10 +307,14 @@ class TestEvalMetrics:
|
||||
group=groups,
|
||||
eval_set=[(X, y)],
|
||||
eval_group=[groups],
|
||||
eval_metric="aucpr"
|
||||
eval_metric="aucpr",
|
||||
)
|
||||
results = ltr.evals_result()["validation_0"]["aucpr"]
|
||||
assert results[-1] >= 0.99
|
||||
|
||||
def test_pr_auc_ltr(self):
|
||||
self.run_pr_auc_ltr("hist")
|
||||
|
||||
@pytest.mark.skipif(**tm.no_sklearn())
|
||||
def test_quantile_error(self) -> None:
|
||||
check_quantile_error("hist")
|
||||
|
||||
Reference in New Issue
Block a user