You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by yanboliang <gi...@git.apache.org> on 2017/04/21 17:17:38 UTC
[GitHub] spark pull request #17715: [SPARK-20047][ML] Constrained Logistic Regression
Github user yanboliang commented on a diff in the pull request:
https://github.com/apache/spark/pull/17715#discussion_r112736608
--- Diff: mllib/src/test/scala/org/apache/spark/ml/classification/LogisticRegressionSuite.scala ---
@@ -1149,6 +1300,49 @@ class LogisticRegressionSuite
assert(model2.interceptVector.toArray.sum ~== 0.0 absTol eps)
}
+ test("multinomial logistic regression with intercept without regularization with bound") {
+ val lowerBoundOfCoefficients = Matrices.dense(3, 4, Array.fill(12)(1.0))
+ val lowerBoundOfIntercept = Vectors.dense(Array.fill(3)(1.0))
+
+ val trainer1 = new LogisticRegression()
+ .setLowerBoundOfCoefficients(lowerBoundOfCoefficients)
+ .setLowerBoundOfIntercept(lowerBoundOfIntercept)
+ .setFitIntercept(true)
+ .setStandardization(true)
+ .setWeightCol("weight")
+ val trainer2 = new LogisticRegression()
+ .setLowerBoundOfCoefficients(lowerBoundOfCoefficients)
+ .setLowerBoundOfIntercept(lowerBoundOfIntercept)
+ .setFitIntercept(true)
+ .setStandardization(false)
+ .setWeightCol("weight")
+
+ val model1 = trainer1.fit(multinomialDataset)
+ val model2 = trainer2.fit(multinomialDataset)
+
+ // The solution is generated by https://github.com/yanboliang/bound-optimization.
+ val coefficientsExpected = new DenseMatrix(3, 4, Array(
+ 2.52076464, 2.73596057, 1.87984904, 2.73264492,
+ 1.93302281, 3.71363303, 1.50681746, 1.93398782,
+ 2.37839917, 1.93601818, 1.81924758, 2.45191255), isTransposed = true)
+ val interceptsExpected = Vectors.dense(1.00010477, 3.44237083, 4.86740286)
+
+ model1.coefficientMatrix.colIter.zip(coefficientsExpected.colIter)
+ .foreach { case (col1: Vector, col2: Vector) =>
+ (col1.asBreeze - col2.asBreeze).toArray.toSeq.sliding(2).foreach {
+ case Seq(v1, v2) => assert(v1 ~== v2 absTol 1E-3)
+ }
+ }
--- End diff --
When reg == 0, multinomial logistic regression has multiple solutions and we centralize the coefficients to get identical result for non-bound regression, but we didn't do this for bound constrained regression, since it may cross the bound if we centralize them. So here we check whether ```coefficients1``` equals to ```coefficientsExpected + constant value``` for each column.
---
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.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org