Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

prevent division by zero numpy

>>> a = np.array([-1, 0, 1, 2, 3], dtype=float)
>>> b = np.array([ 0, 0, 0, 2, 2], dtype=float)

# If you don't pass `out` the indices where (b == 0) will be uninitialized!
>>> c = np.divide(a, b, out=np.zeros(a.shape, dtype=float), where=b!=0)
>>> print(c)
[ 0.   0.   0.   1.   1.5]
Comment

prevent division by zero numpy

>>> a = np.array([-1, 0, 1, 2, 3], dtype=float)
>>> b = np.array([ 0, 0, 0, 2, 2], dtype=float)

# If you don't pass `out` the indices where (b == 0) will be uninitialized!
>>> c = np.divide(a, b, out=np.zeros_like(a), where=b!=0)
>>> print(c)
[ 0.   0.   0.   1.   1.5]
Comment

python - avoid division by zero in numpy.where()

out = a.copy()
out[mask] /= b[mask]
Comment

PREVIOUS NEXT
Code Example
Python :: python timer 
Python :: python get current date 
Python :: basic script 
Python :: pandas melt() function, change the DataFrame format from wide to long 
Python :: flask error handling 
Python :: at=error code=H10 desc="App crashed" django 
Python :: feature selection python 
Python :: get tweet by its id 
Python :: add image pptx python 
Python :: planets list 
Python :: write cell output to file jupyter colab 
Python :: alpha vantage import 
Python :: Python program to print negative numbers in a list 
Python :: python test if list of dicts has key 
Python :: how to use a function to find the average in python 
Python :: prolog avg of list 
Python :: python uppercase 
Python :: Groups the DataFrame using the specified columns 
Python :: unique combinations in python 
Python :: keras maxpooling1d 
Python :: django get query parameters 
Python :: How To Get Redirection URL In Python 
Python :: read .mat file in python 
Python :: insert single value in dataframe using index 
Python :: conda install pypy 
Python :: how to take space separated input in python 
Python :: how to check a string is palindrome or not in python 
Python :: df astype 
Python :: rgb color python 
Python :: remove multiple elements from a list in python 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =