Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python small floats tending to zero

Tags:

I have a Bayesian Classifier programmed in Python, the problem is that when I multiply the features probabilities I get VERY small float values like 2.5e-320 or something like that, and suddenly it turns into 0.0. The 0.0 is obviously of no use to me since I must find the "best" class based on which class returns the MAX value (greater value).

What would be the best way to deal with this? I thought about finding the exponential portion of the number (-320) and, if it goes too low, multiplying the value by 1e20 or some value like that. But maybe there is a better way?

like image 347
Pravel Avatar asked Sep 13 '10 21:09

Pravel


People also ask

What is the smallest float in Python?

The smallest is sys. float_info. min (2.2250738585072014e-308) and the biggest is sys. float_info.

Is 0.0 A float in Python?

Python float() Method The float() method is a built-in Python function that is used to convert an integer or a string to a floating-point value. The float() method takes in one parameter: the value you want to convert to a float. This parameter is optional and its default value is 0.0.

How do floats work in Python?

Float() is a method that returns a floating-point number for a provided number or string. Float() returns the value based on the argument or parameter value that is being passed to it. If no value or blank parameter is passed, it will return the values 0.0 as the floating-point output.

How do you avoid float errors in Python?

An alternative is to stick to floats and add something that is exactly representable as a binary float: values of the form I/2**J . For example, instead of adding 0.01, add 0.125 (1/8) or 0.0625 (1/16).


1 Answers

What you describe is a standard problem with the naive Bayes classifier. You can search for underflow with that to find the answer. or see here.

The short answer is it is standard to express all that in terms of logarithms. So rather than multiplying probabilities, you sum their logarithms.

You might want to look at other algorithms as well for classification.

like image 152
Muhammad Alkarouri Avatar answered Sep 21 '22 08:09

Muhammad Alkarouri