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.mllib.classification;
0019 
0020 import java.util.List;
0021 
0022 import org.junit.Assert;
0023 import org.junit.Test;
0024 
0025 import org.apache.spark.SharedSparkSession;
0026 import org.apache.spark.api.java.JavaRDD;
0027 import org.apache.spark.mllib.regression.LabeledPoint;
0028 
0029 public class JavaLogisticRegressionSuite extends SharedSparkSession {
0030 
0031   int validatePrediction(List<LabeledPoint> validationData, LogisticRegressionModel model) {
0032     int numAccurate = 0;
0033     for (LabeledPoint point : validationData) {
0034       Double prediction = model.predict(point.features());
0035       if (prediction == point.label()) {
0036         numAccurate++;
0037       }
0038     }
0039     return numAccurate;
0040   }
0041 
0042   @Test
0043   public void runLRUsingConstructor() {
0044     int nPoints = 10000;
0045     double A = 2.0;
0046     double B = -1.5;
0047 
0048     JavaRDD<LabeledPoint> testRDD = jsc.parallelize(
0049       LogisticRegressionSuite.generateLogisticInputAsList(A, B, nPoints, 42), 2).cache();
0050     List<LabeledPoint> validationData =
0051       LogisticRegressionSuite.generateLogisticInputAsList(A, B, nPoints, 17);
0052 
0053     LogisticRegressionWithSGD lrImpl = new LogisticRegressionWithSGD(1.0, 100, 1.0, 1.0);
0054     lrImpl.setIntercept(true);
0055     LogisticRegressionModel model = lrImpl.run(testRDD.rdd());
0056 
0057     int numAccurate = validatePrediction(validationData, model);
0058     Assert.assertTrue(numAccurate > nPoints * 4.0 / 5.0);
0059   }
0060 
0061   @Test
0062   public void runLRUsingStaticMethods() {
0063     int nPoints = 10000;
0064     double A = 0.0;
0065     double B = -2.5;
0066 
0067     JavaRDD<LabeledPoint> testRDD = jsc.parallelize(
0068       LogisticRegressionSuite.generateLogisticInputAsList(A, B, nPoints, 42), 2).cache();
0069     List<LabeledPoint> validationData =
0070       LogisticRegressionSuite.generateLogisticInputAsList(A, B, nPoints, 17);
0071 
0072     LogisticRegressionModel model = new LogisticRegressionWithSGD(1.0, 100, 0.01, 1.0)
0073         .run(testRDD.rdd());
0074 
0075     int numAccurate = validatePrediction(validationData, model);
0076     Assert.assertTrue(numAccurate > nPoints * 4.0 / 5.0);
0077   }
0078 }