如何从Spark MLlib的JavaDecsionTreeRegressionExample.java获取预测值?

时间:2017-09-06 09:22:22

标签: java regression apache-spark-mllib decision-tree

我想从JavaDecisionTreeRegressionExample.java获取预测值,但不仅仅是决策树的描述和MAE和RMSE等指标。有谁知道怎么做或者我可以使用哪种方法来获得预测值?

我已经尝试过很多方法,它们由RegressionEvaluator和DecisionTreeRegressionModel类提供,以解决这个问题,但我仍然不知道如何获取它们。所以,如果有人知道怎么做,请告诉我。非常感谢你!

以下是JavaDecisionTreeRegressionExample.java的源代码

package org.apache.spark.examples.ml;
// $example on$
import org.apache.spark.ml.Pipeline;
import org.apache.spark.ml.PipelineModel;
import org.apache.spark.ml.PipelineStage;
import org.apache.spark.ml.evaluation.RegressionEvaluator;
import org.apache.spark.ml.feature.VectorIndexer;
import org.apache.spark.ml.feature.VectorIndexerModel;
import org.apache.spark.ml.regression.DecisionTreeRegressionModel;
import org.apache.spark.ml.regression.DecisionTreeRegressor;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
// $example off$

public class JavaDecisionTreeRegressionExample {
  public static void main(String[] args) {
    SparkSession spark = SparkSession
      .builder()
      .appName("JavaDecisionTreeRegressionExample")
      .getOrCreate();
    // $example on$
    // Load the data stored in LIBSVM format as a DataFrame.
    Dataset<Row> data = spark.read().format("libsvm")
      .load("data/mllib/sample_libsvm_data.txt");

    // Automatically identify categorical features, and index them.
    // Set maxCategories so features with > 4 distinct values are treated as continuous.
    VectorIndexerModel featureIndexer = new VectorIndexer()
      .setInputCol("features")
      .setOutputCol("indexedFeatures")
      .setMaxCategories(4)
      .fit(data);

    // Split the data into training and test sets (30% held out for testing).
    Dataset<Row>[] splits = data.randomSplit(new double[]{0.7, 0.3});
    Dataset<Row> trainingData = splits[0];
    Dataset<Row> testData = splits[1];

    // Train a DecisionTree model.
    DecisionTreeRegressor dt = new DecisionTreeRegressor()
      .setFeaturesCol("indexedFeatures");

    // Chain indexer and tree in a Pipeline.
    Pipeline pipeline = new Pipeline()
      .setStages(new PipelineStage[]{featureIndexer, dt});

    // Train model. This also runs the indexer.
    PipelineModel model = pipeline.fit(trainingData);

    // Make predictions.
    Dataset<Row> predictions = model.transform(testData);

    // Select example rows to display.
    predictions.select("label", "features").show(5);

    // Select (prediction, true label) and compute test error.
    RegressionEvaluator evaluator = new RegressionEvaluator()
      .setLabelCol("label")
      .setPredictionCol("prediction")
      .setMetricName("rmse");
    double rmse = evaluator.evaluate(predictions);
    System.out.println("Root Mean Squared Error (RMSE) on test data = " + rmse);

    DecisionTreeRegressionModel treeModel =
      (DecisionTreeRegressionModel) (model.stages()[1]);
    System.out.println("Learned regression tree model:\n" + treeModel.toDebugString());
    // $example off$

    spark.stop();
  }
}

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。 predictions.select("prediction","label", "features").show(5);Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.isSupported(Unknown Source) at javafx.graphics/com.sun.javafx.application.PlatformImpl.isSupportedImpl(Unknown Source) at javafx.graphics/com.sun.javafx.application.PlatformImpl.isSupported(Unknown Source) at javafx.graphics/javafx.application.Platform.isSupported(Unknown Source) at javafx.graphics/com.sun.javafx.scene.input.PickResultChooser.processOffer(Unknown Source) at javafx.graphics/com.sun.javafx.scene.input.PickResultChooser.offer(Unknown Source) at javafx.graphics/javafx.scene.Node.intersects(Unknown Source) at javafx.graphics/javafx.scene.Node.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.access$500(Unknown Source) at javafx.graphics/javafx.scene.Node$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Parent.access$600(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.access$600(Unknown Source) at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.access$600(Unknown Source) at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.access$600(Unknown Source) at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.access$600(Unknown Source) at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.access$600(Unknown Source) at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.access$600(Unknown Source) at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Unknown Source) at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.layout.Region.access$600(Unknown Source) at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Unknown Source) at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(Unknown Source) at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(Unknown Source) at javafx.graphics/javafx.scene.Node.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Scene$MouseHandler.pickNode(Unknown Source) at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1400(Unknown Source) at javafx.graphics/javafx.scene.Scene.pick(Unknown Source) at javafx.graphics/javafx.scene.Scene.access$6900(Unknown Source) at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source) at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source) at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source) at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source) at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source) at java.base/java.lang.Thread.run(Unknown Source) 然后,您可以获得预测值。