MCPcopy Create free account
hub / github.com/dmlc/xgboost / run_custom_objective

Function run_custom_objective

python-package/xgboost/testing/basic_models.py:14–81  ·  view source on GitHub ↗

Tests custom objective and metric functions.

(  # pylint: disable=too-many-locals
    tree_method: str,
    device: Device,
    dtrain: DMatrix,
    dtest: DMatrix,
)

Source from the content-addressed store, hash-verified

12
13
14def run_custom_objective( # pylint: disable=too-many-locals
15 tree_method: str,
16 device: Device,
17 dtrain: DMatrix,
18 dtest: DMatrix,
19) -> None:
20 """Tests custom objective and metric functions."""
21 param = {
22 "max_depth": 2,
23 "eta": 1,
24 "objective": "reg:logistic",
25 "tree_method": tree_method,
26 "device": device,
27 }
28 watchlist = [(dtest, "eval"), (dtrain, "train")]
29 num_round = 10
30
31 def evalerror(preds: np.ndarray, dtrain: DMatrix) -> Tuple[str, np.float64]:
32 return tm.eval_error_metric(preds, dtrain, rev_link=True)
33
34 # test custom_objective in training
35 bst = train(
36 param,
37 dtrain,
38 num_round,
39 evals=watchlist,
40 obj=tm.logregobj,
41 custom_metric=evalerror,
42 )
43 assert isinstance(bst, Booster)
44 preds = bst.predict(dtest)
45 labels = dtest.get_label()
46 err = sum(1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i]) / float(
47 len(preds)
48 )
49 assert err < 0.1
50
51 # test custom_objective in cross-validation
52 cv(
53 param,
54 dtrain,
55 num_round,
56 nfold=5,
57 seed=0,
58 obj=tm.logregobj,
59 custom_metric=evalerror,
60 )
61
62 # test maximize parameter
63 def neg_evalerror(preds: np.ndarray, dtrain: DMatrix) -> Tuple[str, float]:
64 labels = dtrain.get_label()
65 preds = 1.0 / (1.0 + np.exp(-preds))
66 return "error", float(sum(labels == (preds > 0.0))) / len(labels)
67
68 bst2 = train(
69 param,
70 dtrain,
71 num_round,

Callers 2

test_custom_objectiveMethod · 0.90
test_custom_objectiveMethod · 0.90

Calls 4

cvFunction · 0.85
get_labelMethod · 0.80
trainFunction · 0.50
predictMethod · 0.45

Tested by 2

test_custom_objectiveMethod · 0.72
test_custom_objectiveMethod · 0.72