h2o mojo predict in R Shiny

六眼飞鱼酱① 提交于 2020-01-15 12:10:06

问题


I think I have exhausted the entire internet looking for an example / answer to my query regarding implementing a h2o mojo model to predict within RShiny. We have created a bunch of models, and wish to predict scores in a RShiny front end where users enter values. However, with the following code to implement the prediction we get an error of

Warning: Error in checkForRemoteErrors: 6 nodes produced errors; first error: No method asJSON S3 class: H2OFrame

dataInput <- dfName
dataInput <- toJSON(dataInput)

rawPred <- as.data.frame(h2o.predict_json(model= "folder/mojo_model.zip",  json = dataInput, genmodelpath = "folder/h2o-genmodel.jar"))

Can anyone help with some pointers? Thanks, Siobhan


回答1:


This is not a Shiny issue. The error indicates that you're trying to use toJSON() on an H2OFrame (instead of an R data.frame), which will not work because the jsonlite library does not support that.

Instead you can convert the H2OFrame to a data.frame using:

dataInput <- toJSON(as.data.frame(dataInput))

I can't guarantee that toJSON() will generate the correct input for h2o.predict_json() since I have not tried that, so you will have to try it out yourself. Note that the only way this may work is if this is a 1-row data.frame because the h2o.predict_json() function expects a single row of data, encoded as JSON. If you're trying to score multiple records, you'd have to loop over the rows. If for some reason toJSON() doesn't give you the right format, then you can use a function I wrote in this post here to create the JSON string from a data.frame manually.

There is a ticket open to create a better version of h2o.predict_json() that will support making predictions from a MOJO on data frames (with multiple rows) without having to convert to JSON first. This will make it so you can avoid dealing with JSON altogether.

An alternative is to use a H2O binary model instead of a MOJO, along with the standard predict() function. The only requirement here is that the model must be loaded into H2O cluster memory.




回答2:


The following works now using the json formatting from first two lines and the single quote around var with spaces.

df<- data.frameV1=1,V2=1,CMPNY_EL_IND=1,UW_REGION_NAME = "'LONDON & SE'" )
    dfstr <- sapply(1:ncol(df), function(i) paste(paste0('\"', names(df)[i], '\"'), df[1,i], sep = ':'))
    json <- paste0('{', paste0(dfstr, collapse = ','), '}')
    dataPredict <- as.data.frame(h2o.predict_json(model = "D:\\GBM_model_0_CMP.zip", json = json, genmodelpath = "D:\\h2o-genmodel.jar", labels = TRUE))


来源:https://stackoverflow.com/questions/49411393/h2o-mojo-predict-in-r-shiny

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