首页 文章

(H2O.ai)如何处理hex.genmodel.easy.EasyPredictModelWrapper.predictBinomial中的空时间戳值

提问于
浏览
0

使用h2o,我使用了.csv数据框,其中包含一列日期,其中一些是NULL,用于训练模型 . 查看在解析输入.csv文件后由h2o Flow UI输出的.hex数据帧,空值由 . 表示,其余日期表示为时间戳加倍(即,自纪元时间以来的毫秒数) .

当尝试在java程序中使用模型的MOJO文件进行预测时,在数据集上,我收到错误

Exception in thread "main" java.lang.NullPointerException
        at hex.genmodel.easy.EasyPredictModelWrapper.fillRawData(EasyPredictModelWrapper.java:554)
        at hex.genmodel.easy.EasyPredictModelWrapper.predict(EasyPredictModelWrapper.java:615)
        at hex.genmodel.easy.EasyPredictModelWrapper.preamble(EasyPredictModelWrapper.java:489)
        at hex.genmodel.easy.EasyPredictModelWrapper.predictBinomial(EasyPredictModelWrapper.java:303)
        at SimpleCsvPredictor.predictCsv(SimpleCsvPredictor.java:287)
        at SimpleCsvPredictor.main(SimpleCsvPredictor.java:210)

因为我在数据集的日期列中处理NULL值,方法是在RowData对象中设置t null,h2o的模型EasyPredictionModelWrapper可以对其进行预测 .

问题是,对于此列,模型期望Double值 . 但是没有要传入的Double值,因为该值为null . 请注意,由于模型的训练方式,我不能将这些空值设置为0.0(因为并非所有日期都为空,因此将某些日期设置为零将会错误地表示模型的特定样本) . 那么我该如何解决这个问题呢?或者我可以把它放在一个预期Double的空位置?

感谢您的建议 :)

1 回答

  • 0

    以下是我在 String 之前的日期 StringRowData 对象(见hereEasyPredictModelWrapper 可预测的日期:

    /**
         *
         * @param str_date (in MM/dd/yyyy form)
         * @return string representation of timestamp value, either the string value of the str_date timestamp or "NULL"
         * if can parse str_date to Date object, else returns null
         */
        private String dateString2TimestampString(String str_date) {
            if (str_date.toUpperCase().equals("NULL")) {
                return "NULL";
            } else {
                try {
                    // convert date (MM/DD/YYYY) string to java date
                    DateFormat formatter;
                    formatter = new SimpleDateFormat("MM/dd/yyyy");
                    Date date = (Date) formatter.parse(str_date);
    
                    // convert date string to timestamp (since epoch time) (double??)
                    double epochTimestamp = (double) date.getTime();
                    return new BigDecimal(epochTimestamp).toPlainString();
                } catch (Exception e) {
                    System.out.println("** dateString2TimestampString: could not parse string \"" + str_date + "\" to Date object");
                    System.out.println(e.getClass().getCanonicalName());
                    System.err.println(e.getMessage());
                    System.exit(1);
                    return null;
                }
            }
        }
    

    请务必为包装器设置 convertInvalidNumberToNa config(请参阅this代码顶部附近),以便很好地处理 "NULL" 字符串 . 例如 . :

    EasyPredictModelWrapper model = new EasyPredictModelWrapper(
                new EasyPredictModelWrapper.Config()
                    .setModel(MojoModel.load(MODEL_CLASS_NAME))
                    .setConvertUnknownCategoricalLevelsToNa(true)
                    .setConvertInvalidNumbersToNa(true)
            );
    

相关问题