首先,我有数据(hex_glrm
),其中包含适合模型的4938列(要素)。在拟合之后,我下载并以MOJO格式将模型保存到文件夹tmp
。
script.R
...
model_glrm <- tryCatch({
h2o.glrm(hex_glrm,
cols = names(hex_glrm),
model_id = "model_glrm",
transform = "STANDARDIZE",
loss = "Huber",
regularization_x = "NonNegative",
regularization_y = "UnitOneSparse",
k = 100,
seed = 2017,
gamma_x = 0.2,
gamma_y = 0.2,
init = "Random",
svd_method = "GramSVD",
impute_original = TRUE,
max_runtime_secs = 1200)
}, error = function(err) {
flog.error(err)
NULL
})
if (!is.null(model_glrm)) {
flog.info(" - GLRM model is fitted!")
# Download generated MOJO
h2o.download_mojo(model_glrm, path = "/tmp", get_genmodel_jar = TRUE)
}
...
然后我创建EasyPredictModelWrapper
并加载model_glrm.zip
。之后,我获得了用于预测的新用户数据,并将其填入RowData
。然后我得到预测(drmp
)并创建响应的json。
PredictionServlet.java
...
// Get the MOJO model and the prediction
EasyPredictModelWrapper model = new EasyPredictModelWrapper(MojoModel.load("tmp/model_glrm.zip"));
RowData row = getRowDataFromJson(json); // New data for prediction (4938 features)
DimReductionModelPrediction drmp = model.predictDimReduction(row);
// Create the json response
JsonObject json = new JsonObject();
StringBuilder builder = new StringBuilder();
for (double i : drmp.dimensions) {
builder.append(i);
builder.append(", ");
}
json.addProperty("recommendations", builder.toString());
json.addProperty("size", drmp.dimensions.length);
// Emit the prediction to the servlet response
response.getWriter().write(json.toString());
response.setContentType("application/json; charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setStatus(HttpServletResponse.SC_OK);
...
response.json
{ &#34;推荐&#34;:&#34; 1.2323,2.3343,...&#34;, &#34;大小&#34;:100 }
但是当我得到预测时drmp.dimensions
只包含100个值。如何使用所有重建特征进行预测(4938)?