Error in Xgboost Regression

Asked 2 years ago, Updated 2 years ago, 39 views

The following code will result in an error.

error message

 Error in xgb.iter.update (bst$handle, dtrain, interaction-1, obj): 
  [23:11:58] amalgamation/../src/objective/regression_obj.cu:61: Check failed:preds.Size()==info.labels_.Size()(75 vs.25): labels are not correctly provided.size=75, label.size=25, Loss:reg:squarederror

Current state code

Reading #database
database<-read.csv("database.csv")

database

# Convert database Atmosphere to Qualitative Variables
database$Atmosphere<-as.factor(database$Atmosphere)

database

# Convert csv file to data.table
dt<-data.table (database, keep.rounames=F)

dt

# Extraction of learning data
train.data=dt [1:25,]

# Validation data extraction
test.data=dt [26:39]

# Extract learning data description variable (other than Hydrogen.content) as model.mx_ev
model.mx_ev<-sparse.model.matrix (Hydrogen.content~.,train.data)

model.mx_ev

# Extract learning data object variables as model.mx_ov
model.mx_ov<-xgb.DMatrix (model.mx_ev, label=train.data$Hydrogen.content)

model.mx_ov

# Extract validation data description variable (other than Hydrogen.content) as pred.mx_ev
pred.mx_ev<-sparse.model.matrix (Hydrogen.content~., test.data)

pred.mx_ev

# Extract validation data object variable as model.mx_ov
pred.mx_ov<-xgb.DMatrix (pred.mx_ev, label=test.data$Hydrogen.content)

pred.mx_ov

# Parameter adjustment (default)#
params<-list(
  eta = 0.3,
  gamma = 0,
  max_depth = 6,
  min_child_weight = 1,
  max_delta_step = 0,
  subsample=1,
  colsample_bytree=1,
  colsample_bylevel=1,
  lambda = 1,
  alpha = 0
)

#Xgboost Learning #
xgb<-xgboost(data=model.mx_ev, label=train.data$Hydrogen.content, num_class=3, objective="reg:linear", booter="gbtree", rounds=100, verbose=1)

r

2022-09-30 19:38

1 Answers

Clear the argument num_class=3. This argument specifies the number of classes in the classification and is not used for regression.

Example:

data(iris)
iris<-rename (iris, Hydrogen.content=Sepal.Length)

require(xgboost)
require(data.table)
require (Matrix)

dt<-data.table (iris, keep.rounames=F)
dt<-iris

# Extraction of learning data
train.data<-dt [1:25,]

# Validation data extraction
test.data<-dt [26:39,]

# Extract learning data description variable (other than Hydrogen.content) as model.mx_ev
model.mx_ev<-sparse.model.matrix (Hydrogen.content~.,train.data)

xgb<-xgboost(data=model.mx_ev, label=train.data$Hydrogen.content, objective="reg:linear", nounds=100)

Also, if what you really want to do is not regression but classification, you will have to change objective=.


2022-09-30 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.