ff_train: training a model
The ff_train
function is designed to train an XGBoost model for deforestation prediction using the ForestForesight algorithm. It takes prepared data (usually from ff_prep
), sets up the XGBoost parameters, trains the model, and optionally saves it. The function allows for customization of various XGBoost parameters and supports both training and validation data.
Exercises
Basic Usage:
Train a simple model using prepared data fromff_prep
.
library(ForestForesight)
# Assume we have already prepared data using ff_prep
prepared_data <- ff_prep(...)
model <- ff_train(
train_matrix = prepared_data$data_matrix,
nrounds = 100,
verbose = TRUE
)
print(model)
Using Validation Data:
Train a model with separate validation data for early stopping.
prepared_data <- ff_prep(..., validation_sample = 0.2)
model <- ff_train(
train_matrix = prepared_data$data_matrix,
validation_matrix = prepared_data$validation_matrix,
nrounds = 300,
early_stopping_rounds = 20,
verbose = TRUE
)
print(model$best_iteration)
print(model$best_score)
Customizing XGBoost Parameters:
Experiment with different XGBoost parameters to optimize the model.
model <- ff_train(
train_matrix = prepared_data$data_matrix,
eta = 0.05,
max_depth = 7,
subsample = 0.8,
min_child_weight = 2,
eval_metric = "auc",
verbose = TRUE
)
print(model$params)
Saving the Model:
Train a model and save it to a file for later use.
model <- ff_train(
train_matrix = prepared_data$data_matrix,
modelfilename = "my_deforestation_model.model",
verbose = TRUE
)
# Verify that the model file was created
file.exists("my_deforestation_model.model")
Continue Training from a Saved Model:
Load a previously saved model and continue training it with new data.
# Assume we have a previously saved model
existing_model <- xgboost::xgb.load("my_deforestation_model.model")
# Prepare new data
new_prepared_data <- ff_prep(...)
updated_model <- ff_train(
train_matrix = new_prepared_data$data_matrix,
xgb_model = existing_model,
nrounds = 50, # Additional rounds to train
verbose = TRUE
)
print(updated_model$niter)
These exercises will help you understand how to use the ff_train
function to train XGBoost models for deforestation prediction, experiment with different parameters, and manage model saving and updating. Remember to replace the ff_prep(...)
calls with actual data preparation steps using your ForestForesight data.