0

I'm trying to use try/except to take care of a divide by zero error, but the except doesn't seem to be catching the error and I can't figure out why.

import numpy as np

A = np.array([[0.5,0,0.25,0.125,0.05],[0.5,0.5,0.5,0.5,0.5]])
Y = np.array([[0,1,0,1,1],[0,0,0,0,1]])
p = np.shape(A)[0]
q = np.shape(A)[1]
m = 2
try:
    dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A)) 
except Exception:
    dA = np.zeros((p,q))

It returns a divide by zero error and an invalid value encountered in multiply (because of the divide by zero error), but it kills the program instead of doing what's in except.

RuntimeWarning: divide by zero encountered in true_divide
  dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A))
RuntimeWarning: invalid value encountered in multiply
  dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A))

I tried specifying the error instead of just Exception, but that doesn't work either. What else could be the problem?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
LunarLlama
  • 229
  • 2
  • 11
  • 1
    You have perhaps a divide by zero in np.zeros? – Gerard H. Pille Feb 25 '18 at 14:49
  • 2
    can you create a [mcve]? this sounds surprising – Jean-François Fabre Feb 25 '18 at 14:49
  • 2
    @GerardH.Pille: Hardly: `np.zeros` just creates a 0 filled matrix – Jean-François Fabre Feb 25 '18 at 14:50
  • @Jean-FrançoisFabre is right, we don't know the values of your variables and adding the specific tracebacks to the question could be useful too. – G_M Feb 25 '18 at 14:50
  • If what you divide by is not standard python 0. but numpy 0. it only throws "RuntimeWarning: divide by zero encountered in true_divide" but not error. – Dinesh Feb 25 '18 at 14:51
  • 2
    @edit that's why we need a [mcve]. I'm tired of questions stating that "python is not working". – Jean-François Fabre Feb 25 '18 at 14:52
  • It is a RuntimeWarning: divide by zero, I didn't think this wouldn't raise an exception though. Would I then define a new exception for this? – LunarLlama Feb 25 '18 at 14:53
  • @LunarLlama, as said above without minimal, complete example, its not possible to say. It will also help you debug yourself and you may find solution yourself. – Dinesh Feb 25 '18 at 14:54
  • @Jean-FrançoisFabre I added the rest of my variables; I'm just testing a part of a larger program that seems to not be working, so I didn't think they'd be that important, but thanks for catching me on this. – LunarLlama Feb 25 '18 at 14:56

1 Answers1

3

Division by zero if it is numpy array does not throw Exception, but only gives warning and gives inf values.

You can use numpy.isfinite to check if values in the array are finite (and not nan or inf).

A = np.array([[0.5,0,0.25,0.125,0.05],[0.5,0.5,0.5,0.5,0.5]])
Y = np.array([[0,1,0,1,1],[0,0,0,0,1]])
p = np.shape(A)[0]
q = np.shape(A)[1]
m = 2
try:
    dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A)) 
except Exception:
    dA = np.zeros((p,q))

if not np.isfinite(dA).all():
    dA = np.zeros((p,q))

If you are certain your elements are numpy arrays, you don't need exception:

dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A)) 
if not np.isfinite(dA).all():
    dA = np.zeros((p,q))
Dinesh
  • 1,555
  • 1
  • 16
  • 18