Naive Bayes "scores" output as probability?

32 views
Skip to first unread message

Eirik Rosvold Larsen

unread,
Feb 27, 2016, 12:18:19 PM2/27/16
to scalanlp-discuss
Hi,

I wonder how one would go about to convert the output of NaiveBayes.scores from the negative number output I now get to probabilities (doubles between 0.0 and 1.0)?

Regards, Eirik

David Hall

unread,
Feb 27, 2016, 12:45:19 PM2/27/16
to scalanlp...@googlegroups.com
They are (I think) unnormalized log probabilities, so: exp(scores - softmax(scores)) which is equivalent to normalize(exp(scores), 1) == exp(scores) / sum(exp(scores)) but is more numerically stable

Eirik Rosvold Larsen

unread,
Feb 27, 2016, 2:24:39 PM2/27/16
to scalanlp-discuss
Thanks for the quick answer.

So, NaiveBayes.scores returns Counter[String, Double]

Not quite sure how exactly you suggest I transform this. I tried:
scores.mapValues(v => math.exp(v - softmax(v)))

But I get compilation error:

Error:(24, 41) could not find implicit value for parameter impl: breeze.linalg.softmax.Impl[Double,VR]
    .mapValues(v => math.exp(v - softmax(v)))
                                        ^
Error:(24, 41) not enough arguments for method apply: (implicit impl: breeze.linalg.softmax.Impl[Double,VR])VR in trait UFunc.
Unspecified value parameter impl.
    .mapValues(v => math.exp(v - softmax(v)))
                                        ^
Could you provide a more detailed description of the transform?

Regards, Eirik

David Hall

unread,
Feb 27, 2016, 4:01:26 PM2/27/16
to scalanlp...@googlegroups.com

Not at a computer, but do the operation to the counter directly, after importing breeze.linalg._ and breeze.numerics._

Eirik Rosvold Larsen

unread,
Feb 28, 2016, 3:29:30 AM2/28/16
to scalanlp-discuss
Ah, got it:
val scores: Counter[String, Double] =   ...
val values = DenseVector(scores.valuesIterator.toArray)
exp(values - softmax(values))

Thanks a lot!

Regards, Eirik
Reply all
Reply to author
Forward
0 new messages