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

    https://github.com/apache/spark/pull/7672#discussion_r35929851
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/classification/NaiveBayes.scala ---
    @@ -129,29 +129,51 @@ 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 = {
         $(modelType) match {
           case Multinomial =>
    -        val prob = theta.multiply(features)
    -        BLAS.axpy(1.0, pi, prob)
    -        prob.argmax
    +        multinomialCalculation(features)
           case Bernoulli =>
    -        features.foreachActive{ (index, value) =>
    -          if (value != 0.0 && value != 1.0) {
    -            throw new SparkException(
    -              s"Bernoulli naive Bayes requires 0 or 1 feature values but 
found $features")
    -          }
    -        }
    -        val prob = thetaMinusNegTheta.get.multiply(features)
    -        BLAS.axpy(1.0, pi, prob)
    -        BLAS.axpy(1.0, negThetaSum.get, prob)
    -        prob.argmax
    +        bernoulliCalculation(features)
           case _ =>
             // This should never happen.
             throw new UnknownError(s"Invalid modelType: ${$(modelType)}.")
         }
       }
     
    +  override protected def raw2probabilityInPlace(rawPrediction: Vector): 
Vector = {
    +    posteriorProbabilities(rawPrediction.toDense)
    --- End diff --
    
    This should operate in place, but it is not currently.


---
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