Scala的位置:MatchError

时间:2017-07-07 18:50:18

标签: scala apache-spark

ERROR Executor: Exception in task 0.0 in stage 10.0 (TID 20)
scala.MatchError: [0.0,(20,[0,5,9,17],[0.6931471805599453,0.6931471805599453,0.28768207245178085,1.3862943611198906])] (of class org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema)

我在scala程序中看到这个错误,我试图使用NaiveBayes分类器对电影评论进行分类。我在尝试训练NaiveBayes Classifer的线上看到了这个错误。我无法更正此错误,因为我不知道分类器所期望的数据类型。 NaiveBayes的文档说它期望RDD条目是我所拥有的。任何帮助将不胜感激。请查看此电影评论分类计划的完整SCALA代码。

PS:请忽略代码中缩进的可能错误。它在我的程序文件中。提前谢谢。

    import  org.apache.spark.sql.{Dataset, DataFrame, SparkSession}
    import org.apache.spark.{SparkConf, SparkContext}
    import org.apache.spark.sql.functions._
    import org.apache.spark.sql._
    import org.apache.spark.ml.feature.{HashingTF, IDF, Tokenizer, PCA}
    import org.apache.spark.mllib.classification.{NaiveBayes,NaiveBayesModel}
    import org.apache.spark.mllib.util.MLUtils
    import org.apache.spark.mllib.regression.LabeledPoint
    import org.apache.spark.mllib.linalg._


    //Reading the file from csv into dataframe object
    val sqlContext = new SQLContext(sc)
    val df = sqlContext.read.option("header", "true").option("delimiter",",").option("inferSchema", "true").csv("movie-pang02.csv")


     //Tokenizing the data by splitting the text into words
     val tokenizer = new Tokenizer().setInputCol("text").setOutputCol("words")
     val wordsData = tokenizer.transform(df)


     //Hashing the data by converting the words into rawFeatures
     val hashingTF = new HashingTF().setInputCol("words").setOutputCol("rawFeatures").setNumFeatures(200)
     val featurizedData = hashingTF.transform(wordsData)


      //Applying Estimator on the data which converts the raw features into features by scaling each column
      val idf  = new IDF().setInputCol("rawFeatures").setOutputCol("features")
      val idfModel = idf.fit(featurizedData)
      val rescaledData = idfModel.transform(featurizedData)

      val coder: (String => Int) = (arg: String) => {if (arg == "Pos") 1 else 0}
      val sqlfunc = udf(coder)
      val new_set = rescaledData.withColumn("label", sqlfunc(col("class")))

      val EntireDataRdd = new_set.select("label","features").map{case Row(label: Int, features: Vector) =>  LabeledPoint(label.toDouble, Vectors.dense(features.toArray))}


    //Converted the data into RDD<LabeledPoint> format so as to input it into the inbuilt Naive Bayes classifier
    val labeled = EntireDataRdd.rdd
    val Array(trainingData, testData) = labeled.randomSplit(Array(0.7, 0.3), seed = 1234L)
    //Error in the following statement
    val model = NaiveBayes.train(trainingData, lambda = 1.0, modelType = "multinomial")

    val predictionAndLabel = testData.map(p => (model.predict(p.features), p.label))
    val accuracy = 1.0 * predictionAndLabel.filter(x => x._1 == x._2).count() / testData.count()
    val testErr = predictionAndLabel.filter(r => r._1 != r._2).count.toDouble / testData.count()

1 个答案:

答案 0 :(得分:1)

这是一个痛苦的(并非罕见的)陷阱 - 您将Row的内容与错误的Vector类相匹配 - 它应该是org.apache.spark.ml.linalg.Vector而不是{{1 } ...(是的 - 令人沮丧!)

在映射之前添加正确的导入可以解决此问题:

org.apache.spark.mllib.linalg.Vector