| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.Booster.R |
| 3 |
\name{coef.xgb.Booster} |
| 4 |
\alias{coef.xgb.Booster} |
| 5 |
\title{Extract coefficients from linear booster} |
| 6 |
\usage{ |
| 7 |
\method{coef}{xgb.Booster}(object, ...) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{object}{A fitted booster of 'gblinear' type.} |
| 11 |
|
| 12 |
\item{...}{Not used.} |
| 13 |
} |
| 14 |
\value{ |
| 15 |
The extracted coefficients: |
| 16 |
\itemize{ |
| 17 |
\item If there is only one coefficient per column in the data, will be returned as a |
| 18 |
vector, potentially containing the feature names if available, with the intercept |
| 19 |
as first column. |
| 20 |
\item If there is more than one coefficient per column in the data (e.g. when using |
| 21 |
\code{objective="multi:softmax"}), will be returned as a matrix with dimensions equal |
| 22 |
to \verb{[num_features, num_cols]}, with the intercepts as first row. Note that the column |
| 23 |
(classes in multi-class classification) dimension will not be named. |
| 24 |
} |
| 25 |
|
| 26 |
The intercept returned here will include the 'base_score' parameter (unlike the 'bias' |
| 27 |
or the last coefficient in the model dump, which doesn't have 'base_score' added to it), |
| 28 |
hence one should get the same values from calling \code{predict(..., outputmargin = TRUE)} and |
| 29 |
from performing a matrix multiplication with \code{model.matrix(~., ...)}. |
| 30 |
|
| 31 |
Be aware that the coefficients are obtained by first converting them to strings and |
| 32 |
back, so there will always be some very small lose of precision compared to the actual |
| 33 |
coefficients as used by \link{predict.xgb.Booster}. |
| 34 |
} |
| 35 |
\description{ |
| 36 |
Extracts the coefficients from a 'gblinear' booster object, |
| 37 |
as produced by \code{\link[=xgb.train]{xgb.train()}} when using parameter \code{booster="gblinear"}. |
| 38 |
|
| 39 |
Note: this function will error out if passing a booster model |
| 40 |
which is not of "gblinear" type. |
| 41 |
} |
| 42 |
\examples{ |
| 43 |
library(xgboost) |
| 44 |
|
| 45 |
data(mtcars) |
| 46 |
|
| 47 |
y <- mtcars[, 1] |
| 48 |
x <- as.matrix(mtcars[, -1]) |
| 49 |
|
| 50 |
dm <- xgb.DMatrix(data = x, label = y, nthread = 1) |
| 51 |
params <- xgb.params(booster = "gblinear", nthread = 1) |
| 52 |
model <- xgb.train(data = dm, params = params, nrounds = 2) |
| 53 |
coef(model) |
| 54 |
} |