Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.examples.mllib;
0019 
0020 // $example on$
0021 import java.util.HashMap;
0022 import java.util.Map;
0023 
0024 import scala.Tuple2;
0025 
0026 import org.apache.spark.api.java.JavaPairRDD;
0027 import org.apache.spark.api.java.JavaRDD;
0028 import org.apache.spark.api.java.JavaSparkContext;
0029 import org.apache.spark.mllib.regression.LabeledPoint;
0030 import org.apache.spark.mllib.tree.RandomForest;
0031 import org.apache.spark.mllib.tree.model.RandomForestModel;
0032 import org.apache.spark.mllib.util.MLUtils;
0033 import org.apache.spark.SparkConf;
0034 // $example off$
0035 
0036 public class JavaRandomForestRegressionExample {
0037   public static void main(String[] args) {
0038     // $example on$
0039     SparkConf sparkConf = new SparkConf().setAppName("JavaRandomForestRegressionExample");
0040     JavaSparkContext jsc = new JavaSparkContext(sparkConf);
0041     // Load and parse the data file.
0042     String datapath = "data/mllib/sample_libsvm_data.txt";
0043     JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(jsc.sc(), datapath).toJavaRDD();
0044     // Split the data into training and test sets (30% held out for testing)
0045     JavaRDD<LabeledPoint>[] splits = data.randomSplit(new double[]{0.7, 0.3});
0046     JavaRDD<LabeledPoint> trainingData = splits[0];
0047     JavaRDD<LabeledPoint> testData = splits[1];
0048 
0049     // Set parameters.
0050     // Empty categoricalFeaturesInfo indicates all features are continuous.
0051     Map<Integer, Integer> categoricalFeaturesInfo = new HashMap<>();
0052     int numTrees = 3; // Use more in practice.
0053     String featureSubsetStrategy = "auto"; // Let the algorithm choose.
0054     String impurity = "variance";
0055     int maxDepth = 4;
0056     int maxBins = 32;
0057     int seed = 12345;
0058     // Train a RandomForest model.
0059     RandomForestModel model = RandomForest.trainRegressor(trainingData,
0060       categoricalFeaturesInfo, numTrees, featureSubsetStrategy, impurity, maxDepth, maxBins, seed);
0061 
0062     // Evaluate model on test instances and compute test error
0063     JavaPairRDD<Double, Double> predictionAndLabel =
0064       testData.mapToPair(p -> new Tuple2<>(model.predict(p.features()), p.label()));
0065     double testMSE = predictionAndLabel.mapToDouble(pl -> {
0066       double diff = pl._1() - pl._2();
0067       return diff * diff;
0068     }).mean();
0069     System.out.println("Test Mean Squared Error: " + testMSE);
0070     System.out.println("Learned regression forest model:\n" + model.toDebugString());
0071 
0072     // Save and load model
0073     model.save(jsc.sc(), "target/tmp/myRandomForestRegressionModel");
0074     RandomForestModel sameModel = RandomForestModel.load(jsc.sc(),
0075       "target/tmp/myRandomForestRegressionModel");
0076     // $example off$
0077 
0078     jsc.stop();
0079   }
0080 }