title: How to calculate mean for batch of images in Python in a non vectorized way?
body: <p>In order to calculate the image mean, one can use <code>numpy.mean()</code> if I'm not mistaken.  </p>
  
  <p>What <code>mean()</code> does, is simply summing all elements and then dividing by the number of all elements summed. furthermore if I want to calculate mean channelwise,(each channel has its own mean calculated), and I have an input of shape: <code>(num_batch, num_channel, width, height)</code>, I'd write :</p>
  
  <pre><code>numpy.mean(a,axis=(0,2,3)) 
  </code></pre>
  
  <p>Now, I tried to code its non vectorized version but I'm not sure I'm doing everything correctly.</p>
  
  <p>This is the code that is presumed to be calculating the mean channel-wise, (its output should match that of the command given above:</p>
  
  <pre><code>def calc_mean_classic(a):
      #sum all elements in each channel and divide by the number of elements
      batch = a.shape[0]
      channel = a.shape[1]
      width = a.shape[2]
      height = a.shape[3]
  
      sum = np.zeros((channel))
      for i in range(batch):
          for c in range(channel):
              for w in range(width):
                  for h in range(height):
                      sum[c] += a[i,c,w,h]
  
      return sum, (sum/(width*height))
  </code></pre>
  
  <p>When I run my data with <code>numpy.mean(a,axis=(0,2,3))</code>, I get :</p>
  
  <pre><code>[ 125.30691805  122.95039414  113.86538318]
  </code></pre>
  
  <p>but when I run the function I coded, I get :</p>
  
  <pre><code>sum,m3 = calc_mean_classic(data_train)
  print 'sum=',sum
  print 'm3=',m3
  print m3[0]/(32*32)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>sum= [  6.41571420e+09   6.29506018e+09   5.82990762e+09]
  m3= [ 6265345.90234375  6147519.70703125  5693269.15917969]
  6118.50185776
  </code></pre>
  
  <p>There is an irony here, image size is <code>32*32</code> that's why I'm doing <code>m3[0]/(32*32)</code> (<code>sum/width*height</code>) to see if I can generate the <code>sum[0]</code> value! As you can see they are completely different values, while they should yield the same value!</p>
  
  <p>I have no idea what's going on here.</p>