1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/*
* 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 org.apache.spark.SparkFunSuite
import org.apache.spark.mllib.linalg.{DenseVector, SparseVector, Vector, Vectors}
import org.apache.spark.mllib.util.MLlibTestSparkContext
import org.apache.spark.mllib.util.TestingUtils._
import org.apache.spark.sql.{DataFrame, Row, SQLContext}
class StandardScalerSuite extends SparkFunSuite with MLlibTestSparkContext{
@transient var data: Array[Vector] = _
@transient var resWithStd: Array[Vector] = _
@transient var resWithMean: Array[Vector] = _
@transient var resWithBoth: Array[Vector] = _
override def beforeAll(): Unit = {
super.beforeAll()
data = Array(
Vectors.dense(-2.0, 2.3, 0.0),
Vectors.dense(0.0, -5.1, 1.0),
Vectors.dense(1.7, -0.6, 3.3)
)
resWithMean = Array(
Vectors.dense(-1.9, 3.433333333333, -1.433333333333),
Vectors.dense(0.1, -3.966666666667, -0.433333333333),
Vectors.dense(1.8, 0.533333333333, 1.866666666667)
)
resWithStd = Array(
Vectors.dense(-1.079898494312, 0.616834091415, 0.0),
Vectors.dense(0.0, -1.367762550529, 0.590968109266),
Vectors.dense(0.917913720165, -0.160913241239, 1.950194760579)
)
resWithBoth = Array(
Vectors.dense(-1.0259035695965, 0.920781324866, -0.8470542899497),
Vectors.dense(0.0539949247156, -1.063815317078, -0.256086180682),
Vectors.dense(0.9719086448809, 0.143033992212, 1.103140470631)
)
}
def assertResult(dataframe: DataFrame): Unit = {
dataframe.select("standarded_features", "expected").collect().foreach {
case Row(vector1: Vector, vector2: Vector) =>
assert(vector1 ~== vector2 absTol 1E-5,
"The vector value is not correct after standardization.")
}
}
test("Standardization with default parameter") {
val df0 = sqlContext.createDataFrame(data.zip(resWithStd)).toDF("features", "expected")
val standardscaler0 = new StandardScaler()
.setInputCol("features")
.setOutputCol("standarded_features")
.fit(df0)
assertResult(standardscaler0.transform(df0))
}
test("Standardization with setter") {
val df1 = sqlContext.createDataFrame(data.zip(resWithBoth)).toDF("features", "expected")
val df2 = sqlContext.createDataFrame(data.zip(resWithMean)).toDF("features", "expected")
val df3 = sqlContext.createDataFrame(data.zip(data)).toDF("features", "expected")
val standardscaler1 = new StandardScaler()
.setInputCol("features")
.setOutputCol("standarded_features")
.setWithMean(true)
.setWithStd(true)
.fit(df1)
val standardscaler2 = new StandardScaler()
.setInputCol("features")
.setOutputCol("standarded_features")
.setWithMean(true)
.setWithStd(false)
.fit(df2)
val standardscaler3 = new StandardScaler()
.setInputCol("features")
.setOutputCol("standarded_features")
.setWithMean(false)
.setWithStd(false)
.fit(df3)
assertResult(standardscaler1.transform(df1))
assertResult(standardscaler2.transform(df2))
assertResult(standardscaler3.transform(df3))
}
}
|