Bubble Bubble Bubble Gut
Bubble Bubble Bubble Gut

Reputation: 3358

changing the dtype of numpy array affects the data

While I'm playing around with numpy,

x = np.arange(9).reshape((3,3))
x

it returns array([[0, 1, 2],[3, 4, 5],[6, 7, 8]])

but once I change the datatype by

x.dtype=np.int8
x

now x becomes array([[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0,0, 0],[3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0,0, 0],[6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0,0, 0]], dtype=int8), and x.shape returns (3, 24).

Why is this happening?

Thanks in advance!

Upvotes: 2

Views: 1152

Answers (2)

Abhishek Raj
Abhishek Raj

Reputation: 488

dtype.gist link

I have played your code and observed that when you change the data-type of numpy array then according dtype that the array elements are in now and what were earlier affect the number of elements right now in the array. Let discuss more
In my case earlier dtype of numpy array was int32 and I changed it into

dtype = np.int8  

so 32 / 8 = 4, each elements are now appended by 3 addition zero, and I think in your case earlier numpy array dtype was

dtype = np.int64  

that why each elements is appended by 7 zeros.

so now element of array is 8 bit but in memory it was allocated 32 bit(in my case) and 64 bit in your case. That's why rest memory is allocated now zero.

One more thing, Now if you change the x[0,0] to such value, 8 bit can't hold it will show something else than your value. So this is all about the memory allocation.

I hope this will help you

Upvotes: 0

JohanL
JohanL

Reputation: 6891

When you change the dtype the way you do it, you do not change the data, but the interpretation of it. That means that for each int64 (your standard int type) you get 8 int8 values. And with the LSB order of bytes in memory, the lowest 8 bits (i.e., the lowest byte) are first, giving you your number (as long as it is smaller than 256) followed by 7 zeros.

What you probably want to do, is to convert the data, and generate a new memory layout, generating a new array from the old one, this can be done e.g. by:

x=np.array(x, dtype=np.int8)

or simpler:

x.astype(np.int8)

Upvotes: 6

Related Questions