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

    https://github.com/apache/spark/pull/6189#discussion_r30432509
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/classification/NaiveBayes.scala ---
    @@ -85,17 +89,22 @@ class NaiveBayesModel private[mllib] (
       }
     
       override def predict(testData: Vector): Double = {
    -    val brzData = testData.toBreeze
         modelType match {
           case "Multinomial" =>
    -        labels(brzArgmax(brzPi + brzTheta * brzData))
    +        val prob = thetaMatrix.multiply(testData.toDense)
    +        BLAS.axpy(1.0, piVector, prob)
    +        labels(prob.argmax)
           case "Bernoulli" =>
    -        if (!brzData.forall(v => v == 0.0 || v == 1.0)) {
    -          throw new SparkException(
    -            s"Bernoulli Naive Bayes requires 0 or 1 feature values but 
found $testData.")
    +        testData.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 $testData.")
    +          }
             }
    -        labels(brzArgmax(brzPi +
    -          (brzTheta - brzNegTheta.get) * brzData + brzNegThetaSum.get))
    +        val prob = thetaMinusnegTheta.get.multiply(testData.toDense)
    --- End diff --
    
    `toDense` could be quite expensive. It would be great if we first add 
`multiply(x: Vector)` and optimize the implementation for `SparseVector`, then 
update this PR to use it.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to