mlr-org / mlr3tuning

Hyperparameter optimization package of the mlr3 ecosystem
https://mlr3tuning.mlr-org.com/
GNU Lesser General Public License v3.0
55 stars 5 forks source link

When performing hyperparameter tuning, tuner() reports an error #428

Closed zrq1209 closed 3 months ago

zrq1209 commented 4 months ago

data = read_excel("F:/R data/test1.xlsx") data = as.data.table(data) class(data$biomass)#检查读取数据后的类型

target = "biomass" features = setdiff(names(data), target)

task=TaskRegr$new(data, target = "biomass",id = "prediction") task

filter = flt("correlation") filter$calculate(task) top_features = as.data.table(filter)$feature[1:10]

scores_dt = data.table(feature = names(filter$scores), score = filter$scores)

task_selected = task$select(top_features) new_data = task_selected$data() task_selected = TaskRegr$new(new_data, target = "biomass", id = "prediction")

set.seed(123) data_split = partition(task_selected, ratio = 0.8) train_indices = data_split$train test_indices = data_split$test

train_data = task_selected$data()[train_indices] train_task = TaskRegr$new( as.data.table(train_data), target = "biomass", id = "prediction_train" )

test_data <- task_selected$data()[test_indices] test_task = TaskRegr$new( as.data.table(test_data), target = "biomass", id = "prediction_test" )

learner_ranger = lrn("regr.ranger", id = "ranger")

learner_ranger$train(train_task) learner_ranger$model

resampling = rsmp("cv", folds=10) rr = resample(train_task, learner = learner_ranger, resampling = resampling) print(rr$aggregate())

predictions = learner_ranger$predict(test_task) measures = list(msr("regr.rmse"), msr("regr.mae"), msr("regr.rsq")) performance = predictions$score(measures) print(performance)

learner = lrn("regr.ranger", mtry = to_tune(lower = 1, upper = 9, logscale = FALSE), num.trees = to_tune(lower = 50, upper = 1500, logscale = FALSE), min.node.size = to_tune(lower = 1, upper = 20, logscale = FALSE) )

instance = ti( task = train_task, learner = learner, resampling = rsmp("cv", folds = 5), measures = msr("regr.mae"), terminator = trm("none") ) instance tuner = tnr("random_search", resolution = 5)

Error in assert_binary(truth, response = response, positive = positive, : Assertion on 'truth' failed: Must be of type 'factor', not 'character'.

The code is as above, and the error is reported when running to the tuner function. I am creating a regression task, it is reasonable that the data are numeric type, the whole process does not need factor type data, but the error says that it needs factor type data.

be-marc commented 3 months ago

Please write a minimal reproducible example. I cannot check without the data.