Why don't means of color channels average to equal flattened array mean I am experimenting with different types of mean subtraction on RGB images for a convolutional neural network. I tried calculating a single grand mean of all the training images. I also tried calculating a mean per color channel. The grand mean I got was 122.84 which sounds like a reasonable number. However, when I tried calculating the means per color channel I got  [4.86049, 4.86049, 4.86049].
First off, I find it strange that they are all the same number. Secondly, they seem ridiculously low. And thirdly and perhaps most confusingly, shouldn't the mean of the three color channels equal the grand mean?

Maybe it is a problem with my code? This is what I'm using. The images are in a numpy array of the shape (17611, 224, 224, 3)

trainImgsArr = np.random.rand(17611, 224, 224, 3)
grandMean = np.mean(trainImgsArr)
meanByChannel = np.mean(trainImgsArr, axis = (0,1,2))


Any ideas?

EDIT: Ah, ha! I think I have found the true channel means with the code

 ch1, ch2, ch3 = np.split(trainImgsArr, 3, axis = 3)
 np.mean(ch1)
 np.mean(ch2)
 np.mean(ch3)


Sorry if this was super basic I am very new to python. Could someone perhaps explain where I went wrong using a single np.mean call as in the first code bit?
You just understood numpy wrong (God bless those who understood it right). The correct code should have been:

trainImgsArr = np.random.rand(17611, 224, 224, 3)
grandMean = np.mean(trainImgsArr)
meanByChannel = []
for i in range(0, 3):
    avg = np.mean(trainImgsArr[:, :, :, i]
    meanByChannel.append(avg)


Basically I am doing splicing of the array,taking all pixels of a colour channel from all images and averaging them. Whereas you did something else entirely (your results are not reproducible). Axes (0, 1, 2) does not mean the colour channels it only means the index you are referring to and performing the required operation.



Ignore the convolution part but check the subscripts of h_n-1 you can access the those positions by varying the last argument in trainImgsArr.shape.