I am using xgboost to predict airbnb destinations (similar to the Kaggle competition but for a class project). However when running the prediction command I receive this error message:
Error in predict.xgb.Booster(bst, dval) :
Feature names stored in object
and newdata
are different!
How can I fix this problem?
Here is my code:
setwd("~/Documents/Big Data/Datasets-20180304")
airbnb <- read.csv("airbnb_train.csv", header = T, stringsAsFactors = F)
airbnb_test <- read.csv("airbnb_test.csv", header = T, stringsAsFactors = F)
airbnb <- na.omit(airbnb)
airbnb_test <- na.omit(airbnb_test)
airbnb$country_destination <- as.factor(airbnb$country_destination)
airbnb$country_destination[airbnb$country_destination==0] <- NA
airbnb$country_destination <- recode(airbnb$country_destination, "c('1') = '0'; c('2') = '1'")
airbnb <- na.omit(airbnb)
airbnb_test <- na.omit(airbnb_test)
set.seed(1234)
train_index <- sample(1:nrow(airbnb),size = 0.7*nrow(airbnb),replace = F)
train <- airbnb[train_index,]
validation <- airbnb[-train_index,]
options(na.action='na.pass')
new_tr = sparse.model.matrix(country_destination~.-1,data = train, with = F)
train_label <- train$country_destination
train_label <- as.numeric(train_label)-1
dtrain <- xgb.DMatrix(data = new_tr, label=train_label)
new_val = sparse.model.matrix(country_destination~.-1,data = validation, with = F)
val_label <- validation$country_destination
val_label <- as.numeric(val_label)-1
dval <- xgb.DMatrix(data = new_val, label=val_label)
#default parameters
params <- list(
booster = "gbtree",
objective = "binary:logistic",
eta=0.3,
gamma=0,
max_depth=6,
min_child_weight=1,
subsample=1,
colsample_bytree=1
)
bst <- xgboost(data = dtrain, label = train_label, max_depth = 2, eta = 1, nthread = 2, nrounds = 8, objective = "binary:logistic")
xgbpred <- predict(bst,dval)
What am I doing wrong? How can I ensure that both 'bst' and 'dval' have the same feature_names?