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

    https://github.com/apache/spark/pull/4259#discussion_r24056350
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/regression/LinearRegressionSuite.scala 
---
    @@ -0,0 +1,169 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.ml.regression
    +
    +import org.scalatest.FunSuite
    +
    +import org.apache.spark.mllib.linalg.DenseVector
    +import org.apache.spark.mllib.util.TestingUtils._
    +import org.apache.spark.mllib.util.{LinearDataGenerator, 
MLlibTestSparkContext}
    +import org.apache.spark.sql.{SQLContext, DataFrame}
    +
    +class LinearRegressionSuite extends FunSuite with MLlibTestSparkContext {
    +
    +  @transient var sqlContext: SQLContext = _
    +  @transient var dataset: DataFrame = _
    +
    +  /**
    +   * In `LinearRegressionSuite`, we will make sure that the model trained 
by SparkML
    +   * is the same as the one trained by R's glmnet package. The following 
instruction
    +   * describes how to reproduce the data in R.
    +   *
    +   * val data =
    +   *   sc.parallelize(LinearDataGenerator.generateLinearInput(6.3, 
Array(4.7, 7.2), 10000, 42), 2)
    +   * data.map(x=> x.label + ", " + x.features(0) + ", " + 
x.features(1)).saveAsTextFile("path")
    +   */
    +  override def beforeAll(): Unit = {
    +    super.beforeAll()
    +    sqlContext = new SQLContext(sc)
    +    dataset = sqlContext.createDataFrame(
    +      sc.parallelize(LinearDataGenerator.generateLinearInput(6.3, 
Array(4.7, 7.2), 10000, 42), 2))
    +  }
    +
    +  test("linear regression with intercept without regularization") {
    +    val trainer = new LinearRegression
    +    val model = trainer.fit(dataset)
    +
    +    /**
    +     * Using the following R code to load the data and train the model 
using glmnet package.
    +     *
    +     * library("glmnet")
    +     * data <- read.csv("path", header=FALSE, stringsAsFactors=FALSE)
    +     * features <- as.matrix(data.frame(as.numeric(data$V2), 
as.numeric(data$V3)))
    +     * label <- as.numeric(data$V1)
    +     * weights <- coef(glmnet(features, label, family="gaussian", alpha = 
0, lambda = 0))
    +     * > weights
    +     *  3 x 1 sparse Matrix of class "dgCMatrix"
    +     *                           s0
    +     * (Intercept)         6.300528
    +     * as.numeric.data.V2. 4.701024
    +     * as.numeric.data.V3. 7.198257
    --- End diff --
    
    This is great!


---
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