R - XGBoost: Error building DMatrix

Gabriel Chaves Becchi picture Gabriel Chaves Becchi · Mar 12, 2017 · Viewed 18.3k times · Source

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!

Answer

ad28 picture ad28 · Jun 29, 2017

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