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.SparkConf;
0027 import org.apache.spark.api.java.JavaPairRDD;
0028 import org.apache.spark.api.java.JavaRDD;
0029 import org.apache.spark.api.java.JavaSparkContext;
0030 import org.apache.spark.mllib.regression.LabeledPoint;
0031 import org.apache.spark.mllib.tree.GradientBoostedTrees;
0032 import org.apache.spark.mllib.tree.configuration.BoostingStrategy;
0033 import org.apache.spark.mllib.tree.model.GradientBoostedTreesModel;
0034 import org.apache.spark.mllib.util.MLUtils;
0035 // $example off$
0036 
0037 public class JavaGradientBoostingClassificationExample {
0038   public static void main(String[] args) {
0039     // $example on$
0040     SparkConf sparkConf = new SparkConf()
0041       .setAppName("JavaGradientBoostedTreesClassificationExample");
0042     JavaSparkContext jsc = new JavaSparkContext(sparkConf);
0043 
0044     // Load and parse the data file.
0045     String datapath = "data/mllib/sample_libsvm_data.txt";
0046     JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(jsc.sc(), datapath).toJavaRDD();
0047     // Split the data into training and test sets (30% held out for testing)
0048     JavaRDD<LabeledPoint>[] splits = data.randomSplit(new double[]{0.7, 0.3});
0049     JavaRDD<LabeledPoint> trainingData = splits[0];
0050     JavaRDD<LabeledPoint> testData = splits[1];
0051 
0052     // Train a GradientBoostedTrees model.
0053     // The defaultParams for Classification use LogLoss by default.
0054     BoostingStrategy boostingStrategy = BoostingStrategy.defaultParams("Classification");
0055     boostingStrategy.setNumIterations(3); // Note: Use more iterations in practice.
0056     boostingStrategy.getTreeStrategy().setNumClasses(2);
0057     boostingStrategy.getTreeStrategy().setMaxDepth(5);
0058     // Empty categoricalFeaturesInfo indicates all features are continuous.
0059     Map<Integer, Integer> categoricalFeaturesInfo = new HashMap<>();
0060     boostingStrategy.treeStrategy().setCategoricalFeaturesInfo(categoricalFeaturesInfo);
0061 
0062     GradientBoostedTreesModel model = GradientBoostedTrees.train(trainingData, boostingStrategy);
0063 
0064     // Evaluate model on test instances and compute test error
0065     JavaPairRDD<Double, Double> predictionAndLabel =
0066       testData.mapToPair(p -> new Tuple2<>(model.predict(p.features()), p.label()));
0067     double testErr =
0068       predictionAndLabel.filter(pl -> !pl._1().equals(pl._2())).count() / (double) testData.count();
0069     System.out.println("Test Error: " + testErr);
0070     System.out.println("Learned classification GBT model:\n" + model.toDebugString());
0071 
0072     // Save and load model
0073     model.save(jsc.sc(), "target/tmp/myGradientBoostingClassificationModel");
0074     GradientBoostedTreesModel sameModel = GradientBoostedTreesModel.load(jsc.sc(),
0075       "target/tmp/myGradientBoostingClassificationModel");
0076     // $example off$
0077 
0078     jsc.stop();
0079   }
0080 
0081 }