Last active
June 7, 2022 05:15
-
-
Save randyphoa/abd568df5d602dc6b239df683b67b161 to your computer and use it in GitHub Desktop.
Deploy R in online mode - train iris model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install specific version of xgboost to ensure the development and deployment environment have the same version | |
install.packages("https://cloud.r-project.org/src/contrib/Archive/xgboost/xgboost_1.5.2.1.tar.gz", repos=NULL, type="source") | |
library(xgboost) | |
data(iris) | |
species <- iris$Species | |
label <- as.integer(iris$Species) - 1 | |
iris$Species <- NULL | |
n <- nrow(iris) | |
train.index <- sample(n, floor(0.75 * n)) | |
train.data <- as.matrix(iris[train.index, ]) | |
train.label <- label[train.index] | |
test.data <- as.matrix(iris[-train.index, ]) | |
test.label <- label[-train.index] | |
xgb.train <- xgb.DMatrix(data = train.data, label = train.label) | |
xgb.test <- xgb.DMatrix(data = test.data, label = test.label) | |
num_class <- length(levels(species)) | |
params <- list( | |
booster = "gbtree", | |
eta = 0.001, | |
max_depth = 5, | |
gamma = 3, | |
subsample = 0.75, | |
colsample_bytree = 1, | |
objective = "multi:softprob", | |
eval_metric = "mlogloss", | |
num_class = num_class | |
) | |
model <- xgb.train( | |
params = params, | |
data = xgb.train, | |
nrounds = 50, | |
early_stopping_rounds = 3, | |
watchlist = list(val1 = xgb.train, val2 = xgb.test), | |
verbose = 0 | |
) | |
xgb.save(model, "iris_xgb.model") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment