如何获得 Spark Naive Bayes 分类器中类的概率? [英] How to get the probabilities of classes in Spark Naive Bayes classifier?

查看:24
本文介绍了如何获得 Spark Naive Bayes 分类器中类的概率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Spark 中训练 NaiveBayesModel,但是当我使用它来预测新实例时,我需要获得每个类的概率.我查看了 NaiveBayesModel 中预测函数的代码,得出如下代码:

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
}

这能正常工作吗?行 BLAS.axpy(1.0, piVector, prob) 不断给我一个错误,即找不到值axpy".

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

推荐答案

在最近的pull-request 这已添加到 Spark 主干中,并将在 Spark 1.5 中发布(关闭 SPARK-4362).因此你可以调用

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]

def predictProbabilities(testData: Vector): Vector

这篇关于如何获得 Spark Naive Bayes 分类器中类的概率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆