title: How to normalize a 4D numpy array?
body: <p>I have a three dimensional numpy array of images (<a href="https://www.cs.toronto.edu/~kriz/cifar.html" rel="noreferrer">CIFAR-10 dataset</a>). The image array shape is like below:</p>
  
  <pre><code>a = np.random.rand(32, 32, 3)
  </code></pre>
  
  <p>Before I do any deep learning, I want to normalize the data to get better result. With a 1D array, I know we can do min max normalization like this:</p>
  
  <pre><code>v = np.random.rand(6)
  (v - v.min())/(v.max() - v.min())
  
  Out[68]:
  array([ 0.89502294,  0.        ,  1.        ,  0.65069468,  0.63657915,
          0.08932196])
  </code></pre>
  
  <p>However, when it comes to a 3D array, I am totally lost. Specifically, I have the following questions:</p>
  
  <ol>
  <li>Along which axis do we take the min and max?</li>
  <li>How do we implement this with the 3D array?</li>
  </ol>
  
  <p>I appreciate your help!</p>
  
  <hr>
  
  <p>EDIT:
  It turns out I need to work with a 4D Numpy array with shape <code>(202, 32, 32, 3)</code>, so the first dimension would be the index for the image, and the last 3 dimensions are the actual image. It'll be great if someone can provide me with the code to normalize such a 4D array. Thanks!</p>
  
  <hr>
  
  <p>EDIT 2:
  Thanks to @Eric's code below, I've figured it out:</p>
  
  <pre><code>x_min = x.min(axis=(1, 2), keepdims=True)
  x_max = x.max(axis=(1, 2), keepdims=True)
  
  x = (x - x_min)/(x_max-x_min)
  </code></pre>