How to get the probabilities of classes in Spark Naive Bayes classifier?

好久不见. 提交于 2019-12-11 02:07:14

问题


I'm training a NaiveBayesModel in Spark, however when I'm using it to predict a new instance I need to get the probabilities for each class. I looked at the code of predict function in NaiveBayesModel and come up with the following code:

val thetaMatrix = new DenseMatrix (model.labels.length,model.theta(0).length,model.theta.flatten,true)
val piVector = new DenseVector(model.pi)
//val prob = thetaMatrix.multiply(test.features)

val x = test.map {p =>       
  val prob = thetaMatrix.multiply(p.features)          
  BLAS.axpy(1.0, piVector, prob)
  prob
}

Does this work properly? The line BLAS.axpy(1.0, piVector, prob) keeps giving me an error that the value 'axpy' is not found.


回答1:


In a recent pull-request this was added to the Spark trunk and will be released in Spark 1.5 (closing SPARK-4362). you can therefore call

def predictProbabilities(testData: RDD[Vector]): RDD[Vector]

or

def predictProbabilities(testData: Vector): Vector



来源:https://stackoverflow.com/questions/31842502/how-to-get-the-probabilities-of-classes-in-spark-naive-bayes-classifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!