Abhishek Gangwar
Abhishek Gangwar

Reputation: 1767

Subtraction on numpy array not working

I have a 4D numpy array. I am trying to normalize it's value for that I need to subtract some value from it but the operation is adding the values.

Please help

print(X_train.shape)
print(X_train[0][0][0])
print(X_train[0][0][0]-128)

It's output is:

(34799, 32, 32, 3)

[28 25 24]

[156 153 152]

Shouldn't it be?

[-100,-103,-102]

Please let me know what I am doing wrong. I am new to numpy.

Upvotes: 1

Views: 3049

Answers (1)

Yaniv
Yaniv

Reputation: 839

The fact that it's a 4-dimensional array is not the point here.

I guess that your problem is with the data type of that numpy array. For example, if it's numpy.uint8 (unsigned byte, i.e. allowing only values in [0,255]) then subtracting 128 from 28 will give you 156... :)

Try: print (X_train.dtype) to see the data type associated with your numpy array.

If that's the case, then consider converting it to some other dtype, e.g. X_train = X_train.astype(numpy.int16), or simply to numpy.int8, depending on your expectations from your data.

Upvotes: 4

Related Questions