@@ -9,12 +9,28 @@ automatically based on targets upon training. The behavior can be controlled by
99``base_score `` to a constant value. The following snippet disables the automatic
1010estimation:
1111
12- .. code-block :: python
12+ .. tabs ::
13+ .. code-tab :: py
1314
14- import xgboost as xgb
15+ import xgboost as xgb
1516
16- reg = xgb.XGBRegressor()
17- reg.set_params(base_score = 0.5 )
17+ clf = xgb.XGBClassifier(n_estimators=10)
18+ clf.set_params(base_score=0.5)
19+
20+ .. code-tab :: r R
21+
22+ library(xgboost)
23+
24+ # Load built-in dataset
25+ data(agaricus.train, package = "xgboost")
26+
27+ # Set base_score parameter directly
28+ model <- xgboost(
29+ x = agaricus.train$data,
30+ y = factor(agaricus.train$label),
31+ base_score = 0.5,
32+ nrounds = 10
33+ )
1834
1935In addition, here 0.5 represents the value after applying the inverse link function. See
2036the end of the document for a description.
@@ -24,22 +40,53 @@ Other than the ``base_score``, users can also provide global bias via the data f
2440and multi-class, the ``base_margin `` is a matrix with size ``(n_samples, n_targets) `` or
2541``(n_samples, n_classes) ``.
2642
27- .. code-block :: python
43+ .. tabs ::
44+ .. code-tab :: py
45+
46+ import xgboost as xgb
47+ from sklearn.datasets import make_classification
48+
49+ X, y = make_classification()
50+
51+ clf = xgb.XGBClassifier()
52+ clf.fit(X, y)
53+ # Request for raw prediction
54+ m = clf.predict(X, output_margin=True)
55+
56+ clf_1 = xgb.XGBClassifier()
57+ # Feed the prediction into the next model
58+ # Using base margin overrides the base score, see below sections.
59+ clf_1.fit(X, y, base_margin=m)
60+ clf_1.predict(X, base_margin=m)
61+
62+ .. code-tab :: r R
63+
64+ library(xgboost)
65+
66+ # Load built-in dataset
67+ data(agaricus.train, package = "xgboost")
2868
29- import xgboost as xgb
30- from sklearn.datasets import make_regression
69+ # Train first model
70+ model_1 <- xgboost(
71+ x = agaricus.train$data,
72+ y = factor(agaricus.train$label),
73+ nrounds = 10
74+ )
3175
32- X, y = make_regression()
76+ # Request for raw prediction
77+ m <- predict(model_1, agaricus.train$data, type = "raw")
3378
34- reg = xgb.XGBRegressor()
35- reg.fit(X, y)
36- # Request for raw prediction
37- m = reg.predict(X, output_margin = True )
79+ # Feed the prediction into the next model using base_margin
80+ # Using base margin overrides the base score, see below sections.
81+ model_2 <- xgboost(
82+ x = agaricus.train$data,
83+ y = factor(agaricus.train$label),
84+ base_margin = m,
85+ nrounds = 10
86+ )
3887
39- reg_1 = xgb.XGBRegressor()
40- # Feed the prediction into the next model
41- reg_1.fit(X, y, base_margin = m)
42- reg_1.predict(X, base_margin = m)
88+ # Make predictions with base_margin
89+ pred <- predict(model_2, agaricus.train$data, base_margin = m)
4390
4491
4592It specifies the bias for each sample and can be used for stacking an XGBoost model on top
@@ -145,49 +192,101 @@ Example
145192The following example shows the relationship between ``base_score `` and ``base_margin ``
146193using binary logistic with a `logit ` link function:
147194
148- .. code-block :: python
195+ .. tabs ::
196+ .. code-tab :: py
149197
150- import numpy as np
151- from scipy.special import logit
152- from sklearn.datasets import make_classification
153- from xgboost import train, DMatrix
198+ import numpy as np
199+ from scipy.special import logit
200+ from sklearn.datasets import make_classification
154201
155- X, y = make_classification(random_state = 2025 )
202+ import xgboost as xgb
203+
204+ X, y = make_classification(random_state=2025)
205+
206+ .. code-tab :: r R
207+
208+ library(xgboost)
209+
210+ # Load built-in dataset
211+ data(agaricus.train, package = "xgboost")
212+ X <- agaricus.train$data
213+ y <- agaricus.train$label
156214
157215The intercept is a valid probability (0.5). It's used as the initial estimation of the
158216probability of obtaining a positive sample.
159217
160- .. code-block :: python
218+ .. tabs ::
219+ .. code-tab :: py
220+
221+ intercept = 0.5
222+
223+ .. code-tab :: r R
161224
162- intercept = 0.5
225+ intercept <- 0.5
163226
164227First we use the intercept to train a model:
165228
166- .. code-block :: python
229+ .. tabs ::
230+ .. code-tab :: py
167231
168- booster = train(
169- {" base_score" : intercept, " objective" : " binary:logistic" },
170- dtrain = DMatrix(X, y),
171- num_boost_round = 1 ,
172- )
173- predt_0 = booster.predict(DMatrix(X, y))
232+ booster = xgb. train(
233+ {"base_score": intercept, "objective": "binary:logistic"},
234+ dtrain=xgb. DMatrix(X, y),
235+ num_boost_round=1,
236+ )
237+ predt_0 = booster.predict(xgb. DMatrix(X, y))
174238
175- Apply :py:func: `~scipy.special.logit ` to obtain the "margin":
239+ .. code-tab :: r R
240+
241+ # First model with base_score
242+ model_0 <- xgboost(
243+ x = X, y = factor(y),
244+ base_score = intercept,
245+ objective = "binary:logistic",
246+ nrounds = 1
247+ )
248+ predt_0 <- predict(model_0, X)
176249
177- .. code-block :: python
250+ Apply :py:func: ` ~scipy.special.logit ` to obtain the "margin":
178251
179- margin = np.full(y.shape, fill_value = logit(intercept), dtype = np.float32)
180- Xy = DMatrix(X, y, base_margin = margin)
181- # 0.2 is a dummy value to show that `base_margin` overrides `base_score`.
182- booster = train(
183- {" base_score" : 0.2 , " objective" : " binary:logistic" },
184- dtrain = Xy,
185- num_boost_round = 1 ,
186- )
187- predt_1 = booster.predict(Xy)
252+ .. tabs ::
253+ .. code-tab :: py
254+
255+ # Apply logit function to obtain the "margin"
256+ margin = np.full(y.shape, fill_value=logit(intercept), dtype=np.float32)
257+ Xy = xgb.DMatrix(X, y, base_margin=margin)
258+ # Second model with base_margin
259+ # 0.2 is a dummy value to show that `base_margin ` overrides `base_score `.
260+ booster = xgb.train(
261+ {"base_score": 0.2, "objective": "binary:logistic"},
262+ dtrain=Xy,
263+ num_boost_round=1,
264+ )
265+ predt_1 = booster.predict(Xy)
266+
267+ .. code-tab :: r R
268+
269+ # Apply logit function to obtain the "margin"
270+ logit_intercept <- log(intercept / (1 - intercept))
271+ margin <- rep(logit_intercept, length(y))
272+ # Second model with base_margin
273+ # 0.2 is a dummy value to show that `base_margin ` overrides `base_score `
274+ model_1 <- xgboost(
275+ x = X, y = factor(y),
276+ base_margin = margin,
277+ base_score = 0.2,
278+ objective = "binary:logistic",
279+ nrounds = 1
280+ )
281+ predt_1 <- predict(model_1, X, base_margin = margin)
188282
189283Compare the results:
190284
191- .. code-block :: python
285+ .. tabs ::
286+ .. code-tab :: py
287+
288+ np.testing.assert_allclose(predt_0, predt_1)
289+
290+ .. code-tab :: r R
192291
193- np.testing.assert_allclose (predt_0, predt_1)
292+ all.equal (predt_0, predt_1, tolerance = 1e-6 )
0 commit comments