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

    https://github.com/apache/spark/pull/3833#discussion_r23823997
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/mllib/classification/LogisticRegressionSuite.scala
 ---
    @@ -285,6 +377,97 @@ class LogisticRegressionSuite extends FunSuite with 
MLlibTestSparkContext with M
         assert(modelB1.weights(0) !~== modelB3.weights(0) * 1.0E6 absTol 0.1)
       }
     
    +  test("multinomial logistic regression with LBFGS") {
    +    val nPoints = 10000
    +
    +    /**
    +     * The following weights and xMean/xVariance are computed from iris 
dataset with lambda = 0.2.
    +     * As a result, we are actually drawing samples from probability 
distribution of built model.
    +     */
    +    val weights = Array(
    +      -0.57997, 0.912083, -0.371077, -0.819866, 2.688191,
    +      -0.16624, -0.84355, -0.048509, -0.301789, 4.170682)
    +
    +    val xMean = Array(5.843, 3.057, 3.758, 1.199)
    +    val xVariance = Array(0.6856, 0.1899, 3.116, 0.581)
    +
    +    val testData = 
LogisticRegressionSuite.generateMultinomialLogisticInput(
    +      weights, xMean, xVariance, true, nPoints, 42)
    +
    +    val testRDD = sc.parallelize(testData, 2)
    +    testRDD.cache()
    +
    +    val lr = new 
LogisticRegressionWithLBFGS().setIntercept(true).setNumOfClasses(3)
    +    lr.optimizer.setConvergenceTol(1E-15).setNumIterations(200)
    +
    +    val model = lr.run(testRDD)
    +
    +    /**
    +     * The following is the instruction to reproduce the model using R's 
glmnet package.
    +     *
    +     * First of all, using the following scala code to save the data into 
`path`.
    +     *
    +     *    testRDD.map(x => x.label+ ", " + x.features(0) + ", " + 
x.features(1) + ", " +
    +     *      x.features(2) + ", " + x.features(3)).saveAsTextFile("path")
    +     *
    +     * Using the following R code to load the data and train the model 
using glmnet package.
    +     *
    +     *    library("glmnet")
    +     *    data <- read.csv("/Users/dbtsai/data.csv/a", header=FALSE)
    +     *    label = factor(data$V1)
    +     *    features = as.matrix(data.frame(data$V2, data$V3, data$V4, 
data$V5))
    +     *    weights = coef(glmnet(features,label, family="multinomial", 
alpha = 0, lambda = 0))
    +     *
    +     * The model weights of mutinomial logstic regression in R have `K` 
set of linear predictors
    +     * for `K` classes classification problem; however, only `K-1` set is 
required if the first
    +     * outcome is chosen as a "pivot", and the other `K-1` outcomes are 
separately regressed against
    +     * the pivot outcome. This can be done by subtracting the first 
weights from those `K-1` set
    +     * weights. The mathematical discussion and proof can be found here:
    +     * http://en.wikipedia.org/wiki/Multinomial_logistic_regression
    +     *
    +     *    weights1 = weights$`1` - weights$`0`
    +     *    weights2 = weights$`2` - weights$`0`
    +     *
    +     *    > weights1
    +     *    5 x 1 sparse Matrix of class "dgCMatrix"
    +     *                    s0
    +     *             2.6228269
    +     *    data.V2 -0.5837166
    +     *    data.V3  0.9285260
    +     *    data.V4 -0.3783612
    +     *    data.V5 -0.8123411
    +     *    > weights2
    +     *    5 x 1 sparse Matrix of class "dgCMatrix"
    +     *                     s0
    +     *             4.11197445
    +     *    data.V2 -0.16918650
    +     *    data.V3 -0.81104784
    +     *    data.V4 -0.06463799
    +     *    data.V5 -0.29198337
    +     */
    +
    +    assert(model.weights(0) ~== -0.5837166 relTol 0.05)
    --- End diff --
    
    `assert(model.weights ~== Array(-0.58, ..., 4.11) relTol 0.05)`


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