R:预测错误.xgboost:存储在' object '和' newdata '中的特性名称是不同的



我使用xgboost编写了一个脚本,使用来自田间和卫星图像的数据来预测某一地区的土壤类别。脚本如下:'

rm(list=ls())
library(xgboost)
library(caret) 
library(raster)
library(sp)
library(rgeos)
library(ggplot2)

setwd("G:/DATA")
data <- read.csv('96PointsClay02finalone.csv')   
head(data)           
summary(data)       
dim(data)  
ras <- stack("Allindices04TIFF.tif")
names(ras) <- c("b1", "b2", "b3", "b4", "b5", "b6", "b7", "b10", "b11","DEM",
"R1011", "SCI", "SAVI", "NDVI", "NDSI", "NDSandI", "MBSI", 
"GSI", "GSAVI", "EVI", "DryBSI", "BIL", "BI","SRCI")
set.seed(27) # set seed for generating random data.
# createDataPartition() function from the caret package to split the original dataset into a training and testing set and split data into training (80%) and testing set (20%)
parts = createDataPartition(data$Clay, p = .8, list = F)
train = data[parts, ]
test = data[-parts, ]
#define predictor and response variables in training set
train_x = data.matrix(train[, -1])
train_y = train[,1]
#define predictor and response variables in testing set
test_x = data.matrix(test[, -1])
test_y = test[, 1]
#define final training and testing sets
xgb_train = xgb.DMatrix(data = train_x, label = train_y)
xgb_test = xgb.DMatrix(data = test_x, label = test_y)
#defining a watchlist
watchlist = list(train=xgb_train, test=xgb_test)
#fit XGBoost model and display training and testing data at each iteartion
model = xgb.train(data = xgb_train, max.depth = 3, watchlist=watchlist, nrounds = 100)
#define final model
model_xgboost = xgboost(data = xgb_train, max.depth = 3, nrounds = 86, verbose = 0)
summary(model_xgboost)
#use model to make predictions on test data
pred_y = predict(model_xgboost, xgb_test)
# performance metrics on the test data
mean((test_y - pred_y)^2) #mse - Mean Squared Error
caret::RMSE(test_y, pred_y) #rmse - Root Mean Squared Error
y_test_mean = mean(test_y)
rmseE<- function(error)
{
sqrt(mean(error^2))
}
y = test_y
yhat = pred_y
rmseresult=rmseE(y-yhat)
(r2 = R2(yhat , y, form = "traditional"))
cat('The R-square of the test data is ', round(r2,4), ' and the RMSE is ', round(rmseresult,4),  'n')
#use model to make predictions on satellite image
result <- predict(model_xgboost, ras[1:(nrow(ras)*ncol(ras))])
#create a result raster
res <- raster(ras)
#fill in results and add a "1" to them (to get back to initial class numbering! - see above "Prepare data" for more information)
res <- setValues(res,result+1)
#Save the output .tif file into saved directory
writeRaster(res, "xgbmodel_output", format = "GTiff", overwrite=T)

脚本工作良好,直到它达到result <- predict(model_xgboost, ras[1:(nrow(ras)*ncol(ras))])这需要一些时间,然后给出这个错误:

Error in predict.xgb.Booster(model_xgboost, ras[1:(nrow(ras) * ncol(ras))]) : 
Feature names stored in `object` and `newdata` are different!
我意识到我在那一行做错了什么。但是,我不知道如何将xgboost模型应用于代表我研究区域的光栅图像。如果有人伸出援助之手,启发我,帮助我解决这个问题....

我的数据为csv和栅格图像可以在这里找到.

<代码>

最后,我得到了这个错误的原因。这是我的错误,因为训练数据中的列数与卫星图像中的层数不相同。

最新更新