how to create weight values for those classes with zero samples?

时间:2017-05-16 09:18:55

标签: python python-2.7 math matrix machine-learning

I have an image which has three values (0,1, and 2) that each of them relates to different objects. I want to dynamically create a probability matrix (H) like this (reads an image and calculate this for each image):

H(i, j) = 0          if i != j
H(i, j) = 1 - f(i)   if i == j (with f(i) = the frequency of class i in array)

First I counted pixels with each class:

cl0=np.count_nonzero(im == 0)   #0=background class
cl1=np.count_nonzero(im == 1)   #obj1
cl2=np.count_nonzero(im == 2)    #obj2

I have set of images that some of them do not have one class so the frequency of that value is zero. For example,(65228, 308, 0) for one image. Once I want to create inverse class weight by this formula (total number of sample)/((number of classes)*(number of sample in class i)) to give more probability those classes with less samples.

w0=round(sum_/(no_classes*cl0),3)
w1=round(sum_/(no_classes*cl1),3)
w2=round(sum_/(no_classes*cl2),3)

I am facing this error:

ZeroDivisionError: division by zero

Could someone please guide me how can I tackle with this classes with zero samples to tackle imbalance classes? How can I normalize the weights? Thank u

0 个答案:

没有答案