jerry
jerry

Reputation: 405

How to implement a certain operation on some specific bins of histogram?

In the following code hist shows the values of counts of a histogram. In to implement certain operation only on the bins having counts grater than zero and grater than 1. But when I store the files and print E_bin it also prints the empty arrays which is because it considers hist having values zero. How can I overcome this problem and store only those files where hist values are grater than 0 and one?

`hist:  [3., 0., 0., 0., 0., 0., 1,. 2., 0., 3.]
for j in range(len(hist)):
    val =hist[j]
    E_bin =[]
    for k in range(len(w)):
        if j<len(hist)-1 and val>0 and bin_edges[j]<= w[k] <bin_edges[j+1]:
            E_bin.append(w[k])
        elif j==len(hist)-1 and  val>0 and bin_edges[j]<= w[k]<= 
        bin_edges[j+1]:
            E_bin.append(w[k]) 
    E_bin = np.array(E_bin)
    print("E_bin: ",E_bin)
    np.save("./InputData/Samples/Sample_%s_bin_%s"%(i,j),E_bin)`

Upvotes: 0

Views: 22

Answers (1)

venkata krishnan
venkata krishnan

Reputation: 2046

use numpy.nonzero to get the indices of values which are non zero and implement your logic on that.

for i in np.nonzero(hist):
    #rest of the code

Upvotes: 1

Related Questions