lgb.save.Rd 1.36 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{lgb.save}
\alias{lgb.save}
\title{Save LightGBM model}
\usage{
7
lgb.save(booster, filename, num_iteration = NULL, start_iteration = 1L)
Guolin Ke's avatar
Guolin Ke committed
8
9
10
11
}
\arguments{
\item{booster}{Object of class \code{lgb.Booster}}

12
\item{filename}{Saved filename}
Guolin Ke's avatar
Guolin Ke committed
13

14
15
16
17
18
\item{num_iteration}{Number of iterations to save, NULL or <= 0 means use best iteration}

\item{start_iteration}{Index (1-based) of the first boosting round to save.
For example, passing \code{start_iteration=5, num_iteration=3} for a regression model
means "save the fifth, sixth, and seventh tree"}
Guolin Ke's avatar
Guolin Ke committed
19
20
}
\value{
21
lgb.Booster
Guolin Ke's avatar
Guolin Ke committed
22
23
24
25
26
}
\description{
Save LightGBM model
}
\examples{
27
\donttest{
28
29
\dontshow{setLGBMthreads(2L)}
\dontshow{data.table::setDTthreads(1L)}
Guolin Ke's avatar
Guolin Ke committed
30
31
32
33
34
35
36
library(lightgbm)
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)
37
38
39
40
41
params <- list(
  objective = "regression"
  , metric = "l2"
  , min_data = 1L
  , learning_rate = 1.0
42
  , num_threads = 2L
43
)
Guolin Ke's avatar
Guolin Ke committed
44
valids <- list(test = dtest)
45
46
47
model <- lgb.train(
  params = params
  , data = dtrain
48
  , nrounds = 10L
49
  , valids = valids
50
  , early_stopping_rounds = 5L
51
)
52
lgb.save(model, tempfile(fileext = ".txt"))
Guolin Ke's avatar
Guolin Ke committed
53
}
54
}