|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.examples.mllib; |
| 19 | + |
| 20 | +// $example on$ |
| 21 | +import scala.Tuple2; |
| 22 | + |
| 23 | +import org.apache.spark.api.java.*; |
| 24 | +import org.apache.spark.api.java.function.Function; |
| 25 | +import org.apache.spark.mllib.classification.LogisticRegressionModel; |
| 26 | +import org.apache.spark.mllib.classification.LogisticRegressionWithLBFGS; |
| 27 | +import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics; |
| 28 | +import org.apache.spark.mllib.regression.LabeledPoint; |
| 29 | +import org.apache.spark.mllib.util.MLUtils; |
| 30 | +// $example off$ |
| 31 | +import org.apache.spark.SparkConf; |
| 32 | +import org.apache.spark.SparkContext; |
| 33 | + |
| 34 | +public class JavaBinaryClassificationMetricsExample { |
| 35 | + public static void main(String[] args) { |
| 36 | + SparkConf conf = new SparkConf().setAppName("Java Binary Classification Metrics Example"); |
| 37 | + SparkContext sc = new SparkContext(conf); |
| 38 | + // $example on$ |
| 39 | + String path = "data/mllib/sample_binary_classification_data.txt"; |
| 40 | + JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(sc, path).toJavaRDD(); |
| 41 | + |
| 42 | + // Split initial RDD into two... [60% training data, 40% testing data]. |
| 43 | + JavaRDD<LabeledPoint>[] splits = |
| 44 | + data.randomSplit(new double[]{0.6, 0.4}, 11L); |
| 45 | + JavaRDD<LabeledPoint> training = splits[0].cache(); |
| 46 | + JavaRDD<LabeledPoint> test = splits[1]; |
| 47 | + |
| 48 | + // Run training algorithm to build the model. |
| 49 | + final LogisticRegressionModel model = new LogisticRegressionWithLBFGS() |
| 50 | + .setNumClasses(2) |
| 51 | + .run(training.rdd()); |
| 52 | + |
| 53 | + // Clear the prediction threshold so the model will return probabilities |
| 54 | + model.clearThreshold(); |
| 55 | + |
| 56 | + // Compute raw scores on the test set. |
| 57 | + JavaRDD<Tuple2<Object, Object>> predictionAndLabels = test.map( |
| 58 | + new Function<LabeledPoint, Tuple2<Object, Object>>() { |
| 59 | + public Tuple2<Object, Object> call(LabeledPoint p) { |
| 60 | + Double prediction = model.predict(p.features()); |
| 61 | + return new Tuple2<Object, Object>(prediction, p.label()); |
| 62 | + } |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + // Get evaluation metrics. |
| 67 | + BinaryClassificationMetrics metrics = new BinaryClassificationMetrics(predictionAndLabels.rdd()); |
| 68 | + |
| 69 | + // Precision by threshold |
| 70 | + JavaRDD<Tuple2<Object, Object>> precision = metrics.precisionByThreshold().toJavaRDD(); |
| 71 | + System.out.println("Precision by threshold: " + precision.toArray()); |
| 72 | + |
| 73 | + // Recall by threshold |
| 74 | + JavaRDD<Tuple2<Object, Object>> recall = metrics.recallByThreshold().toJavaRDD(); |
| 75 | + System.out.println("Recall by threshold: " + recall.toArray()); |
| 76 | + |
| 77 | + // F Score by threshold |
| 78 | + JavaRDD<Tuple2<Object, Object>> f1Score = metrics.fMeasureByThreshold().toJavaRDD(); |
| 79 | + System.out.println("F1 Score by threshold: " + f1Score.toArray()); |
| 80 | + |
| 81 | + JavaRDD<Tuple2<Object, Object>> f2Score = metrics.fMeasureByThreshold(2.0).toJavaRDD(); |
| 82 | + System.out.println("F2 Score by threshold: " + f2Score.toArray()); |
| 83 | + |
| 84 | + // Precision-recall curve |
| 85 | + JavaRDD<Tuple2<Object, Object>> prc = metrics.pr().toJavaRDD(); |
| 86 | + System.out.println("Precision-recall curve: " + prc.toArray()); |
| 87 | + |
| 88 | + // Thresholds |
| 89 | + JavaRDD<Double> thresholds = precision.map( |
| 90 | + new Function<Tuple2<Object, Object>, Double>() { |
| 91 | + public Double call(Tuple2<Object, Object> t) { |
| 92 | + return new Double(t._1().toString()); |
| 93 | + } |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + // ROC Curve |
| 98 | + JavaRDD<Tuple2<Object, Object>> roc = metrics.roc().toJavaRDD(); |
| 99 | + System.out.println("ROC curve: " + roc.toArray()); |
| 100 | + |
| 101 | + // AUPRC |
| 102 | + System.out.println("Area under precision-recall curve = " + metrics.areaUnderPR()); |
| 103 | + |
| 104 | + // AUROC |
| 105 | + System.out.println("Area under ROC = " + metrics.areaUnderROC()); |
| 106 | + |
| 107 | + // Save and load model |
| 108 | + model.save(sc, "target/tmp/LogisticRegressionModel"); |
| 109 | + LogisticRegressionModel sameModel = LogisticRegressionModel.load(sc, |
| 110 | + "target/tmp/LogisticRegressionModel"); |
| 111 | + // $example off$ |
| 112 | + } |
| 113 | +} |
0 commit comments