Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - XGBoost: Error building DMatrix

I am having trouble using the XGBoost in R. I am reading a CSV file with my data:

get_data = function()
{
#Loading Data
path = "dados_eye.csv"
data = read.csv(path)

#Dividing into two groups
train_porcentage = 0.05
train_lines = nrow(data)*train_porcentage
train = data[1:train_lines,]
test = data[train_lines:nrow(data),]
rownames(train) = c(1:nrow(train))
rownames(test) = c(1:nrow(test))

return (list("test" = test, "train" = train))
}

This function is Called my the main.R

lista_dados = get_data()
#machine = train_svm(lista_dados$train)
#machine = train_rf(lista_dados$train)
machine = train_xgt(lista_dados$train)

The problem is here in the train_xgt

train_xgt = function(train_data)
{
data_train = data.frame(train_data[,1:14])
label_train = data.frame(factor(train_data[,15]))

print(is.data.frame(data_train))
print(is.data.frame(label_train))

dtrain = xgb.DMatrix(data_train, label=label_train)
machine = xgboost(dtrain, num_class = 4 ,max.depth = 2, 
    eta = 1, nround = 2,nthread = 2, 
    objective = "binary:logistic")

return (machine)    
}

This is the Error:

becchi@ubuntu:~/Documents/EEG_DATA/Dados_Eye$ Rscript main.R

[1] TRUE

[1] TRUE

Error in xgb.DMatrix(data_train, label = label_train) :
xgb.DMatrix: does not support to construct from list Calls: train_xgt -> xgb.DMatrix Execution halted becchi@ubuntu:~/Documents/EEG_DATA/Dados_Eye$

As you can see, they are both DataFrames.

I dont know what I am doing wrong, please help!

like image 472
Gabriel Chaves Becchi Avatar asked Mar 12 '17 03:03

Gabriel Chaves Becchi


People also ask

Is DMatrix necessary for XGBoost?

To train on the dataset using a DMatrix, we need to use the XGBoost train() method. The train() method takes two required arguments, the parameters, and the DMatrix. Following is the code for training using DMatrix.

What is DMatrix in XGBoost?

DMatrix is an internal data structure that is used by XGBoost, which is optimized for both memory efficiency and training speed. You can construct DMatrix from multiple different sources of data. Parameters. data (os. PathLike/string/numpy.


1 Answers

Just convert data frame to matrix first using as.matrix() and then pass to xgb.Dmatrix().

like image 197
ad28 Avatar answered Oct 05 '22 17:10

ad28