Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize a 4D numpy array?

I have a three dimensional numpy array of images (CIFAR-10 dataset). The image array shape is like below:

a = np.random.rand(32, 32, 3)

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:

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])

However, when it comes to a 3D array, I am totally lost. Specifically, I have the following questions:

  1. Along which axis do we take the min and max?
  2. How do we implement this with the 3D array?

I appreciate your help!


EDIT: It turns out I need to work with a 4D Numpy array with shape (202, 32, 32, 3), 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!


EDIT 2: Thanks to @Eric's code below, I've figured it out:

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)
like image 562
George Liu Avatar asked Feb 25 '17 18:02

George Liu


People also ask

How do I normalize data in NumPy array?

In order to normalize a vector in NumPy, we can use the np. linalg. norm() function, which returns the vector's norm value. We can then use the norm value to divide each value in the array to get the normalized array.


1 Answers

Assuming you're working with image data of shape (W, H, 3), you should probably normalize over each channel (axis=2) separately, as mentioned in the other answer.

You can do this with:

# keepdims makes the result shape (1, 1, 3) instead of (3,). This doesn't matter here, but
# would matter if you wanted to normalize over a different axis.
v_min = v.min(axis=(0, 1), keepdims=True)
v_max = v.max(axis=(0, 1), keepdims=True)
(v - v_min)/(v_max - v_min)
like image 101
Eric Avatar answered Sep 18 '22 08:09

Eric