Github user miccagiann commented on a diff in the pull request: https://github.com/apache/spark/pull/1311#discussion_r14926802 --- Diff: docs/mllib-linear-methods.md --- @@ -242,7 +242,96 @@ Similarly, you can use replace `SVMWithSGD` by All of MLlib's methods use Java-friendly types, so you can import and call them there the same way you do in Scala. The only caveat is that the methods take Scala RDD objects, while the Spark Java API uses a separate `JavaRDD` class. You can convert a Java RDD to a Scala one by -calling `.rdd()` on your `JavaRDD` object. +calling `.rdd()` on your `JavaRDD` object. A standalone application example +that is equivalent to the provided example in Scala is given bellow: + +{% highlight java %} +import java.util.Random; + +import scala.Tuple2; + +import org.apache.spark.api.java.*; +import org.apache.spark.SparkConf; +import org.apache.spark.api.java.function.Function; +import org.apache.spark.SparkContext; +import org.apache.spark.mllib.regression.LabeledPoint; +import org.apache.spark.mllib.util.MLUtils; +import org.apache.spark.mllib.classification.*; +import org.apache.spark.mllib.linalg.Vector; +import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics; + +public class SVMClassifier { + public static void main(String[] args) { + SparkConf conf = new SparkConf().setAppName("SVM Classifier Example"); + SparkContext sc = new SparkContext(conf); + String path = "{SPARK_HOME}/mllib/data/sample_libsvm_data.txt"; + JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(sc, path).toJavaRDD(); + + // Split initial RDD into two... [60% training data, 40% testing data]. + JavaRDD<LabeledPoint> training = 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; + } + } + ); + training.cache(); + JavaRDD<LabeledPoint> test = data.subtract(training); + + // Run training algorithm to build the model. + int numIterations = 100; + final SVMModel model = SVMWithSGD.train(JavaRDD.toRDD(training), numIterations); + + // Clear the default threshold. + model.clearThreshold(); + + // Compute raw scores on the test set. + JavaRDD<Tuple2<Object, Object>> scoreAndLabels = test.map( + new Function<LabeledPoint, Tuple2<Object, Object>>() { + public final SVMModel m = model; + public Tuple2<Object, Object> call(LabeledPoint p) { + Double score = m.predict(p.features()); --- End diff -- The only requirement for an inner class to call a variable declared in the outer class is by declaring the variable as final. I have fixed it! Thanks!
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---