【问题标题】:Apache Spark Decision Tree PredictionsApache Spark 决策树预测
【发布时间】:2017-04-11 01:25:02
【问题描述】:

我有以下代码用于使用决策树进行分类。我需要将测试数据集的预测放入 java 数组并打印出来。有人可以帮我扩展此代码。我需要一个预测标签和实际标签的二维数组并打印预测标签。

public class DecisionTreeClass {
    public  static void main(String args[]){
        SparkConf sparkConf = new SparkConf().setAppName("DecisionTreeClass").setMaster("local[2]");
        JavaSparkContext jsc = new JavaSparkContext(sparkConf);


        // Load and parse the data file.
        String datapath = "/home/thamali/Desktop/tlib.txt";
        JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(jsc.sc(), datapath).toJavaRDD();//A training example used in supervised learning is called a “labeled point” in MLlib.
        // Split the data into training and test sets (30% held out for testing)
        JavaRDD<LabeledPoint>[] splits = data.randomSplit(new double[]{0.7, 0.3});
        JavaRDD<LabeledPoint> trainingData = splits[0];
        JavaRDD<LabeledPoint> testData = splits[1];

        // Set parameters.
        //  Empty categoricalFeaturesInfo indicates all features are continuous.
        Integer numClasses = 12;
        Map<Integer, Integer> categoricalFeaturesInfo = new HashMap();
        String impurity = "gini";
        Integer maxDepth = 5;
        Integer maxBins = 32;

        // Train a DecisionTree model for classification.
        final DecisionTreeModel model = DecisionTree.trainClassifier(trainingData, numClasses,
                categoricalFeaturesInfo, impurity, maxDepth, maxBins);

        // Evaluate model on test instances and compute test error
        JavaPairRDD<Double, Double> predictionAndLabel =
                testData.mapToPair(new PairFunction<LabeledPoint, Double, Double>() {
                    @Override
                    public Tuple2<Double, Double> call(LabeledPoint p) {
                        return new Tuple2(model.predict(p.features()), p.label());
                    }
                });

        Double testErr =
                1.0 * predictionAndLabel.filter(new Function<Tuple2<Double, Double>, Boolean>() {
                    @Override
                    public Boolean call(Tuple2<Double, Double> pl) {
                        return !pl._1().equals(pl._2());
                    }
                }).count() / testData.count();

        System.out.println("Test Error: " + testErr);
        System.out.println("Learned classification tree model:\n" + model.toDebugString());


    }

}

【问题讨论】:

    标签: java apache-spark machine-learning decision-tree


    【解决方案1】:

    您基本上拥有预测和标签变量。如果你真的需要一个二维双数组的列表,你可以改变你使用的方法:

    JavaRDD<double[]> valuesAndPreds = testData.map(point -> new double[]{model.predict(point.features()), point.label()});
    

    并在该引用上运行collect 以获取二维双精度数组列表。

    List<double[]> values = valuesAndPreds.collect();
    

    我会在这里查看文档:https://spark.apache.org/docs/latest/mllib-evaluation-metrics.html。您还可以使用 MulticlassMetrics 等类更改数据以获取模型的其他静态性能测量。这需要将 mapToPair 函数更改为 map 函数并将泛型更改为对象。所以像:

    JavaRDD<Tuple2<Object, Object>> valuesAndPreds = testData().map(point -> new Tuple2<>(model.predict(point.features()), point.label()));
    

    然后运行:

    MulticlassMetrics multiclassMetrics = new MulticlassMetrics(JavaRDD.toRDD(valuesAndPreds));
    

    所有这些东西都在 Spark 的 MLLib 文档中有很好的记录。此外,您提到需要打印结果。如果这是家庭作业,我会让你弄清楚那部分,因为从列表中学习如何做这将是一个很好的练习。

    编辑:

    另外,注意到您使用的是 java 7,而我所拥有的是 java 8。要回答您关于如何变成二维双精度数组的主要问题,您可以这样做:

    JavaRDD<double[]> valuesAndPreds = testData.map(new org.apache.spark.api.java.function.Function<LabeledPoint, double[]>() {
                    @Override
                    public double[] call(LabeledPoint point) {
                        return new double[]{model.predict(point.features()), point.label()};
                    }
                });
    

    然后运行collect,得到两个双打的列表。另外,要提示打印部分,请查看 java.util.Arrays toString 实现。

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 2016-07-12
      • 1970-01-01
      • 2016-07-07
      • 2020-08-11
      • 2018-06-29
      • 2013-07-11
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多