weight Param 应该是双精度的,用于确定样本的重要性,例如纠正倾斜的标签分布。
假设您有这样的数据:
val data = spark.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt")
val skewed = data
.where($"label" === 0.0).limit(5)
.union(data.where($"label" === 1.0))
skewed.groupBy($"label").count.show
+-----+-----+
|label|count|
+-----+-----+
| 0.0| 5|
| 1.0| 57|
+-----+-----+
我们可以为 label 等于 0.0 的记录添加更高的权重:
val weighted = skewed
.withColumn("weight", when($"label" === 0.0, 1.0).otherwise(0.1))
val weightedModel = new NaiveBayes().setWeightCol("weight").fit(weighted)
weightedModel.transform(weighted.where($"label" === 0.0)).show
+-----+--------------------+------+--------------------+-----------+----------+
|label| features|weight| rawPrediction|probability|prediction|
+-----+--------------------+------+--------------------+-----------+----------+
| 0.0|(692,[127,128,129...| 1.0|[-165013.81130787...| [1.0,0.0]| 0.0|
| 0.0|(692,[129,130,131...| 1.0|[-191959.02863649...| [1.0,0.0]| 0.0|
| 0.0|(692,[154,155,156...| 1.0|[-201850.30335886...| [1.0,0.0]| 0.0|
| 0.0|(692,[127,128,129...| 1.0|[-202315.73236242...| [1.0,0.0]| 0.0|
| 0.0|(692,[153,154,155...| 1.0|[-258710.53340756...| [1.0,0.0]| 0.0|
+-----+--------------------+------+--------------------+-----------+----------+
要缩放特征向量,您可以使用ElementwiseProduct。