01.04.2015 Views

1FfUrl0

1FfUrl0

1FfUrl0

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6<br />

Similarly, we do this for the prior probabilities:<br />

Accounting for arithmetic underflows<br />

There is yet another roadblock. In reality, we work with probabilities much smaller<br />

than the ones we have dealt with in the toy example. In reality, we also have more<br />

than two features, which we multiply with each other. This will quickly lead to the<br />

point where the accuracy provided by NumPy does not suffice anymore:<br />

>>> import numpy as np<br />

>>> np.set_printoptions(precision=20) # tell numpy to print out more<br />

digits (default is 8)<br />

>>> np.array([2.48E-324])<br />

array([ 4.94065645841246544177e-324])<br />

>>> np.array([2.47E-324])<br />

array([ 0.])<br />

So, how probable is it that we will ever hit a number like 2.47E-324? To answer this,<br />

we just have to imagine a likelihood for the conditional probabilities of 0.0001 and<br />

then multiply 65 of them together (meaning that we have 65 low probable feature<br />

values) and you've been hit by the arithmetic underflow:<br />

>>> x=0.00001<br />

>>> x**64 # still fine<br />

1e-320<br />

>>> x**65 # ouch<br />

0.0<br />

A float in Python is typically implemented using double in C. To find out whether<br />

it is the case for your platform, you can check it as follows:<br />

>>> import sys<br />

>>> sys.float_info<br />

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_<br />

exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307,<br />

dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)<br />

To mitigate this, you could switch to math libraries such as mpmath (http://code.<br />

google.com/p/mpmath/) that allow arbitrary accuracy. However, they are not fast<br />

enough to work as a NumPy replacement.<br />

[ 125 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!