Github user yanboliang commented on a diff in the pull request:
https://github.com/apache/spark/pull/15721#discussion_r93765319
--- 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 it's not practical to set smoothing as 0.0, so it's better to test
NB with none zero smoothing value. If it does not applicable to
```testArbitrarilyScaledWeights```, we can omit it. Or generating two
estimators whose smoothing values are respectively 0.0 and 1.0 for different
test functions.
---
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]