-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created JavaDecisionTree example from example in docs, and corrected …
…doc example as needed.
- Loading branch information
Showing
2 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
examples/src/main/java/org/apache/spark/examples/mllib/JavaDecisionTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.examples.mllib; | ||
|
||
import java.util.HashMap; | ||
|
||
import scala.reflect.ClassTag; | ||
import scala.Tuple2; | ||
|
||
import org.apache.spark.api.java.function.Function2; | ||
import org.apache.spark.api.java.JavaPairRDD; | ||
import org.apache.spark.api.java.JavaRDD; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.apache.spark.api.java.function.Function; | ||
import org.apache.spark.api.java.function.PairFunction; | ||
import org.apache.spark.mllib.regression.LabeledPoint; | ||
import org.apache.spark.mllib.tree.DecisionTree; | ||
import org.apache.spark.mllib.tree.model.DecisionTreeModel; | ||
import org.apache.spark.mllib.util.MLUtils; | ||
import org.apache.spark.SparkConf; | ||
|
||
|
||
/** | ||
* Classification and regression using decision trees. | ||
*/ | ||
public final class JavaDecisionTree { | ||
|
||
public static void main(String[] args) { | ||
if (args.length != 1) { | ||
System.err.println("Usage: JavaDecisionTree <libsvm format data file>"); | ||
System.exit(1); | ||
} | ||
SparkConf sparkConf = new SparkConf().setAppName("JavaDecisionTree"); | ||
JavaSparkContext sc = new JavaSparkContext(sparkConf); | ||
String datapath = args[0]; | ||
|
||
JavaRDD<LabeledPoint> data = JavaRDD.fromRDD(MLUtils.loadLibSVMFile(sc.sc(), datapath)); | ||
|
||
// Compute the number of classes from the data. | ||
Integer numClasses = data.map(new Function<LabeledPoint, Double>() { | ||
@Override public Double call(LabeledPoint p) { | ||
return p.label(); | ||
} | ||
}).countByValue().size(); | ||
// Empty categoricalFeaturesInfo indicates all features are continuous. | ||
HashMap<Integer, Integer> categoricalFeaturesInfo = new HashMap<Integer, Integer>(); | ||
String impurity = "gini"; | ||
Integer maxDepth = 5; | ||
Integer maxBins = 100; | ||
|
||
// Train a DecisionTree model for classification. | ||
final DecisionTreeModel model = DecisionTree.trainClassifier(data, numClasses, | ||
categoricalFeaturesInfo, impurity, maxDepth, maxBins); | ||
|
||
// Evaluate model on training instances and compute training error | ||
JavaPairRDD<Double, Double> predictionAndLabel = | ||
data.mapToPair(new PairFunction<LabeledPoint, Double, Double>() { | ||
@Override public Tuple2<Double, Double> call(LabeledPoint p) { | ||
return new Tuple2<Double, Double>(model.predict(p.features()), p.label()); | ||
} | ||
}); | ||
Double trainErr = | ||
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() / data.count(); | ||
System.out.print("Training error: " + trainErr); | ||
System.out.print("Learned classification tree model:\n" + model); | ||
|
||
// Train a DecisionTree model for regression. | ||
impurity = "variance"; | ||
|
||
final DecisionTreeModel regressionModel = DecisionTree.trainRegressor(data, | ||
categoricalFeaturesInfo, impurity, maxDepth, maxBins); | ||
|
||
// Evaluate model on training instances and compute training error | ||
JavaPairRDD<Double, Double> regressorPredictionAndLabel = | ||
data.mapToPair(new PairFunction<LabeledPoint, Double, Double>() { | ||
@Override public Tuple2<Double, Double> call(LabeledPoint p) { | ||
return new Tuple2<Double, Double>(regressionModel.predict(p.features()), p.label()); | ||
} | ||
}); | ||
Double trainMSE = | ||
regressorPredictionAndLabel.map(new Function<Tuple2<Double, Double>, Double>() { | ||
@Override public Double call(Tuple2<Double, Double> pl) { | ||
Double diff = pl._1() - pl._2(); | ||
return diff * diff; | ||
} | ||
}).reduce(new Function2<Double, Double, Double>() { | ||
@Override public Double call(Double a, Double b) { | ||
return a + b; | ||
} | ||
}) / data.count(); | ||
System.out.print("Training Mean Squared Error: " + trainMSE); | ||
System.out.print("Learned regression tree model:\n" + regressionModel); | ||
|
||
sc.stop(); | ||
} | ||
} |