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

    https://github.com/apache/spark/pull/1207#discussion_r15740240
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/mllib/feature/StandardScalerSuite.scala 
---
    @@ -0,0 +1,208 @@
    +/*
    + * 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.mllib.feature
    +
    +import org.scalatest.FunSuite
    +
    +import org.apache.spark.mllib.linalg.{DenseVector, SparseVector, Vector, 
Vectors}
    +import org.apache.spark.mllib.util.LocalSparkContext
    +import org.apache.spark.mllib.util.TestingUtils._
    +import org.apache.spark.mllib.rdd.RDDFunctions._
    +import org.apache.spark.mllib.stat.{MultivariateStatisticalSummary, 
MultivariateOnlineSummarizer}
    +import org.apache.spark.rdd.RDD
    +
    +class StandardScalerSuite extends FunSuite with LocalSparkContext {
    +
    +  private def computeSummary(data: RDD[Vector]): 
MultivariateStatisticalSummary = {
    +    data.treeAggregate(new MultivariateOnlineSummarizer)(
    +      (aggregator, data) => aggregator.add(data),
    +      (aggregator1, aggregator2) => aggregator1.merge(aggregator2))
    +  }
    +
    +  test("Standardization with dense input") {
    +    val data = Array(
    +      Vectors.dense(-2.0, 2.3, 0),
    +      Vectors.dense(0.0, -1.0, -3.0),
    +      Vectors.dense(0.0, -5.1, 0.0),
    +      Vectors.dense(3.8, 0.0, 1.9),
    +      Vectors.dense(1.7, -0.6, 0.0),
    +      Vectors.dense(0.0, 1.9, 0.0)
    +    )
    +
    +    val dataRDD = sc.parallelize(data, 3)
    +
    +    val standardizer1 = new StandardScaler(withMean = true, withStd = true)
    +    val standardizer2 = new StandardScaler()
    +    val standardizer3 = new StandardScaler(withMean = true, withStd = 
false)
    +
    +    withClue("Using a standardizer before fitting the model should throw 
exception.") {
    +      intercept[IllegalStateException] {
    +        data.map(standardizer1.transform)
    +      }
    +    }
    +
    +    standardizer1.fit(dataRDD)
    +    standardizer2.fit(dataRDD)
    +    standardizer3.fit(dataRDD)
    +
    +    val data1 = data.map(standardizer1.transform)
    +    val data2 = data.map(standardizer2.transform)
    +    val data3 = data.map(standardizer3.transform)
    +
    +    val data1RDD = standardizer1.transform(dataRDD)
    +    val data2RDD = standardizer2.transform(dataRDD)
    +    val data3RDD = standardizer3.transform(dataRDD)
    +
    +    val summary = computeSummary(dataRDD)
    +    val summary1 = computeSummary(data1RDD)
    +    val summary2 = computeSummary(data2RDD)
    +    val summary3 = computeSummary(data3RDD)
    +
    +    assert((data, data1, data1RDD.collect()).zipped.forall(
    +        (v1, v2, v3) => (v1, v2, v3) match {
    +          case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true
    +          case (v1: SparseVector, v2: SparseVector, v3: SparseVector) => 
true
    +          case _ => false
    +        }
    +      ), "The vector type should be preserved after standardization.")
    +
    +    assert((data, data2, data2RDD.collect()).zipped.forall(
    +        (v1, v2, v3) => (v1, v2, v3) match {
    +          case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true
    +          case (v1: SparseVector, v2: SparseVector, v3: SparseVector) => 
true
    +          case _ => false
    +        }
    +      ), "The vector type should be preserved after standardization.")
    +
    +    assert((data, data3, data3RDD.collect()).zipped.forall(
    +        (v1, v2, v3) => (v1, v2, v3) match {
    +          case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true
    +          case (v1: SparseVector, v2: SparseVector, v3: SparseVector) => 
true
    +          case _ => false
    +        }
    +      ), "The vector type should be preserved after standardization.")
    +
    +    assert((data1, data1RDD.collect()).zipped.forall((v1, v2) => v1 ~== v2 
absTol 1E-5))
    --- End diff --
    
    For each RDD, I just call twice of collect(). I don't want to add another 
variable for this. (ps, RDD version is used for computing the summary stats, so 
we need both.)


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