r - 使用保存的 h2o rulefit 模型进行预测会引发 NullPointerException 错误
问题描述
按照文档中的 h2o rulefit 模型示例(https://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/rulefit.html),效果很好,下面是示例:
library(h2o)
h2o.init()
# Import the titanic dataset:
f <- "https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv"
coltypes <- list(by.col.name = c("pclass", "survived"), types=c("Enum", "Enum"))
df <- h2o.importFile(f, col.types = coltypes)
# Split the dataset into train and test
splits <- h2o.splitFrame(data = df, ratios = 0.8, seed = 1)
train <- splits[[1]]
test <- splits[[2]]
# Set the predictors and response; set the factors:
response <- "survived"
predictors <- c("age", "sibsp", "parch", "fare", "sex", "pclass")
# Build and train the model:
rfit <- h2o.rulefit(y = response,
x = predictors,
training_frame = train,
max_rule_length = 10,
max_num_rules = 100,
seed = 1)
# Retrieve the rule importance:
print(rfit@model$rule_importance)
# Predict on the test data:
h2o.predict(rfit, newdata = test)
但是当我保存模型时,关闭 h2o 连接并开始新的连接,加载模型并尝试预测(我在 linux 上使用最新的 h2o):
h2o.saveModel(rfit, path = '/my/path/')
h2o.shutdown()
h2o.init()
H2O is not running yet, starting it now...
Note: In case of errors look at the following log files:
/tmp/RtmpWvc6F9/file238276aca9de/h2o_Martin_Hulin_started_from_r.out
/tmp/RtmpWvc6F9/file2382281d69c3/h2o_Martin_Hulin_started_from_r.err
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
Starting H2O JVM and connecting: ..... Connection successful!
R is connected to the H2O cluster:
H2O cluster uptime: 4 seconds 333 milliseconds
H2O cluster timezone: America/New_York
H2O data parsing timezone: UTC
H2O cluster version: 3.32.0.1
H2O cluster version age: 2 months and 9 days
H2O cluster name: H2O_started_from_R_Martin_Hulin_lag006
H2O cluster total nodes: 1
H2O cluster total memory: 26.64 GB
H2O cluster total cores: 36
H2O cluster allowed cores: 36
H2O cluster healthy: TRUE
H2O Connection ip: localhost
H2O Connection port: 54321
H2O Connection proxy: NA
H2O Internal Security: FALSE
H2O API Extensions: Amazon S3, XGBoost, Algos, AutoML, Core V3, TargetEncoder, Core V4
R Version: R version 3.5.1 (2018-07-02)
loaded_model <- h2o.loadModel(path = '/ma/path/rfit')
h2o.predict(loaded_model, newdata = test)
它抛出 NullPointerException 错误。
java.lang.NullPointerException
at hex.rulefit.RuleFitModel.updateModelMetrics(RuleFitModel.java:144)
at hex.rulefit.RuleFitModel.score(RuleFitModel.java:124)
at water.api.ModelMetricsHandler$1.compute2(ModelMetricsHandler.java:396)
at water.H2O$H2OCountedCompleter.compute(H2O.java:1577)
at jsr166y.CountedCompleter.exec(CountedCompleter.java:468)
at jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:263)
at jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:974)
at jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1477)
at jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)
Error: java.lang.NullPointerException
我尝试了不同的数据和其他保存方式,但遇到了同样的问题。
知道应该如何保存/加载规则拟合模型,还是它是一个错误?
谢谢!
解决方案
推荐阅读
- c# - Npgsql.NpgsqlException: '没有提供密码
- java - 一段时间后从后台恢复时,Android应用布局组件为空
- java - MySQLNonTransientConnectionException:连接关闭后不允许任何操作。
- python - Python中的XOR RGB图像解密
- html - 将鼠标悬停在父 div 上时 CSS 淡化子元素,然后将鼠标悬停在子元素上时再次更改
- vue.js - 从另一个模块访问 vuex 模块状态
- python - 如何将我的 Google Blogger 文件导入 Pelican 而不会出错?
- hyperledger - 超级账本节点的更好做法是什么……?
- ios - iOS 以编程方式获取 Eve Motion 传感器状态
- ios - 从 pod 安装框架后,无法在 Swift 项目的类中导入,抛出错误 no such module swift import