Github user sethah commented on a diff in the pull request: https://github.com/apache/spark/pull/15721#discussion_r93891824 --- Diff: mllib/src/test/scala/org/apache/spark/ml/classification/NaiveBayesSuite.scala --- @@ -157,50 +162,26 @@ class NaiveBayesSuite extends SparkFunSuite with MLlibTestSparkContext with Defa validateProbabilities(featureAndProbabilities, model, "multinomial") } - test("Naive Bayes Multinomial with weighted samples") { - val nPoints = 1000 - val piArray = Array(0.5, 0.1, 0.4).map(math.log) - val thetaArray = 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 = generateNaiveBayesInput(piArray, thetaArray, nPoints, 42, "multinomial").toDF() - val (overSampledData, weightedData) = - MLTestingUtils.genEquivalentOversampledAndWeightedInstances(testData, - "label", "features", 42L) - val nb = new NaiveBayes().setModelType("multinomial") - val unweightedModel = nb.fit(weightedData) - val overSampledModel = nb.fit(overSampledData) - val weightedModel = nb.setWeightCol("weight").fit(weightedData) - assert(weightedModel.theta ~== overSampledModel.theta relTol 0.001) - assert(weightedModel.pi ~== overSampledModel.pi relTol 0.001) - assert(unweightedModel.theta !~= overSampledModel.theta relTol 0.001) - assert(unweightedModel.pi !~= overSampledModel.pi relTol 0.001) - } - - test("Naive Bayes Bernoulli with weighted samples") { - val nPoints = 10000 - val piArray = Array(0.5, 0.3, 0.2).map(math.log) - val thetaArray = Array( - 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 = generateNaiveBayesInput(piArray, thetaArray, nPoints, 42, "bernoulli").toDF() - val (overSampledData, weightedData) = - MLTestingUtils.genEquivalentOversampledAndWeightedInstances(testData, - "label", "features", 42L) - val nb = new NaiveBayes().setModelType("bernoulli") - val unweightedModel = nb.fit(weightedData) - val overSampledModel = nb.fit(overSampledData) - val weightedModel = nb.setWeightCol("weight").fit(weightedData) - assert(weightedModel.theta ~== overSampledModel.theta relTol 0.001) - assert(weightedModel.pi ~== overSampledModel.pi relTol 0.001) - assert(unweightedModel.theta !~= overSampledModel.theta relTol 0.001) - assert(unweightedModel.pi !~= overSampledModel.pi relTol 0.001) + test("Naive Bayes with weighted samples") { + val numClasses = 3 + def modelEquals(m1: NaiveBayesModel, m2: NaiveBayesModel): Unit = { + assert(m1.pi ~== m2.pi relTol 0.01) + assert(m1.theta ~== m2.theta relTol 0.01) + } + val testParams = Seq( + ("bernoulli", bernoulliDataset), + ("multinomial", dataset) + ) + testParams.foreach { case (family, dataset) => + // NaiveBayes is sensitive to constant scaling of the weights unless smoothing is set to 0 + val estimator = new NaiveBayes().setSmoothing(0.0).setModelType(family) --- End diff -- I think the test with smoothing as 0.0 is a nice check on the weighting algorithm for Naive Bayes, so I prefer to keep it. I made separate smoothing/no smoothing estimators.
--- 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