Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7672#discussion_r35732495
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/classification/NaiveBayes.scala ---
    @@ -129,29 +129,49 @@ class NaiveBayesModel private[ml] (
           throw new UnknownError(s"Invalid modelType: ${$(modelType)}.")
       }
     
    -  override protected def predict(features: Vector): Double = {
    +  override val numClasses: Int = pi.size
    +
    +  private def posteriorProbabilities(logProb: DenseVector) = {
    +    val logProbArray = logProb.toArray
    +    val maxLog = logProbArray.max
    +    val scaledProbs = logProbArray.map(lp => math.exp(lp - maxLog))
    +    val probSum = scaledProbs.sum
    +    new DenseVector(scaledProbs.map(_ / probSum))
    +  }
    +
    +  private def multinomialCalculation(testData: Vector) = {
    +    val prob = theta.multiply(testData)
    +    BLAS.axpy(1.0, pi, prob)
    +    prob
    +  }
    +
    +  private def bernoulliCalculation(testData: Vector) = {
    +    testData.foreachActive((_, value) =>
    +      if (value != 0.0 && value != 1.0) {
    +        throw new SparkException(
    +          s"Bernoulli naive Bayes requires 0 or 1 feature values but found 
$testData.")
    +      }
    +    )
    +    val prob = thetaMinusNegTheta.get.multiply(testData)
    +    BLAS.axpy(1.0, pi, prob)
    +    BLAS.axpy(1.0, negThetaSum.get, prob)
    +    prob
    +  }
    +
    +  override protected def predictRaw(features: Vector): Vector = {
    --- End diff --
    
    I'd prefer predictRaw to return the output of multinomialCalculation and 
bernoulliCalculation. and for raw2probabilityInPlace to convert those values to 
probabilities.  That will match the behavior of LogisticRegressionModel more 
closely.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to