0

I am calculating the frequency of 3 different classes in images with three values 0-2. I want to obtain the median frequency weighting to balance the imbalanced number of classes.

cl1=np.count_nonzero(im == 1)   #1=class 1,   
cl2=np.count_nonzero(im == 2)   #2=class 2,
cl0=65536-(cl1+cl2)
sum_=cl0+cl1+cl2
median_=np.median([cl0,cl1,cl2])
print cl0,cl1,cl2
print median_
try:
    w0=round(median_/(cl0/sum_),4) # EAFP 
except ZeroDivisionError: 
    w0=0
try:
    w1=round(median_/(cl1/sum_),4) # EAFP 
except ZeroDivisionError: 
    w1=0
try:
    w2=round(median_/(cl2/sum_),4) # EAFP 
except ZeroDivisionError: 
    w2=0
W=[w0,w1,w2]
print W

However, I am getting division by zero error in the output

 /home/.../med.py:39: RuntimeWarning: divide by zero encountered in double_scalars
  try:w0=round(median_/(cl0/sum_),4) # EAFP
/home/.../med.py:41: RuntimeWarning: divide by zero encountered in double_scalars
  try:w1=round(median_/(cl1/sum_),4) # EAFP
/home/.../med.py:43: RuntimeWarning: divide by zero encountered in double_scalars
  try:w2=round(median_/(cl2/sum_),4) # EAFP

 65298 238 0
    238.0
    [inf, inf, inf]
    65162 374 0
    374.0
    [inf, inf, inf]
    64985 551 0
    551.0
    [inf, inf, inf]
    64712 690 134
    690.0
    .
    .
    .

Could someone guide, please? I do not know what is the reason?

S.EB
  • 1,966
  • 4
  • 29
  • 54
  • 1
    Possible duplicate of [Ignore divide by 0 warning in python](https://stackoverflow.com/questions/29950557/ignore-divide-by-0-warning-in-python) – glibdud Oct 27 '17 at 16:12
  • Sorry, maybe I don't understand the question: what are you actually trying to do? Have you given a little thought about what should happen if the denominator would be 0? – CristiFati Oct 28 '17 at 01:14

1 Answers1

0
exception ZeroDivisionError
Raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.


If I understand well what is going on base on the exception description, it is not going through the exception because the calculation is happening within the function so to have the exception triggered should be handled inside it (in this case the round function. When the function is done the result passed is inf.
Again the ZeroDivisionErrorcheck it the second argument of a division is zero and not if the result is inf. Hope I could help.

cl1=np.count_nonzero(im == 1)   #1=class 1,   
cl2=np.count_nonzero(im == 2)   #2=class 2,
cl0=65536-(cl1+cl2)
sum_=cl0+cl1+cl2
median_=np.median([cl0,cl1,cl2])
print cl0,cl1,cl2
print median_
try:
    div = median_/(cl0/sum_) 
except ZeroDivisionError: 
    w0=0
else:
    w0=round(median_/(cl0/sum_),4)
Filipe Lemos
  • 500
  • 3
  • 13
  • thanks for the comment, how can I trigger the exception inside the round function? – S.EB Oct 28 '17 at 11:51
  • There is this example from the Python documentation on what to do, I think this would be better. I added to my reply what it could look like. `>>> def divide(x, y):` `... try:` `... result = x / y` `... except ZeroDivisionError:` `... print("division by zero!")` `... else:` `... print("result is", result)` – Filipe Lemos Oct 29 '17 at 02:48
  • again the same issue...I do not know what is the problem :((( – S.EB Oct 29 '17 at 14:46
  • Hey S.EB, I played around a little and found out this: on the try loop the numbers are all `int` and to get the exception the division need to be by `float` numbers. For me worked just fine when I defined all my variables as `float`. Try and let me know, if that solve I'll edit the answer. – Filipe Lemos Oct 30 '17 at 12:51
  • Not even changing them to `float` and doing the division outside the mean? – Filipe Lemos Oct 30 '17 at 19:15