I have a 2d numpy array with shape - (20,1200) e.g. 24000 pixels, with values of zeros and ones. I want to iterate with blocks of (4,60) and sum the percentage of one's per block, meaning - ((number_of_ones)/240)*100. The result should be injected to a new numpy table (5X20) as each cell represents (4X60) from the original table. Currently, I'm using 2 nested for loops, but it takes time and not best practice.
final_seg = []
for i in range(0, 20, 4):
for j in range(0, 1200, 60):
# slice segment from table
data = big_table[i:i + 4, j:j + 60]
n_zeros = np.count_nonzero(data == 1)
final_seg.append((n_zeros/240) * 100)
return final_seg
What's the equivalent with numpy commands?