When executing the following code, numpy will give RuntimeWarning
a warning , and get inf
the division result of
>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([3, 2, 0])
>>> a / b
<input>:1: RuntimeWarning: divide by zero encountered in true_divide
array([0.33333333, 1. , inf])
This warning is due to the fact that the division by 0 exists between the two vectors performing the division. If what we expect is to get inf
after , then we can use the following method to modify the numpy warning configuration to shield the warning:
>>> np.seterr(divide="ignore")
{
'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}
>>> a / b
array([0.33333333, 1. , inf])