【问题标题】:(H2O.ai) How to handle empty timestamp values in hex.genmodel.easy.EasyPredictModelWrapper.predictBinomial(H2O.ai) 如何处理 hex.genmodel.easy.EasyPredictModelWrapper.predictBinomial 中的空时间戳值
【发布时间】:2017-08-12 03:37:05
【问题描述】:

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

尝试在 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 值,方法是在 h2o 的模型 EasyPredictionModelWrapper 可以对其进行预测的 RowData 对象中将它们设置为 null。

问题在于,对于此列,模型需要一个 Double 值。但是没有可以传入的 Double 值,因为该值为 null。请注意,由于模型的训练方式,我不能只将这些空值设置为 0.0(因为并非所有日期都为空,因此将某些日期设置为零会歪曲模型的特定样本)。那么我该如何解决这个问题,或者我可以在预期 Double 的地方放置什么来代替 null?

谢谢你的建议:)

【问题讨论】:

    标签: java h2o


    【解决方案1】:

    在我将row.put("date_field", "<date string>") 一些<date string> 转换为RowData 对象(参见here)之前,我在Strings 之前所做的事情是EasyPredictModelWrapper 可以预测的:

    /**
         *
         * @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 配置(请参阅this 代码顶部附近),以便很好地处理"NULL" 字符串。例如:

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

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 2019-09-24
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 2021-08-30
      相关资源
      最近更新 更多