1
$\begingroup$

I write a code in Rstudio with xgboost to solve a Machine Learning problem. This is my actual code:

library(xgboost)
library(tidyverse)
library(caret)
library(readxl)

library(data.table)
library(mlr)

data <- iris
righe_train <- sample(nrow(data),nrow(data)*0.8)
train <- data[righe_train,]
test <- data[-righe_train,]

setDT(train) 
setDT(test)

labels <- train$Species
ts_label <- test$Species
new_tr <- model.matrix(~.+0,data = train[,-c("Species"),with=F]) 
new_ts <- model.matrix(~.+0,data = test[,-c("Species"),with=F])

#convert factor to numeric 
labels <- as.numeric(labels)-1
ts_label <- as.numeric(ts_label)-1


#preparing matrix 
dtrain <- xgb.DMatrix(data = new_tr,label = labels) 
dtest <- xgb.DMatrix(data = new_ts,label=ts_label)

#default parameters
params <- list(booster = "gbtree",
                 objective = "multi:softmax",
                 num_class = 3,
                 eta=0.3,
                 gamma=0,
                 max_depth=6,
                 min_child_weight=1,
                 subsample=1,
                 colsample_bytree=1)

xgbcv <- xgb.cv( params = params,
                 data = dtrain,
                 nrounds = 100,
                 nfold = 5,
                 showsd = T,
                 stratified = T,
                 print_every_n = 10,
                 early_stopping_round = 20,
                 maximize = F)
##best iteration = 79

min(xgbcv$test.error.mean)


#first default - model training
xgb1 <- xgb.train (params = params,
                   data = dtrain, 
                   nrounds = 21,
                   watchlist = list(val=dtest,train=dtrain),
                   print.every.n = 10,
                   early.stop.round = 10,
                   maximize = F ,
                   merror = "error")
                  # eval_metric = "error")
#model prediction
xgbpred <- predict (xgb1,dtest)
xgbpred <- ifelse (xgbpred > 0.5,1,0)

#confusion matrix
library(caret)

factors_both <- as.factor(c(xgbpred, ts_label))
xgbpred_f <- factors_both[1:length(xgbpred)]
ts_label_f <- factors_both[length(xgbpred)+1:length(xgbpred)*2]

confusionMatrix (xgbpred_f,ts_label_f)
#Accuracy - 86.54%` 

#view variable importance plot
mat <- xgb.importance (feature_names = colnames(new_tr),model = xgb1)
xgb.plot.importance (importance_matrix = mat[1:20])

So, this is a Machine Learning supervised models. How can I classify a new registration? I have this new registration:

new_record <- c(5.3,3.2,2.0,0.2)

How can I classify it using the previous model?

$\endgroup$

1 Answer 1

1
$\begingroup$

With new data, you need to go down exactly the same route as with the data used for training. Something like:

# Some data
newdf = data.frame(Sepal.Length=c(5.3), Sepal.Width=c(3.2), Petal.Length=c(2.0), Petal.Width=c(0.2))

# Model matrix
newdf = model.matrix(~.+0,data = newdf) 

# Predict on xgb.DMatrix object
predict(xgb1,xgb.DMatrix(data = newdf))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.