predict.lgb.Booster.Rd 4.63 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/lgb.Booster.R
\name{predict.lgb.Booster}
\alias{predict.lgb.Booster}
\title{Predict method for LightGBM model}
\usage{
7
8
\method{predict}{lgb.Booster}(
  object,
9
  newdata,
10
  type = "response",
11
  start_iteration = NULL,
12
13
  num_iteration = NULL,
  header = FALSE,
14
  params = list(),
15
16
  ...
)
Guolin Ke's avatar
Guolin Ke committed
17
18
19
20
}
\arguments{
\item{object}{Object of class \code{lgb.Booster}}

21
\item{newdata}{a \code{matrix} object, a \code{dgCMatrix} object or
22
a character representing a path to a text file (CSV, TSV, or LibSVM)}
Guolin Ke's avatar
Guolin Ke committed
23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
\item{type}{Type of prediction to output. Allowed types are:\itemize{
            \item \code{"response"}: will output the predicted score according to the objective function being
                  optimized (depending on the link function that the objective uses), after applying any necessary
                  transformations - for example, for \code{objective="binary"}, it will output class probabilities.
            \item \code{"class"}: for classification objectives, will output the class with the highest predicted
                  probability. For other objectives, will output the same as "response".
            \item \code{"raw"}: will output the non-transformed numbers (sum of predictions from boosting iterations'
                  results) from which the "response" number is produced for a given objective function - for example,
                  for \code{objective="binary"}, this corresponds to log-odds. For many objectives such as
                  "regression", since no transformation is applied, the output will be the same as for "response".
            \item \code{"leaf"}: will output the index of the terminal node / leaf at which each observations falls
                  in each tree in the model, outputted as integers, with one column per tree.
            \item \code{"contrib"}: will return the per-feature contributions for each prediction, including an
                  intercept (each feature will produce one column). If there are multiple classes, each class will
                  have separate feature contributions (thus the number of columns is features+1 multiplied by the
                  number of classes).
            }

            Note that, if using custom objectives, types "class" and "response" will not be available and will
            default towards using "raw" instead.}

45
46
47
48
49
50
51
52
53
\item{start_iteration}{int or None, optional (default=None)
Start index of the iteration to predict.
If None or <= 0, starts from the first iteration.}

\item{num_iteration}{int or None, optional (default=None)
Limit number of iterations in the prediction.
If None, if the best iteration exists and start_iteration is None or <= 0, the
best iteration is used; otherwise, all iterations from start_iteration are used.
If <= 0, all iterations from start_iteration are used (no limits).}
Guolin Ke's avatar
Guolin Ke committed
54
55
56

\item{header}{only used for prediction for text file. True if text file has header}

57
58
59
\item{params}{a list of additional named parameters. See
\href{https://lightgbm.readthedocs.io/en/latest/Parameters.html#predict-parameters}{
the "Predict Parameters" section of the documentation} for a list of parameters and
60
61
valid values. Where these conflict with the values of keyword arguments to this function,
the values in \code{params} take precedence.}
62

63
\item{...}{ignored}
Guolin Ke's avatar
Guolin Ke committed
64
65
}
\value{
66
67
68
For prediction types that are meant to always return one output per observation (e.g. when predicting
        \code{type="response"} on a binary classification or regression objective), will return a vector with one
        element per row in \code{newdata}.
Guolin Ke's avatar
Guolin Ke committed
69

70
71
72
        For prediction types that are meant to return more than one output per observation (e.g. when predicting
        \code{type="response"} on a multi-class objective, or when predicting \code{type="leaf"}, regardless of
        objective), will return a matrix with one row per observation in \code{newdata} and one column per output.
Guolin Ke's avatar
Guolin Ke committed
73
74
75
76
77
}
\description{
Predicted values based on class \code{lgb.Booster}
}
\examples{
78
\donttest{
Guolin Ke's avatar
Guolin Ke committed
79
80
81
82
83
84
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
data(agaricus.test, package = "lightgbm")
test <- agaricus.test
dtest <- lgb.Dataset.create.valid(dtrain, test$data, label = test$label)
85
86
87
88
89
90
params <- list(
  objective = "regression"
  , metric = "l2"
  , min_data = 1L
  , learning_rate = 1.0
)
Guolin Ke's avatar
Guolin Ke committed
91
valids <- list(test = dtest)
92
93
94
model <- lgb.train(
  params = params
  , data = dtrain
95
  , nrounds = 5L
96
97
  , valids = valids
)
Guolin Ke's avatar
Guolin Ke committed
98
preds <- predict(model, test$data)
99
100

# pass other prediction parameters
101
preds <- predict(
102
103
104
105
106
107
    model,
    test$data,
    params = list(
        predict_disable_shape_check = TRUE
   )
)
Guolin Ke's avatar
Guolin Ke committed
108
}
109
}