2

I am using latest release of LightGBM to solve a multi classification problem. When I switch the objective to "multiclass", this error occurs;

Error in data$update_params(params) : 
  [LightGBM] [Fatal] Number of classes should be specified and greater than 1 for multiclass training

I leave a reproducible example that indicates my way

catnames <- names(purrr::keep(train_x,is.factor))

dtrain <- lgb.Dataset(as.matrix(train_x), label = train_y,categorical_feature = catnames)
data_file <- tempfile(fileext = ".data")
lgb.Dataset.save(dtrain, data_file)
dtrain <- lgb.Dataset(data_file)
lgb.Dataset.construct(dtrain)

model <- lgb.train(data=dtrain,
                   objective = "multiclass",
                   alpha = 0.1,
                   nrounds = 1000,
                   learning_rate = .1
                   )

Tried to save my target (train_y) as factor, nothing changed.

sametsokel
  • 334
  • 3
  • 11

1 Answers1

2

When using the multi-class objective in LightGBM, you need to pass another parameter that tells the learner the number of classes to predict.

So, it should probably look more like this:

model <- lgb.train(data=dtrain,
                   objective = "multiclass",
                   num_classes = INSERT NUMBER OF TARGET CLASSES HERE,
                   alpha = 0.1,
                   nrounds = 1000,
                   learning_rate = .1,
                   )

My experience is more with the python API so it might be that (if this does not work) you need to pass the num_class parameter in the form of a list for a params keyword argument in lgb.train.

Campbell Hutcheson
  • 549
  • 2
  • 4
  • 12