PolynomialExpansion
Polynomial expansion is the process of expanding your features into a polynomial space, which is formulated by an n-degree combination of original dimensions. A PolynomialExpansion class provides this functionality. The example below shows how to expand your features into a 3-degree polynomial space.
1
import org.apache.spark.ml.feature.PolynomialExpansion
2
import org.apache.spark.ml.linalg.Vectors
3
​
4
val data = Array(
5
Vectors.dense(2.0, 1.0),
6
Vectors.dense(0.0, 0.0),
7
Vectors.dense(3.0, -1.0)
8
)
9
val df = spark.createDataFrame(data.map(Tuple1.apply)).toDF("features")
10
​
11
val polyExpansion = new PolynomialExpansion()
12
.setInputCol("features")
13
.setOutputCol("polyFeatures")
14
.setDegree(3)
15
​
16
val polyDF = polyExpansion.transform(df)
17
polyDF.show(false)
18
​
19
/*
20
output:
21
​
22
----------+------------------------------------------+
23
|features |polyFeatures |
24
+----------+------------------------------------------+
25
|[2.0,1.0] |[2.0,4.0,8.0,1.0,2.0,4.0,1.0,2.0,1.0] |
26
|[0.0,0.0] |[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0] |
27
|[3.0,-1.0]|[3.0,9.0,27.0,-1.0,-3.0,-9.0,1.0,3.0,-1.0]|
28
+----------+------------------------------------------+
29
​
30
​
31
*/
Copied!
​
Last modified 1yr ago
Copy link