我在R中使用XGBoost遇到麻烦。
我正在读取包含数据的CSV文件:
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))
}
这个函数叫做我的main.R
lista_dados = get_data()
#machine = train_svm(lista_dados$train)
#machine = train_rf(lista_dados$train)
machine = train_xgt(lista_dados$train)
问题在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)
}
这是错误:
becchi @ ubuntu:〜/ Documents / EEG_DATA / Dados_Eye $ Rscript main.R
[1]是
[1]是
xgb.DMatrix(data_train,label = label_train)中的错误:
xgb.DMatrix:不支持从列表构造调用:train_xgt
-> xgb.DMatrix执行停止becchi @ ubuntu:〜/ Documents / EEG_DATA / Dados_Eye $
如您所见,它们都是DataFrame。
我不知道我在做什么错,请帮忙!
最佳答案
只需先使用as.matrix()
将数据帧转换为矩阵,然后再传递给xgb.Dmatrix()
。