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

    https://github.com/apache/spark/pull/15211#discussion_r96756712
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/classification/LinearSVCSuite.scala ---
    @@ -0,0 +1,251 @@
    +/*
    + * 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.classification
    +
    +import scala.util.Random
    +
    +import breeze.linalg.{DenseVector => BDV}
    +
    +import org.apache.spark.SparkFunSuite
    +import org.apache.spark.ml.classification.LinearSVCSuite._
    +import org.apache.spark.ml.feature.LabeledPoint
    +import org.apache.spark.ml.linalg.{Vector, Vectors}
    +import org.apache.spark.ml.param.ParamsSuite
    +import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTestingUtils}
    +import org.apache.spark.ml.util.TestingUtils._
    +import org.apache.spark.mllib.util.MLlibTestSparkContext
    +import org.apache.spark.sql.{Dataset, Row}
    +
    +
    +class LinearSVCSuite extends SparkFunSuite with MLlibTestSparkContext with 
DefaultReadWriteTest {
    +
    +  import testImplicits._
    +
    +  private val nPoints = 50
    +  @transient var smallBinaryDataset: Dataset[_] = _
    +  @transient var smallValidationDataset: Dataset[_] = _
    +  @transient var binaryDataset: Dataset[_] = _
    +  private val eps: Double = 1e-5
    +
    +  override def beforeAll(): Unit = {
    +    super.beforeAll()
    +
    +    // NOTE: Intercept should be small for generating equal 0s and 1s
    +    val A = 0.01
    +    val B = -1.5
    +    val C = 1.0
    +    smallBinaryDataset = generateSVMInput(A, Array[Double](B, C), nPoints, 
42).toDF()
    +    smallValidationDataset = generateSVMInput(A, Array[Double](B, C), 
nPoints, 17).toDF()
    +    binaryDataset = generateSVMInput(1.0, Array[Double](1.0, 2.0, 3.0, 
4.0), 10000, 42).toDF()
    +  }
    +
    +  /**
    +   * Enable the ignored test to export the dataset into CSV format,
    +   * so we can validate the training accuracy compared with R's e1071 
package.
    +   */
    +  ignore("export test data into CSV format") {
    +    binaryDataset.rdd.map { case Row(label: Double, features: Vector) =>
    +      label + "," + features.toArray.mkString(",")
    +    }.repartition(1).saveAsTextFile("target/tmp/LinearSVC/binaryDataset")
    +  }
    +
    +  test("Linear SVC binary classification") {
    +    val svm = new LinearSVC()
    +    val model = svm.fit(smallBinaryDataset)
    +    assert(model.transform(smallValidationDataset)
    +      .where("prediction=label").count() > nPoints * 0.8)
    +  }
    +
    +  test("Linear SVC binary classification with regularization") {
    +    val svm = new LinearSVC()
    +    val model = svm.setRegParam(0.1).fit(smallBinaryDataset)
    +    assert(model.transform(smallValidationDataset)
    +      .where("prediction=label").count() > nPoints * 0.8)
    +  }
    +
    +  test("params") {
    +    ParamsSuite.checkParams(new LinearSVC)
    +    val model = new LinearSVCModel("linearSVC", Vectors.dense(0.0), 0.0)
    +    ParamsSuite.checkParams(model)
    +  }
    +
    +  test("linear svc: default params") {
    +    val lsvc = new LinearSVC()
    +    assert(lsvc.getRegParam === 0.0)
    +    assert(lsvc.getMaxIter === 100)
    +    assert(lsvc.getFitIntercept)
    +    assert(lsvc.getTol === 1E-6)
    +    assert(lsvc.getStandardization)
    +    assert(!lsvc.isDefined(lsvc.weightCol))
    +    assert(lsvc.getThreshold === 0.0)
    +    assert(lsvc.getAggregationDepth === 2)
    +    assert(lsvc.getLabelCol === "label")
    +    assert(lsvc.getFeaturesCol === "features")
    +    assert(lsvc.getPredictionCol === "prediction")
    +    assert(lsvc.getRawPredictionCol === "rawPrediction")
    +    val model = lsvc.setMaxIter(5).fit(smallBinaryDataset)
    +    model.transform(smallBinaryDataset)
    +      .select("label", "prediction", "rawPrediction")
    +      .collect()
    +    assert(model.getThreshold === 0.0)
    +    assert(model.getFeaturesCol === "features")
    +    assert(model.getPredictionCol === "prediction")
    +    assert(model.getRawPredictionCol === "rawPrediction")
    +    assert(model.intercept !== 0.0)
    +    assert(model.hasParent)
    +    assert(model.numFeatures === 2)
    +
    +    // copied model must have the same parent.
    +    MLTestingUtils.checkCopy(model)
    +  }
    +
    +  test("linear svc doesn't fit intercept when fitIntercept is off") {
    +    val lsvc = new LinearSVC().setFitIntercept(false).setMaxIter(5)
    +    val model = lsvc.fit(smallBinaryDataset)
    +    assert(model.intercept === 0.0)
    +
    +    val lsvc2 = new LinearSVC().setFitIntercept(true).setMaxIter(5)
    +    val model2 = lsvc2.fit(smallBinaryDataset)
    +    assert(model2.intercept !== 0.0)
    +  }
    +
    +  test("linearSVC with sample weights") {
    +    def modelEquals(m1: LinearSVCModel, m2: LinearSVCModel): Unit = {
    +      assert(m1.coefficients ~== m2.coefficients absTol 0.05)
    +      assert(m1.intercept ~== m2.intercept absTol 0.05)
    +    }
    +
    +    val estimator = new LinearSVC().setRegParam(0.01).setTol(0.01)
    +    val dataset = smallBinaryDataset
    +    MLTestingUtils.testArbitrarilyScaledWeights[LinearSVCModel, LinearSVC](
    +      dataset.as[LabeledPoint], estimator, modelEquals)
    +    MLTestingUtils.testOutliersWithSmallWeights[LinearSVCModel, LinearSVC](
    +      dataset.as[LabeledPoint], estimator, 2, modelEquals)
    +    MLTestingUtils.testOversamplingVsWeighting[LinearSVCModel, LinearSVC](
    +      dataset.as[LabeledPoint], estimator, modelEquals, 42L)
    +  }
    +
    +  test("linearSVC comparison with R e1071") {
    +    val trainer1 = new LinearSVC()
    +      .setRegParam(0.00002)
    +      .setMaxIter(200)
    +      .setTol(1e-4)
    +    val model1 = trainer1.fit(binaryDataset)
    +
    +    /*
    +      Use the following R code to load the data and train the model using 
glmnet package.
    +
    +      library(e1071)
    +      data <- 
read.csv("path/target/tmp/LinearSVC/binaryDataset/part-00000", header=FALSE)
    +      label <- factor(data$V1)
    +      features <- as.matrix(data.frame(data$V2, data$V3, data$V4, data$V5))
    +      svm_model <- svm(features, label, type='C', kernel='linear', 
cost=10, scale=F, tolerance=1e-4)
    +      summary(svm_model)
    +      w <- -t(svm_model$coefs) %*% svm_model$SV
    --- End diff --
    
    Remove "-" to make values match lines coefficientsR, interceptR below


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

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to