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 import org.apache.spark.SparkConf;
0021 import org.apache.spark.SparkContext;
0022 
0023 // $example on$
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.mllib.classification.LogisticRegressionModel;
0029 import org.apache.spark.mllib.classification.LogisticRegressionWithLBFGS;
0030 import org.apache.spark.mllib.evaluation.MulticlassMetrics;
0031 import org.apache.spark.mllib.regression.LabeledPoint;
0032 import org.apache.spark.mllib.util.MLUtils;
0033 // $example off$
0034 
0035 /**
0036  * Example for LogisticRegressionWithLBFGS.
0037  */
0038 public class JavaLogisticRegressionWithLBFGSExample {
0039   public static void main(String[] args) {
0040     SparkConf conf = new SparkConf().setAppName("JavaLogisticRegressionWithLBFGSExample");
0041     SparkContext sc = new SparkContext(conf);
0042     // $example on$
0043     String path = "data/mllib/sample_libsvm_data.txt";
0044     JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(sc, path).toJavaRDD();
0045 
0046     // Split initial RDD into two... [60% training data, 40% testing data].
0047     JavaRDD<LabeledPoint>[] splits = data.randomSplit(new double[] {0.6, 0.4}, 11L);
0048     JavaRDD<LabeledPoint> training = splits[0].cache();
0049     JavaRDD<LabeledPoint> test = splits[1];
0050 
0051     // Run training algorithm to build the model.
0052     LogisticRegressionModel model = new LogisticRegressionWithLBFGS()
0053       .setNumClasses(10)
0054       .run(training.rdd());
0055 
0056     // Compute raw scores on the test set.
0057     JavaPairRDD<Object, Object> predictionAndLabels = test.mapToPair(p ->
0058       new Tuple2<>(model.predict(p.features()), p.label()));
0059 
0060     // Get evaluation metrics.
0061     MulticlassMetrics metrics = new MulticlassMetrics(predictionAndLabels.rdd());
0062     double accuracy = metrics.accuracy();
0063     System.out.println("Accuracy = " + accuracy);
0064 
0065     // Save and load model
0066     model.save(sc, "target/tmp/javaLogisticRegressionWithLBFGSModel");
0067     LogisticRegressionModel sameModel = LogisticRegressionModel.load(sc,
0068       "target/tmp/javaLogisticRegressionWithLBFGSModel");
0069     // $example off$
0070 
0071     sc.stop();
0072   }
0073 }