Francesco Dal Pont Francesco Dal Pont - 3 months ago 36
R Question

Predictions with h2o.randomForest saved model

I've trained a random forest model in Rstudio, using h2o library, and then i saved it as .rda file, now i would like to score other data using the model buildt.
So i loaded the model i've turned the new dataset in a H2OFrame and i try to get score using

predict( model, new_data)
function.
What i get is the following error message:

ERROR: Unexpected HTTP Status code: 404 Not Found (url = http://localhost:54321/4/Predictions/models/DRF_model_R_1468754145815_1/frames/file1840210c1889_sid_9b90_2)

water.exceptions.H2OKeyNotFoundArgumentException
[1] "water.exceptions.H2OKeyNotFoundArgumentException: Object 'DRF_model_R_1468754145815_1' not found in function: predict for argument: model"
[2] " water.api.ModelMetricsHandler.predict2(ModelMetricsHandler.java:239)"
[3] " sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
[4] " sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)"
[5] " sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)"
[6] " java.lang.reflect.Method.invoke(Method.java:498)"
[7] " water.api.Handler.handle(Handler.java:62)"
[8] " water.api.RequestServer.handle(RequestServer.java:655)"
[9] " water.api.RequestServer.serve(RequestServer.java:596)"
[10] " water.JettyHTTPD$H2oDefaultServlet.doGeneric(JettyHTTPD.java:745)"
[11] " water.JettyHTTPD$H2oDefaultServlet.doPost(JettyHTTPD.java:681)"
[12] " javax.servlet.http.HttpServlet.service(HttpServlet.java:755)"
[13] " javax.servlet.http.HttpServlet.service(HttpServlet.java:848)"
[14] " org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)"
[15] " org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)"
[16] " org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)"
[17] " org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)"
[18] " org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)"
[19] " org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)"
[20] " org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)"
[21] " org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)"
[22] " org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)"
[23] " org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)"
[24] " org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)"
[25] " org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)"
[26] " org.eclipse.jetty.server.Server.handle(Server.java:370)"
[27] " org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)"
[28] " org.eclipse.jetty.server.BlockingHttpConnection.handleRequest(BlockingHttpConnection.java:53)"
[29] " org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)"
[30] " org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)"
[31] " org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)"
[32] " org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)"
[33] " org.eclipse.jetty.server.BlockingHttpConnection.handle(BlockingHttpConnection.java:72)"
[34] " org.eclipse.jetty.server.bio.SocketConnector$ConnectorEndPoint.run(SocketConnector.java:264)"
[35] " org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)"
[36] " org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)"
[37] " java.lang.Thread.run(Thread.java:745)"

Error in .h2o.doSafeREST(h2oRestApiVersion = h2oRestApiVersion, urlSuffix = page, :


ERROR MESSAGE:

Object 'DRF_model_R_1468754145815_1' not found in function: predict for argument: model


I'm new to H2O.. Any idea on what is going wrong?

Answer

You cannot save it as an rda file, from inside R. The model exists on the H2O cluster, and you must use h2o.saveModel() to save it. And then h2o.loadModel() to load it again. See ?h2o.saveModel and ?h2o.loadModel.

BTW, note the asymmetry between the functions: you give a directory to saveModel, and you give a filename to loadModel. (The filename will be the model ID.)