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?