You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by jkbradley <gi...@git.apache.org> on 2015/02/26 02:46:42 UTC

[GitHub] spark pull request: [SPARK-4894][mllib] Added Bernoulli option to ...

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

    https://github.com/apache/spark/pull/4087#discussion_r25398725
  
    --- Diff: mllib/src/test/scala/org/apache/spark/mllib/classification/NaiveBayesSuite.scala ---
    @@ -71,23 +86,67 @@ class NaiveBayesSuite extends FunSuite with MLlibTestSparkContext {
         assert(numOfPredictions < input.length / 5)
       }
     
    -  test("Naive Bayes") {
    +  def validateModelFit(piData: Array[Double], thetaData: Array[Array[Double]], model: NaiveBayesModel) = {
    +    def closeFit(d1: Double, d2: Double, precision: Double): Boolean = {
    +      (d1 - d2).abs <= precision
    +    }
    +    val modelIndex = (0 until piData.length).zip(model.labels.map(_.toInt))
    +    for (i <- modelIndex) {
    +      assert(closeFit(math.exp(piData(i._2)), math.exp(model.pi(i._1)), 0.05))
    +    }
    +    for (i <- modelIndex) {
    +      for (j <- 0 until thetaData(i._2).length) {
    +        assert(closeFit(math.exp(thetaData(i._2)(j)), math.exp(model.theta(i._1)(j)), 0.05))
    +      }
    +    }
    +  }
    +
    +  test("Naive Bayes Multinomial") {
    +    val nPoints = 1000
    +
    +    val pi = Array(0.5, 0.1, 0.4).map(math.log)
    +    val theta = Array(
    +      Array(0.70, 0.10, 0.10, 0.10), // label 0
    +      Array(0.10, 0.70, 0.10, 0.10), // label 1
    +      Array(0.10, 0.10, 0.70, 0.10)  // label 2
    +    ).map(_.map(math.log))
    +
    +    val testData = NaiveBayesSuite.generateNaiveBayesInput(pi, theta, nPoints, 42, NaiveBayesModels.Multinomial)
    +    val testRDD = sc.parallelize(testData, 2)
    +    testRDD.cache()
    +
    +    val model = NaiveBayes.train(testRDD, 1.0, "Multinomial")
    +    validateModelFit(pi, theta, model)
    +
    +    val validationData = NaiveBayesSuite.generateNaiveBayesInput(pi, theta, nPoints, 17, NaiveBayesModels.Multinomial)
    +    val validationRDD = sc.parallelize(validationData, 2)
    +
    +    // Test prediction on RDD.
    +    validatePrediction(model.predict(validationRDD.map(_.features)).collect(), validationData)
    +
    +    // Test prediction on Array.
    +    validatePrediction(validationData.map(row => model.predict(row.features)), validationData)
    +  }
    +
    +  test("Naive Bayes Bernoulli") {
         val nPoints = 10000
     
         val pi = Array(0.5, 0.3, 0.2).map(math.log)
         val theta = Array(
    -      Array(0.91, 0.03, 0.03, 0.03), // label 0
    -      Array(0.03, 0.91, 0.03, 0.03), // label 1
    -      Array(0.03, 0.03, 0.91, 0.03)  // label 2
    +      Array(0.50, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.40), // label 0
    +      Array(0.02, 0.70, 0.10, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02), // label 1
    +      Array(0.02, 0.02, 0.60, 0.02,  0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.30)  // label 2
         ).map(_.map(math.log))
     
    -    val testData = NaiveBayesSuite.generateNaiveBayesInput(pi, theta, nPoints, 42)
    +
    +    val testData = NaiveBayesSuite.generateNaiveBayesInput(pi, theta, nPoints, 45, NaiveBayesModels.Bernoulli)
         val testRDD = sc.parallelize(testData, 2)
         testRDD.cache()
     
    -    val model = NaiveBayes.train(testRDD)
    +    val model = NaiveBayes.train(testRDD, 1.0, "Bernoulli") ///!!! this gives same result on both models check the math
    --- End diff --
    
    Just wondering--- is the bug listed here still happening?


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

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