Bucketizer

Bucketizer

transforms a column of continuous features to a column of feature buckets, where the buckets are specified by users. It takes a parameter:
splits: Parameter for mapping continuous features into buckets. With n+1 splits, there are n buckets. A bucket defined by splits x,y holds values in the range [x,y) except the last bucket, which also includes y. Splits should be strictly increasing. Values at -inf, inf must be explicitly provided to cover all Double values; Otherwise, values outside the splits specified will be treated as errors. Two examples of splits are Array(Double.NegativeInfinity, 0.0, 1.0, Double.PositiveInfinity) and Array(0.0, 1.0, 2.0).
Note that if you have no idea of the upper and lower bounds of the targeted column, you should add Double.NegativeInfinity and Double.PositiveInfinity as the bounds of your splits to prevent a potential out of Bucketizer bounds exception.
Note also that the splits that you provided have to be in strictly increasing order, i.e. s0 < s1 < s2 < ... < sn.
1
import org.apache.spark.ml.feature.Bucketizer
2
​
3
val splits = Array(Double.NegativeInfinity, -0.5, 0.0, 0.5, Double.PositiveInfinity)
4
​
5
val data = Array(-999.9, -0.5, -0.3, 0.0, 0.2, 999.9)
6
val dataFrame = spark.createDataFrame(data.map(Tuple1.apply)).toDF("features")
7
​
8
val bucketizer = new Bucketizer()
9
.setInputCol("features")
10
.setOutputCol("bucketedFeatures")
11
.setSplits(splits)
12
​
13
// Transform original data into its bucket index.
14
val bucketedData = bucketizer.transform(dataFrame)
15
​
16
println(s"Bucketizer output with ${bucketizer.getSplits.length-1} buckets")
17
bucketedData.show()
18
​
19
val splitsArray = Array(
20
Array(Double.NegativeInfinity, -0.5, 0.0, 0.5, Double.PositiveInfinity),
21
Array(Double.NegativeInfinity, -0.3, 0.0, 0.3, Double.PositiveInfinity))
22
​
23
val data2 = Array(
24
(-999.9, -999.9),
25
(-0.5, -0.2),
26
(-0.3, -0.1),
27
(0.0, 0.0),
28
(0.2, 0.4),
29
(999.9, 999.9))
30
val dataFrame2 = spark.createDataFrame(data2).toDF("features1", "features2")
31
​
32
val bucketizer2 = new Bucketizer()
33
.setInputCols(Array("features1", "features2"))
34
.setOutputCols(Array("bucketedFeatures1", "bucketedFeatures2"))
35
.setSplitsArray(splitsArray)
36
​
37
// Transform original data into its bucket index.
38
val bucketedData2 = bucketizer2.transform(dataFrame2)
39
​
40
println(s"Bucketizer output with [" +
41
s"${bucketizer2.getSplitsArray(0).length-1}, " +
42
s"${bucketizer2.getSplitsArray(1).length-1}] buckets for each input column")
43
​
44
/*
45
Bucketizer output with 4 buckets
46
+--------+----------------+
47
|features|bucketedFeatures|
48
+--------+----------------+
49
| -999.9| 0.0|
50
| -0.5| 1.0|
51
| -0.3| 1.0|
52
| 0.0| 2.0|
53
| 0.2| 2.0|
54
| 999.9| 3.0|
55
+--------+----------------+
56
​
57
Bucketizer output with [4, 4] buckets for each input column
58
+---------+---------+-----------------+-----------------+
59
|features1|features2|bucketedFeatures1|bucketedFeatures2|
60
+---------+---------+-----------------+-----------------+
61
| -999.9| -999.9| 0.0| 0.0|
62
| -0.5| -0.2| 1.0| 1.0|
63
| -0.3| -0.1| 1.0| 1.0|
64
| 0.0| 0.0| 2.0| 2.0|
65
| 0.2| 0.4| 2.0| 3.0|
66
| 999.9| 999.9| 3.0| 3.0|
67
+---------+---------+-----------------+-----------------+
68
*/
Copied!
​
Last modified 1yr ago
Copy link