171

I'm trying to perform an element wise divide in python, but if a zero is encountered, I need the quotient to just be zero.

For example:

array1 = np.array([0, 1, 2])
array2 = np.array([0, 1, 1])

array1 / array2 # should be np.array([0, 1, 2])

I could always just use a for-loop through my data, but to really utilize numpy's optimizations, I need the divide function to return 0 upon divide by zero errors instead of ignoring the error.

Unless I'm missing something, it doesn't seem numpy.seterr() can return values upon errors. Does anyone have any other suggestions on how I could get the best out of numpy while setting my own divide by zero error handling?

3
  • In my python version (Python 2.7.11 |Continuum Analytics, Inc.) that is exactly the output that you get. With a warning. Aug 9, 2016 at 16:15
  • The most succinct correct answer is stackoverflow.com/a/37977222/2116338
    – mrplants
    Dec 2, 2017 at 21:51
  • In case you are doing x / np.abs(x): np.sign() maps R -> {-1, 0, 1}. Feb 15, 2022 at 16:05

9 Answers 9

334

In numpy v1.7+, you can take advantage of the "where" option for ufuncs. You can do things in one line and you don't have to deal with the errstate context manager.

>>> 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]

In this case, it does the divide calculation anywhere 'where' b does not equal zero. When b does equal zero, then it remains unchanged from whatever value you originally gave it in the 'out' argument.

5
  • 6
    If a and/or b might be integer arrays, then it's the same concept, you just need to explicitly set the correct output type: c = np.divide(a, b, out=np.zeros(a.shape, dtype=float), where=b!=0)
    – DStauffman
    Dec 10, 2019 at 20:04
  • 1
    out=np.zeros_like(a) is critical, as stated in the commented line. Jan 31, 2020 at 22:45
  • 1
    If I use np.divide(a, b, out=np.zeros_like(a), where=b!=0), I get the error Assigning to function call which doesn't return. The weird thing is, I use it twice and the error only pops up once. May 6, 2020 at 18:59
  • 2
    In case anyone is interested in which is fastest, this method is faster than @denis's/@Franck Dernoncourt's answer, running one million cycles I'm getting 8 seconds for this versus 11 seconds for theirs.
    – kory
    Oct 30, 2020 at 10:55
  • 2
    In my case it wasn't only float values precisely equal to zero that need to be 'zeroed', but those values 'close' to zero. So the following is useful np.divide(a, b, out=np.zeros_like(a), where=~np.isclose(b,np.zeros_like(b))) and you can set the atol and rtol of isclose as suits your use case.
    – Colin
    Feb 1, 2022 at 5:06
57

Building on @Franck Dernoncourt's answer, fixing -1 / 0 and my bug on scalars:

def div0( a, b, fill=np.nan ):
    """ a / b, divide by 0 -> `fill`
        div0( [-1, 0, 1], 0, fill=np.nan) -> [nan nan nan]
        div0( 1, 0, fill=np.inf ) -> inf
    """
    with np.errstate(divide='ignore', invalid='ignore'):
        c = np.true_divide( a, b )
    if np.isscalar( c ):
        return c if np.isfinite( c ) \
            else fill
    else:
        c[ ~ np.isfinite( c )] = fill
        return c
3
  • Thanks, I didn't even catch that bug with @Frank Dernoncourt's code.
    – hlin117
    Feb 29, 2016 at 17:38
  • Hi, I'm trying to do array math and I want 0/0 to result in 0 but I also want to ignore np.NaN in my calculations as well. Will this work for that? Also, I am trying to understand. What does c[ ~ np.isfinite( c )] = 0 do? I've never used ~ in python. What is it for? Thank you
    – user20408
    May 17, 2016 at 14:41
  • @user20408, ~ inverts True and False in numpy arrays: print ~ np.array([ True, False, False ]). c[ ~ np.isfinite( c )] = 0 means: find the positions where c is finite, invert those to NOT finite with ~, and set the not-finite values to 0. See also stackoverflow.com/search?q=[numpy]+"boolean+indexing"
    – denis
    May 20, 2016 at 9:07
53

Building on the other answers, and improving on:

Code:

import numpy as np

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

with np.errstate(divide='ignore', invalid='ignore'):
    c = np.true_divide(a,b)
    c[c == np.inf] = 0
    c = np.nan_to_num(c)

print('c: {0}'.format(c))

Output:

c: [ 0.          0.          0.          1.          0.66666667]
4
  • 2
    Good job for checking 0/0 as well as 1/0 errors.
    – hlin117
    Aug 20, 2015 at 4:31
  • I tried your method with the example arrays given in DStauffman's answer and it seems to result in very high numbers instead of np.inf, which remains at the final result Sep 30, 2019 at 11:01
  • I would discourage this approach. If either a or b contains NaN, your solution suddenly gives 0 as a result. This can easily hide errors in your code and is absolutely unexpected.
    – DerWeh
    Jan 13, 2020 at 11:12
  • 1
    According to recent numpy manual nan_to_num() takes values to substitute for positive inf and negative inf as well. numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None) is the signature. May 16, 2020 at 6:38
20

DEPRECATED (PYTHON 2 SOLUTION):

One-liner (throws warning)

np.nan_to_num(array1 / array2)
2
  • Divide by zero (confusingly, since it's incorrect!) gives inf in numpy, not undefined or nan. So this will set divide by zero to a very large number, not 0 as one might expect and the OP asks. You can get around this by setting posinf=0.
    – c z
    Feb 18, 2021 at 12:40
  • Ah, yeah this answer is so old that the code is in Python 2 (where it works).
    – Ulf Aslak
    Feb 19, 2021 at 13:37
17

Try doing it in two steps. Division first, then replace.

with numpy.errstate(divide='ignore'):
    result = numerator / denominator
    result[denominator == 0] = 0

The numpy.errstate line is optional, and just prevents numpy from telling you about the "error" of dividing by zero, since you're already intending to do so, and handling that case.

2
  • 5
    You should probably perform the division in the context np.errstate(divide='ignore'): Oct 8, 2014 at 4:18
  • @WarrenWeckesser Fair point. I've edited the answer to include the context. divide='warn' could also be useful if s/he wanted to still be notified. Oct 9, 2014 at 0:38
3

You can also replace based on inf, only if the array dtypes are floats, as per this answer:

>>> a = np.array([1,2,3], dtype='float')
>>> b = np.array([0,1,3], dtype='float')
>>> c = a / b
>>> c
array([ inf,   2.,   1.])
>>> c[c == np.inf] = 0
>>> c
array([ 0.,  2.,  1.])
0

One answer I found searching a related question was to manipulate the output based upon whether the denominator was zero or not.

Suppose arrayA and arrayB have been initialized, but arrayB has some zeros. We could do the following if we want to compute arrayC = arrayA / arrayB safely.

In this case, whenever I have a divide by zero in one of the cells, I set the cell to be equal to myOwnValue, which in this case would be zero

myOwnValue = 0
arrayC = np.zeros(arrayA.shape())
indNonZeros = np.where(arrayB != 0)
indZeros = np.where(arrayB = 0)

# division in two steps: first with nonzero cells, and then zero cells
arrayC[indNonZeros] = arrayA[indNonZeros] / arrayB[indNonZeros]
arrayC[indZeros] = myOwnValue # Look at footnote

Footnote: In retrospect, this line is unnecessary anyways, since arrayC[i] is instantiated to zero. But if were the case that myOwnValue != 0, this operation would do something.

1
  • indZeros = np.where(arrayB = 0) this line should be indZeros = np.where(arrayB == 0)
    – megjosh
    Dec 9, 2020 at 0:46
0

A solution with try/except to manage any kind of those numpy RuntimeWarnings:

with np.errstate(all='raise'):
    try:
        array3 = array1/array2
    except:
        array3 = array1 # Give whatever default value you like
-1

An other solution worth mentioning :

>>> a = np.array([1,2,3], dtype='float')
>>> b = np.array([0,1,3], dtype='float')
>>> b_inv = np.array([1/i if i!=0 else 0 for i in b])
>>> a*b_inv
array([0., 2., 1.])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.