Github user yinxusen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9689#discussion_r45314368
  
    --- Diff: docs/mllib-evaluation-metrics.md ---
    @@ -1350,163 +582,21 @@ and evaluate the performance of the algorithm by 
several regression metrics.
     <div data-lang="scala" markdown="1">
     Refer to the [`RegressionMetrics` Scala 
docs](api/scala/index.html#org.apache.spark.mllib.evaluation.RegressionMetrics) 
for details on the API.
     
    -{% highlight scala %}
    -import org.apache.spark.mllib.regression.LabeledPoint
    -import org.apache.spark.mllib.regression.LinearRegressionModel
    -import org.apache.spark.mllib.regression.LinearRegressionWithSGD
    -import org.apache.spark.mllib.linalg.Vectors
    -import org.apache.spark.mllib.evaluation.RegressionMetrics
    -import org.apache.spark.mllib.util.MLUtils
    -
    -// Load the data
    -val data = MLUtils.loadLibSVMFile(sc, 
"data/mllib/sample_linear_regression_data.txt").cache()
    -
    -// Build the model
    -val numIterations = 100
    -val model = LinearRegressionWithSGD.train(data, numIterations)
    -
    -// Get predictions
    -val valuesAndPreds = data.map{ point =>
    -  val prediction = model.predict(point.features)
    -  (prediction, point.label)
    -}
    -
    -// Instantiate metrics object
    -val metrics = new RegressionMetrics(valuesAndPreds)
    -
    -// Squared error
    -println(s"MSE = ${metrics.meanSquaredError}")
    -println(s"RMSE = ${metrics.rootMeanSquaredError}")
    -
    -// R-squared
    -println(s"R-squared = ${metrics.r2}")
    -
    -// Mean absolute error
    -println(s"MAE = ${metrics.meanAbsoluteError}")
    -
    -// Explained variance
    -println(s"Explained variance = ${metrics.explainedVariance}")
    -
    -{% endhighlight %}
    +{% include_example 
scala/org/apache/spark/examples/mllib/RegressionMetricsExample.scala %}
     
     </div>
     
     <div data-lang="java" markdown="1">
     Refer to the [`RegressionMetrics` Java 
docs](api/java/org/apache/spark/mllib/evaluation/RegressionMetrics.html) for 
details on the API.
     
    -{% highlight java %}
    -import scala.Tuple2;
    -
    -import org.apache.spark.api.java.*;
    -import org.apache.spark.api.java.function.Function;
    -import org.apache.spark.mllib.linalg.Vectors;
    -import org.apache.spark.mllib.regression.LabeledPoint;
    -import org.apache.spark.mllib.regression.LinearRegressionModel;
    -import org.apache.spark.mllib.regression.LinearRegressionWithSGD;
    -import org.apache.spark.mllib.evaluation.RegressionMetrics;
    -import org.apache.spark.SparkConf;
    -
    -public class LinearRegression {
    -  public static void main(String[] args) {
    -    SparkConf conf = new SparkConf().setAppName("Linear Regression 
Example");
    -    JavaSparkContext sc = new JavaSparkContext(conf);
    -
    -    // Load and parse the data
    -    String path = "data/mllib/sample_linear_regression_data.txt";
    -    JavaRDD<String> data = sc.textFile(path);
    -    JavaRDD<LabeledPoint> parsedData = data.map(
    -      new Function<String, LabeledPoint>() {
    -        public LabeledPoint call(String line) {
    -          String[] parts = line.split(" ");
    -          double[] v = new double[parts.length - 1];
    -          for (int i = 1; i < parts.length - 1; i++)
    -            v[i - 1] = Double.parseDouble(parts[i].split(":")[1]);
    -          return new LabeledPoint(Double.parseDouble(parts[0]), 
Vectors.dense(v));
    -        }
    -      }
    -    );
    -    parsedData.cache();
    -
    -    // Building the model
    -    int numIterations = 100;
    -    final LinearRegressionModel model =
    -      LinearRegressionWithSGD.train(JavaRDD.toRDD(parsedData), 
numIterations);
    -
    -    // Evaluate model on training examples and compute training error
    -    JavaRDD<Tuple2<Object, Object>> valuesAndPreds = parsedData.map(
    -      new Function<LabeledPoint, Tuple2<Object, Object>>() {
    -        public Tuple2<Object, Object> call(LabeledPoint point) {
    -          double prediction = model.predict(point.features());
    -          return new Tuple2<Object, Object>(prediction, point.label());
    -        }
    -      }
    -    );
    -
    -    // Instantiate metrics object
    -    RegressionMetrics metrics = new 
RegressionMetrics(valuesAndPreds.rdd());
    -
    -    // Squared error
    -    System.out.format("MSE = %f\n", metrics.meanSquaredError());
    -    System.out.format("RMSE = %f\n", metrics.rootMeanSquaredError());
    -
    -    // R-squared
    -    System.out.format("R Squared = %f\n", metrics.r2());
    -
    -    // Mean absolute error
    -    System.out.format("MAE = %f\n", metrics.meanAbsoluteError());
    -
    -    // Explained variance
    -    System.out.format("Explained Variance = %f\n", 
metrics.explainedVariance());
    -
    -    // Save and load model
    -    model.save(sc.sc(), "myModelPath");
    -    LinearRegressionModel sameModel = LinearRegressionModel.load(sc.sc(), 
"myModelPath");
    -  }
    -}
    -
    -{% endhighlight %}
    +{% include_example 
java/org/apache/spark/examples/mllib/JavaLinearRegressionExample.java %}
    --- End diff --
    
    Rename it to `JavaRegressionMetricsExample`.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to