zhengruifeng commented on a change in pull request #27944: [SPARK-31180][ML] 
Implement PowerTransform
URL: https://github.com/apache/spark/pull/27944#discussion_r401522135
 
 

 ##########
 File path: 
mllib/src/test/scala/org/apache/spark/ml/feature/PowerTransformSuite.scala
 ##########
 @@ -0,0 +1,197 @@
+/*
+ * 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.feature
+
+import scala.util.Random
+
+import org.apache.spark.ml.linalg._
+import org.apache.spark.ml.param.ParamsSuite
+import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTest}
+import org.apache.spark.ml.util.TestingUtils._
+import org.apache.spark.sql.Row
+
+class PowerTransformSuite extends MLTest with DefaultReadWriteTest {
+
+  import testImplicits._
+
+  @transient var data: Array[Vector] = _
+  @transient var dataWithNoise: Array[Vector] = _
+  @transient var resWithYeoJohnson: Array[Vector] = _
+  @transient var resWithBoxCox: Array[Vector] = _
+
+  private val seed = 42L
+
+  override def beforeAll(): Unit = {
+    super.beforeAll()
+
+    data = Array(
+      Vectors.dense(1.28331718, 1.18092228, 0.84160269),
+      Vectors.dense(0.94293279, 1.60960836, 0.3879099),
+      Vectors.dense(1.35235668, 0.21715673, 1.09977091)
+    )
+
+    val rng = new Random(seed)
+    dataWithNoise = data.flatMap { vector =>
+      val values = vector.toArray
+      Iterator.tabulate(1000) { i =>
+        val valuesWithNoise = values.map { v => v + rng.nextGaussian * 1e-6 }
+        Vectors.dense(valuesWithNoise)
+      }
+    }
+
+    resWithYeoJohnson = Array(
+      Vectors.dense(1.88609649e+02, 1.64321816e+00, 1.26875990e+00),
+      Vectors.dense(4.39620682e+01, 2.44852195e+00, 4.76941835e-01),
+      Vectors.dense(2.46706192e+02, 2.33862299e-01, 1.83629062e+00)
+    )
+
+    resWithBoxCox = Array(
+      Vectors.dense(0.49024348, 0.17881995, -0.15637811),
+      Vectors.dense(-0.05102892, 0.58863196, -0.57612414),
+      Vectors.dense(0.69420008, -0.84857822, 0.10051454)
+    )
+  }
+
+  test("params") {
+    ParamsSuite.checkParams(new PowerTransform)
+    val lambda = Vectors.dense(1, 0.5, 3)
+    val model = new PowerTransformModel("ptm", lambda)
+    ParamsSuite.checkParams(model)
+  }
+
+  private def assertResult: Row => Unit = {
+    case Row(vector1: Vector, vector2: Vector) =>
+      assert(vector1 ~== vector2 relTol 1E-5,
+        "The vector value is not correct after transformation.")
+  }
+
+  test("Yeo-Johnson") {
+    /*
+      Using the following Python code to load the data and train the model 
using
+      scikit-learn package.
+
+      from sklearn.preprocessing import PowerTransformer
+      import numpy as np
+
+      X = np.array([[1.28331718, 1.18092228, 0.84160269],
+                    [0.94293279, 1.60960836, 0.3879099],
+                    [1.35235668, 0.21715673, 1.09977091]], dtype=np.float)
+      pt = PowerTransformer(standardize=False)
+      ptm = pt.fit(X)
+
+      >>> ptm.lambdas_
+      array([9.00955644, 1.72211468, 2.16092368])
+      >>> ptm.transform(X)
+      array([[1.88609649e+02, 1.64321816e+00, 1.26875990e+00],
+             [4.39620682e+01, 2.44852195e+00, 4.76941835e-01],
+             [2.46706192e+02, 2.33862299e-01, 1.83629062e+00]])
+     */
+
+    val df = data.zip(resWithYeoJohnson).toSeq.toDF("features", "expected")
+    val pt = new PowerTransform()
+      .setInputCol("features")
+      .setOutputCol("transformed")
+      .setModelType("yeo-johnson")
+
+    val ptm = pt.fit(df)
+    assert(ptm.lambda ~== Vectors.dense(9.00955644, 1.72211468, 2.16092368) 
relTol 1e-5)
+
+    val transformed = ptm.transform(df)
+    checkVectorSizeOnDF(transformed, "transformed", ptm.numFeatures)
+
+    testTransformer[(Vector, Vector)](df, ptm, "transformed", "expected")(
+      assertResult)
+  }
+
+  test("Box-Cox") {
+    /*
+      Python code:
+
+      pt = PowerTransformer(method="box-cox", standardize=False)
+      ptm = pt.fit(X)
+
+      >>> ptm.lambdas_
+      array([4.92011835, 0.86296595, 1.15354434])
+      >>> ptm.transform(X)
+      array([[ 0.49024348,  0.17881995, -0.15637811],
+             [-0.05102892,  0.58863196, -0.57612414],
+             [ 0.69420008, -0.84857822,  0.10051454]])
+     */
+
+    val df = data.zip(resWithBoxCox).toSeq.toDF("features", "expected")
+    val pt = new PowerTransform()
+      .setInputCol("features")
+      .setOutputCol("transformed")
+      .setModelType("box-cox")
+
+    val ptm = pt.fit(df)
+    assert(ptm.lambda ~== Vectors.dense(4.92011835, 0.86296595, 1.15354434) 
relTol 1e-5)
+
+    val transformed = ptm.transform(df)
+    checkVectorSizeOnDF(transformed, "transformed", ptm.numFeatures)
+
+    testTransformer[(Vector, Vector)](df, ptm, "transformed", "expected")(
+      assertResult)
+  }
+
+  test("Yeo-Johnson with down-sampling") {
+    val df = dataWithNoise.map(Tuple1.apply).toSeq.toDF("features")
+    val pt = new PowerTransform()
+      .setInputCol("features")
+      .setOutputCol("transformed")
+      .setModelType("yeo-johnson")
+
+    val ptm = pt.fit(df)
+    assert(ptm.lambda ~== Vectors.dense(9.00955644, 1.72211468, 2.16092368) 
relTol 1e-5)
+
+    pt.setNumBins(100)
 
 Review comment:
   about 3000 distinct values, 100 bins, each bin contains about 30 values

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to