【问题标题】:Unable to map predictions to JavaRDD无法将预测映射到 JavaRDD
【发布时间】:2015-03-23 23:42:42
【问题描述】:

我正在尝试将预测映射到 LinearRegression 模型,以便将它们传递到 BinaryClassificationMetrics 对象:

// Make predictions on test documents. cvModel uses the best model found (lrModel).
DataFrame predictions = cvModel.transform(testingFrame);
JavaRDD<Tuple2<Object, Object>> scoreAndLabels = predictions.map(
        new Function<Row, Tuple2<Object, Object>>() {
            @Override
            public Tuple2<Object, Object> call(Row r) {
                Double score = r.getDouble(1);
                return new Tuple2<Object, Object>(score, r.getDouble(0));
            }
        }
);
BinaryClassificationMetrics metrics
        = new BinaryClassificationMetrics(JavaRDD.toRDD(scoreAndLabels));

但是,当我调用predictions.map(...) 时,我收到以下编译错误:

method map in class DataFrame cannot be applied to given types;
  required: Function1<Row,R>,ClassTag<R>
  found: <anonymous Function<Row,Tuple2<Object,Object>>>
  reason: cannot infer type-variable(s) R
    (actual and formal argument lists differ in length)
  where R is a type-variable:
    R extends Object declared in method <R>map(Function1<Row,R>,ClassTag<R>)

关于如何映射预测数据帧的数据有什么建议吗?

【问题讨论】:

    标签: java apache-spark rdd apache-spark-mllib


    【解决方案1】:

    想通了!我必须将 DataFrame 转换为 JavaRDD,然后从那里直接开始:

    DataFrame predictions = cvModel.transform(testingFrame);
    JavaRDD<Tuple2<Object, Object>> scoreAndLabels = predictions.toJavaRDD().map(
            new Function<Row, Tuple2<Object, Object>>() {
                @Override
                public Tuple2<Object, Object> call(Row r) {
                    Double score = r.getDouble(4);
                    Double label = r.getDouble(1);
                    return new Tuple2<Object, Object>(score, label);
                }
            });
    
    BinaryClassificationMetrics metrics
            = new BinaryClassificationMetrics(JavaRDD.toRDD(scoreAndLabels));
    

    【讨论】:

    • 你应该接受这个。没有理由再保持这个开放了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2022-08-11
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多