VectorSizeHint
It can sometimes be useful to explicitly specify the size of the vectors for a column of VectorType. For example, VectorAssembler uses size information from its input columns to produce size information and metadata for its output column. While in some cases this information can be obtained by inspecting the contents of the column, in a streaming dataframe the contents are not available until the stream is started. VectorSizeHint allows a user to explicitly specify the vector size for a column so that VectorAssembler, or other transformers that might need to know vector size, can use that column as an input.
To use VectorSizeHint a user must set the inputCol and size parameters. Applying this transformer to a dataframe produces a new dataframe with updated metadata for inputCol specifying the vector size. Downstream operations on the resulting dataframe can get this size using the meatadata.
VectorSizeHint can also take an optional handleInvalid parameter which controls its behaviour when the vector column contains nulls or vectors of the wrong size. By default handleInvalid is set to โ€œerrorโ€, indicating an exception should be thrown. This parameter can also be set to โ€œskipโ€, indicating that rows containing invalid values should be filtered out from the resulting dataframe, or โ€œoptimisticโ€, indicating that the column should not be checked for invalid values and all rows should be kept. Note that the use of โ€œoptimisticโ€ can cause the resulting dataframe to be in an inconsistent state, me:aning the metadata for the column VectorSizeHint was applied to does not match the contents of that column. Users should take care to avoid this kind of inconsistent state.
1
import org.apache.spark.ml.feature.{VectorAssembler, VectorSizeHint}
2
import org.apache.spark.ml.linalg.Vectors
3
โ€‹
4
val dataset = spark.createDataFrame(
5
Seq(
6
(0, 18, 1.0, Vectors.dense(0.0, 10.0, 0.5), 1.0),
7
(0, 18, 1.0, Vectors.dense(0.0, 10.0), 0.0))
8
).toDF("id", "hour", "mobile", "userFeatures", "clicked")
9
โ€‹
10
val sizeHint = new VectorSizeHint()
11
.setInputCol("userFeatures")
12
.setHandleInvalid("skip")
13
.setSize(3)
14
โ€‹
15
val datasetWithSize = sizeHint.transform(dataset)
16
println("Rows where 'userFeatures' is not the right size are filtered out")
17
datasetWithSize.show(false)
18
โ€‹
19
val assembler = new VectorAssembler()
20
.setInputCols(Array("hour", "mobile", "userFeatures"))
21
.setOutputCol("features")
22
โ€‹
23
// This dataframe can be used by downstream transformers as before
24
val output = assembler.transform(datasetWithSize)
25
println("Assembled columns 'hour', 'mobile', 'userFeatures' to vector column 'features'")
26
output.select("features", "clicked").show(false)
27
โ€‹
28
/*
29
Output:
30
Rows where 'userFeatures' is not the right size are filtered out
31
+---+----+------+--------------+-------+
32
|id |hour|mobile|userFeatures |clicked|
33
+---+----+------+--------------+-------+
34
|0 |18 |1.0 |[0.0,10.0,0.5]|1.0 |
35
+---+----+------+--------------+-------+
36
โ€‹
37
Assembled columns 'hour', 'mobile', 'userFeatures' to vector column 'features'
38
+-----------------------+-------+
39
|features |clicked|
40
+-----------------------+-------+
41
|[18.0,1.0,0.0,10.0,0.5]|1.0 |
42
+-----------------------+-------+
43
*/
Copied!
Last modified 1yr ago
Copy link