【发布时间】:2017-12-30 04:30:08
【问题描述】:
我正在 Spark/Scala 中执行朴素贝叶斯分类。好像没问题,代码是:
import org.apache.spark.ml.feature.{HashingTF, IDF, Tokenizer}
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.ml.feature.StringIndexer
val dfLemma2 = dfLemma.withColumn("racist", 'racist.cast("String"))
val indexer = new StringIndexer().setInputCol("racist").setOutputCol("indexracist")
val indexed = indexer.fit(dfLemma2).transform(dfLemma2)
indexed.show()
val hashingTF = new HashingTF()
.setInputCol("lemma").setOutputCol("rawFeatures").setNumFeatures(20)
val featurizedData = hashingTF.transform(indexed)
val idf = new IDF().setInputCol("rawFeatures").setOutputCol("features")
val idfModel = idf.fit(featurizedData)
val rescaledData = idfModel.transform(featurizedData)
rescaledData.select("features", "indexracist").take(3).foreach(println)
val changedTypedf = rescaledData.withColumn("indexracist", 'indexracist.cast("double"))
changedTypedf.show()
// val labeled = changedTypedf.map(row => LabeledPoint(row(0), row.getAs[Vector](4)))
val labeled = changedTypedf.select("indexracist","features").rdd.map(row => LabeledPoint(
row.getAs[Double]("indexracist"),
org.apache.spark.mllib.linalg.Vectors.fromML(row.getAs[org.apache.spark.ml.linalg.SparseVector]("features"))
))
import org.apache.spark.mllib.classification.{NaiveBayes, NaiveBayesModel}
import org.apache.spark.mllib.util.MLUtils
// Split data into training (60%) and test (40%).
val Array(training, test) = labeled.randomSplit(Array(0.6, 0.4))
val model = NaiveBayes.train(training, lambda = 1.0, modelType = "multinomial")
val predictionAndLabel = test.map(p => (model.predict(p.features), p.label))
predictionAndLabel.take(100)
这个输出:
res330: Array[(Double, Double)] = Array((0.0,0.0), (0.0,0.0), (0.0,0.0), (0.0,0.0),
我假设是一个(预测,标签)对的数组。 我想输出的是这些对加入到原始文本中,这是训练数据框中称为引理的列,因此类似于:
--------------------------------------------------
| Prediction | Label | lemma |
--------------------------------------------------
| 0.0 | 0.0 |[cakes, are, good] |
| 0.0 | 0.0 |[jim, says, hi] |
| 1.0 | 1.0 |[shut, the, dam, door]|
...
--------------------------------------------------
感谢任何指针,因为我的 Spark/Scala 很弱。
编辑,文本列在“索引”中称为“引理”:
+------+-------------------------------------------------------------------------------------------------------------------+
|racist|lemma |
+------+-------------------------------------------------------------------------------------------------------------------+
|true |[@cllrwood, abbo, @ukip, britainfirst] |
|false |[objectofthemonth, george, lansbury, bust, jussuf, abbo, amp, fascinating, insight, son, jerome] |
|false |[nowplay, one, night, stand, van, brave, @bbraveofficial, bbravesquad, abbo, safe] |
|false |[@mahesh, weet, son, satyamurthy, kante, abbo, chana, better, aaamovie] |
【问题讨论】:
-
stackoverflow.com/questions/7539831/scala-draw-table-to-console - 可能有您正在寻找的答案。我不知道有任何 Spark/Scala OOTB 方法可以做到这一点。
-
谢谢谢尔盖。我真正关心的不是格式,而是来自两个数据集的正确行/字段的组合。对大多数 Scala 程序员来说可能很明显,但对我来说不是!
-
我可以从您的输入中获取样本吗?我会在你的解决方案中工作
标签: scala apache-spark apache-spark-mllib