R: Load LightGBM model

lgb.load {lightgbm} | R Documentation

## Load LightGBM model

### Description

Load LightGBM takes in either a file path or model string. If both are provided, Load will default to loading from file

### Usage

    lgb.load(filename = NULL, model_str = NULL)

### Arguments

`filename` | path of model file
`model_str` | a str containing the model

### Value

lgb.Booster

### Examples

    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)
    params <- list(objective = "regression", metric = "l2")
    valids <- list(test = dtest)
    model <- lgb.train(
      params = params
      , data = dtrain
      , nrounds = 5L
      , valids = valids
      , min_data = 1L
      , learning_rate = 1.0
      , early_stopping_rounds = 3L
    )
    model_file <- tempfile(fileext = ".txt")
    lgb.save(model, model_file)
    load_booster <- lgb.load(filename = model_file)
    model_string <- model$save_model_to_string(NULL) # saves best iteration
    load_booster_from_str <- lgb.load(model_str = model_string)

[Package lightgbm version 3.1.1 Index]