Python Forum

Full Version: Numpy saving and loading introduces zeros in the middle of every element
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am do a simple numpy saving and loading test with this code:
a = np.array([[[10,10,10], [10, 10, 10]], [[10,10,10], [10,10,10]]])
print(a)

f = io.BytesIO()
np.save(f, a)

co = codecs.encode(f.getvalue(), 'bz2')
cd = codecs.decode(co, 'bz2')

np.save(f, cd)
f.seek(0)

n = np.frombuffer(np.load(f, allow_pickle=True), dtype=np.int32)
print(n)

#print(n.reshape(a.shape))
Of course, 'a' outputs:
Output:
[[[10 10 10] [10 10 10]] [[10 10 10] [10 10 10]]]
However, 'n' outputs:
Output:
[10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0] [10 10 10 10 10 10 10 10 10 10 10 10]
The shape isn't an issue since I can reshape it later. What is the issue is all the zeros that are being introduced between every number.
I am using this line to remove them:
n = np.array(n)[::2]
It works fine, but I have a feeling that in my actually program, its going to accidentally remove a zero its not supposed to, or have some other problem like that.

How do I get these zeros not to come about?