[PYTHON] I don't want to use -inf with np.log

TL;DR

Specify the minimum value (maximum value) with np.clip!

Execution environment

What's going on?

In the float type of python, the value less than 1e-323 will be 0.0. The python float type can range from -1.7976931348623157e + 308 to 1.9796931348623157e + 308 in many environments.

$ python
>>> a = 1e-323
>>> b = 1e-324
>>> print(a)
 1e-323
>>> print(b)
 0.0

Since b becomes 0.0, if you log for this b, an error will occur and it will become -inf. Also, a 0% warning will appear.

>>> import numpy as np
>>> b = 1e-324
>>> np.log(b)
 __main__:1: RuntimeWarning: divide by zero encountered in log
 -inf

solution

Specify the minimum value (maximum value) with np.clip.

>>> import numpy as np
>>> b = 1e-324
>>> np.log(np.clip(a=b, a_min=1e-323, a_max=1e+10))
 -743.7469247408213

By specifying the minimum value (maximum value), it will be possible to calculate even if a value outside the float range is entered. However, values that exceed the minimum value (maximum value) are rounded to the set values, which has the disadvantage of increasing the error.

Recommended Posts

I don't want to use -inf with np.log
I want to use MATLAB feval with python
I want to use Temporary Directory with Python2
I want to use ip vrf with SONiC
I want to do ○○ with Pandas
I want to debug with Python
I want to use R functions easily with ipython notebook
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to use jar from python
I want to use Linux on mac
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to use a wildcard that I want to shell with Python remove
I want to use an external library with IBM Cloud Functions
I really want to use GitHub Flavored Markdown (GFM) with Pelican!
I tried to use lightGBM, xgboost with Boruta
I want to analyze songs with Spotify API 2
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I want to be an OREMO with setParam!
I don't want to take a coding test
I want to analyze songs with Spotify API 1
I want to use ceres solver from python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to write to a file with Python
I want to use the activation function Mish
Because I don't want to go out with people whose desktops are dirty
I want to use mkl with numpy and scipy under pyenv + poetry environment
I want to convert an image to WebP with lollipop
I want to detect unauthorized login to facebook with Jubatus (1)
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to handle optimization with python and cplex
I want to climb a mountain with reinforcement learning
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib
[TensorFlow] I want to process windows with Ragged Tensor
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
[ML Ops] I want to do multi-project with Python
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want to solve Sudoku (Sudoku)
[Python] I want to add a static directory with Flask [I want to use something other than static]
[Python] I want to use only index when looping a list with a for statement
I want to be able to analyze data with Python (Part 3)
I want to remove Python's Unresolved Import Warning with vsCode
I want to specify another version of Python with pyvenv
I want to make a blog editor with django admin
I want to start a jupyter environment with one command