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.ml;
0019 
0020 // $example on$
0021 import org.apache.spark.ml.classification.BinaryLogisticRegressionTrainingSummary;
0022 import org.apache.spark.ml.classification.LogisticRegression;
0023 import org.apache.spark.ml.classification.LogisticRegressionModel;
0024 import org.apache.spark.sql.Dataset;
0025 import org.apache.spark.sql.Row;
0026 import org.apache.spark.sql.SparkSession;
0027 import org.apache.spark.sql.functions;
0028 // $example off$
0029 
0030 public class JavaLogisticRegressionSummaryExample {
0031   public static void main(String[] args) {
0032     SparkSession spark = SparkSession
0033       .builder()
0034       .appName("JavaLogisticRegressionSummaryExample")
0035       .getOrCreate();
0036 
0037     // Load training data
0038     Dataset<Row> training = spark.read().format("libsvm")
0039       .load("data/mllib/sample_libsvm_data.txt");
0040 
0041     LogisticRegression lr = new LogisticRegression()
0042       .setMaxIter(10)
0043       .setRegParam(0.3)
0044       .setElasticNetParam(0.8);
0045 
0046     // Fit the model
0047     LogisticRegressionModel lrModel = lr.fit(training);
0048 
0049     // $example on$
0050     // Extract the summary from the returned LogisticRegressionModel instance trained in the earlier
0051     // example
0052     BinaryLogisticRegressionTrainingSummary trainingSummary = lrModel.binarySummary();
0053 
0054     // Obtain the loss per iteration.
0055     double[] objectiveHistory = trainingSummary.objectiveHistory();
0056     for (double lossPerIteration : objectiveHistory) {
0057       System.out.println(lossPerIteration);
0058     }
0059 
0060     // Obtain the receiver-operating characteristic as a dataframe and areaUnderROC.
0061     Dataset<Row> roc = trainingSummary.roc();
0062     roc.show();
0063     roc.select("FPR").show();
0064     System.out.println(trainingSummary.areaUnderROC());
0065 
0066     // Get the threshold corresponding to the maximum F-Measure and rerun LogisticRegression with
0067     // this selected threshold.
0068     Dataset<Row> fMeasure = trainingSummary.fMeasureByThreshold();
0069     double maxFMeasure = fMeasure.select(functions.max("F-Measure")).head().getDouble(0);
0070     double bestThreshold = fMeasure.where(fMeasure.col("F-Measure").equalTo(maxFMeasure))
0071       .select("threshold").head().getDouble(0);
0072     lrModel.setThreshold(bestThreshold);
0073     // $example off$
0074 
0075     spark.stop();
0076   }
0077 }