Create RMSLE metric in caret in r

混江龙づ霸主 提交于 2020-01-01 19:58:09

问题


Could someone please help me with the following: I need to change my xgboost training model with caret package to an undefault metric RMSLE. By default caret and xgboost train and measure in RMSE.

Here are the lines of code:

create custom summary function in caret format

custom_summary = function(data, lev = NULL, model = NULL){
out = rmsle(data[, "obs"], data[, "pred"])
names(out) = c("rmsle")
out
}

create control object

control = trainControl(method = "cv",  
                   number = 2,     
                   summaryFunction = custom_summary)

create grid of tuning parameters

grid = expand.grid(nrounds = 100, 
               max_depth = 6,          
               eta = 0.075,     
               gamma = 0, 
               colsample_bytree = 0.4, 
               min_child_weight = 2.25,
               subsample = 1)

cl = makeCluster(3, type="SOCK") #make clusters

registerDoSNOW(cl)  #register clusters

set.seed(1)

train my model

caret4 =  train(price_doc~. - sub_area - id,
                    data=train.train,
                    method="xgbTree",
                    trControl=control, 
                    tuneGrid=grid, 
                    metric="rmsle",
                    maximize = FALSE)

and I keep getting an error: { :task 1 failed - "can not find function "rmsle""


回答1:


I also encountered the same issue in my project. This is even after loading the Metrics package in memory using the below command.
library(Metrics)

If you see, rmsle function is being called from another function called custom_summary. It is not called directly. So I loaded the Metrics package from within the function custom_summary and it solved the issue for me.

so here, the custom_summary function should look like:

custom_summary = function(data, lev = NULL, model = NULL) {
library(Metrics)
out = rmsle(data[, "obs"], data[, "pred"])
names(out) = c("rmsle")
out
}



来源:https://stackoverflow.com/questions/46827054/create-rmsle-metric-in-caret-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!