如何在 Spark 中将数字特征与文本(词袋)正确结合?
How do I properly combine numerical features with text (bag of words) in Spark?
我的问题与this类似,但针对Spark,原问题没有令人满意的答案。
我正在使用 Spark 2.2 LinearSVC 模型,将推文数据作为输入:推文的文本(已经过预处理)作为 hash-tfidf 及其月份如下:
val hashingTF = new HashingTF().setInputCol("text").setOutputCol("hash-tf")
.setNumFeatures(30000)
val idf = new IDF().setInputCol("hash-tf").setOutputCol("hash-tfidf")
.setMinDocFreq(10)
val monthIndexer = new StringIndexer().setInputCol("month")
.setOutputCol("month-idx")
val va = new VectorAssembler().setInputCols(Array("month-idx", "hash-tfidf"))
.setOutputCol("features")
如果有 30,000 个单词特征,这些不会淹没这个月吗?或者 VectorAssembler
是否足够聪明来处理这个问题。 (如果可能的话,我该如何获得该模型的最佳功能?)
VectorAssembler
将简单地将所有数据合并到一个向量中,它对权重或其他任何事情都不做任何事情。
由于 30,000 个词向量非常稀疏,因此更密集的特征(月份)很可能会对结果产生更大的影响,因此这些特征可能不会像您输入的那样得到 "swamped"它。您可以训练模型并检查特征的权重以确认这一点。只需使用 LinearSVCModel
提供的 coefficients
方法即可查看特征对最终总和的影响程度:
val model = new LinearSVC().fit(trainingData)
val coeffs = model.coefficients
系数越高的特征对最终结果的影响越大
如果给月份的权重太大low/high,可以使用setWeightCol()
方法为它们设置权重。
我的问题与this类似,但针对Spark,原问题没有令人满意的答案。
我正在使用 Spark 2.2 LinearSVC 模型,将推文数据作为输入:推文的文本(已经过预处理)作为 hash-tfidf 及其月份如下:
val hashingTF = new HashingTF().setInputCol("text").setOutputCol("hash-tf")
.setNumFeatures(30000)
val idf = new IDF().setInputCol("hash-tf").setOutputCol("hash-tfidf")
.setMinDocFreq(10)
val monthIndexer = new StringIndexer().setInputCol("month")
.setOutputCol("month-idx")
val va = new VectorAssembler().setInputCols(Array("month-idx", "hash-tfidf"))
.setOutputCol("features")
如果有 30,000 个单词特征,这些不会淹没这个月吗?或者 VectorAssembler
是否足够聪明来处理这个问题。 (如果可能的话,我该如何获得该模型的最佳功能?)
VectorAssembler
将简单地将所有数据合并到一个向量中,它对权重或其他任何事情都不做任何事情。
由于 30,000 个词向量非常稀疏,因此更密集的特征(月份)很可能会对结果产生更大的影响,因此这些特征可能不会像您输入的那样得到 "swamped"它。您可以训练模型并检查特征的权重以确认这一点。只需使用 LinearSVCModel
提供的 coefficients
方法即可查看特征对最终总和的影响程度:
val model = new LinearSVC().fit(trainingData)
val coeffs = model.coefficients
系数越高的特征对最终结果的影响越大
如果给月份的权重太大low/high,可以使用setWeightCol()
方法为它们设置权重。