Github user srowen commented on the pull request:
https://github.com/apache/spark/pull/6761#issuecomment-117248710
Here is roughly the code to compute expected class probabilities for a
given piece of input:
```
def expectedMultinomialProbabilities(model: NaiveBayesModel, testData:
Vector) = {
val piVector = new BDV(model.pi)
// model.labels is row-major; treat it as col-major representation of
transpose, and transpose:
val thetaMatrix = new BDM(model.theta(0).length, model.theta.length,
model.theta.flatten).t
val logClassProbs = piVector + (thetaMatrix * testData.toBreeze)
logClassProbs.toArray.map(math.exp)
}
def expectedBernoulliProbabilities(model: NaiveBayesModel, testData:
Vector) = {
val piVector = new BDV(model.pi)
val thetaMatrix = new BDM(model.theta(0).length, model.theta.length,
model.theta.flatten).t
val negThetaMatrix = new BDM(model.theta(0).length, model.theta.length,
model.theta.flatten.map(v => math.log(1.0 - math.exp(v)))).t
val testBreeze = testData.toBreeze
val negTestBreeze = new BDV(Array.fill(testBreeze.size)(1.0)) -
testBreeze
val logClassProbs = piVector + (thetaMatrix * testBreeze) +
(negThetaMatrix * negTestBreeze)
logClassProbs.toArray.map(math.exp)
}
```
I intentionally approached it differently from the implementation in
`NaiveBayesModel`; this is probably more straightforward but less performant,
but that's the point of a cross-check in a test I suppose.
I haven't tested it; maybe I can test my test tomorrow. It should at least
compile and be 90+% there.
---
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]