spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mengxr <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-1945][MLLIB] Documentation Improvements...
Date Mon, 14 Jul 2014 03:39:37 GMT
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1311#discussion_r14863037
  
    --- Diff: docs/mllib-optimization.md ---
    @@ -263,7 +267,110 @@ println("Loss of each step in training process")
     loss.foreach(println)
     println("Area under ROC = " + auROC)
     {% endhighlight %}
    -
    +</div>
    +
    +<div data-lang="java" markdown="1">
    +{% highlight java %}
    +import java.util.Random;
    +import java.util.Arrays;
    +
    +import scala.Product2;
    +import scala.Tuple2;
    +
    +import org.apache.spark.api.java.*;
    +import org.apache.spark.SparkConf;
    +import org.apache.spark.SparkContext;
    +import org.apache.spark.api.java.function.Function;
    +import org.apache.spark.mllib.regression.LabeledPoint;
    +import org.apache.spark.mllib.util.MLUtils;
    +import org.apache.spark.mllib.linalg.Vector;
    +import org.apache.spark.mllib.linalg.Vectors;
    +import org.apache.spark.mllib.optimization.*;
    +import org.apache.spark.mllib.classification.LogisticRegressionModel;
    +import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics;
    +
    +public class LBFGSExample {
    +  public static void main(String[] args) {
    +    SparkConf conf = new SparkConf().setAppName("L-BFGS Example");
    +    SparkContext sc = new SparkContext(conf);
    +    String path = "{SPARK_HOME}/mllib/data/sample_libsvm_data.txt";
    +    JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(sc, path).toJavaRDD();
    +    int numFeatures = data.take(1).get(0).features().size();
    +    
    +    // Split initial RDD into two... [60% training data, 40% testing data].
    +    JavaRDD<LabeledPoint> trainingInit = data.filter(
    +      new Function<LabeledPoint, Boolean>() {
    +      public final Random random = new Random(11L);
    +      public Boolean call(LabeledPoint p) {
    +        if (random.nextDouble() <= 0.6)
    +        return true;
    +        else
    +        return false;
    +      }
    +      }
    +    );
    +    JavaRDD<LabeledPoint> test = data.subtract(trainingInit);
    +    
    +    // Append 1 into the training data as intercept.
    +    JavaRDD<Tuple2<Object, Vector>> training = data.map(
    +      new Function<LabeledPoint, Tuple2<Object, Vector>>() {
    +        public Tuple2<Object, Vector> call(LabeledPoint p) {
    +          return new Tuple2<Object, Vector>(p.label(), MLUtils.appendBias(p.features()));
    +        }
    +      });
    +    training.cache();
    +
    +    // Run training algorithm to build the model.
    +    int numCorrections = 10;
    +    double convergenceTol = 1e-4;
    +    int maxNumIterations = 20;
    +    double regParam = 0.1;
    +    Vector initialWeightsWithIntercept = Vectors.dense(new double[numFeatures + 1]);
    +
    +    Tuple2<Vector, double[]> result = LBFGS.runLBFGS(
    +        JavaRDD.toRDD(training),
    +        new LogisticGradient(),
    +        new SquaredL2Updater(),
    +        numCorrections,
    +        convergenceTol,
    +        maxNumIterations,
    +        regParam,
    +        initialWeightsWithIntercept);
    +    Vector weightsWithIntercept = (Vector) result.productElement(0);
    --- End diff --
    
    use `._1()` (and `._2()` below)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message