Python Forum
Numpy saving and loading introduces zeros in the middle of every element
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy saving and loading introduces zeros in the middle of every element
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getting trailing zeros with 1 during pandas read fullstop 1 1,050 Jan-05-2020, 04:01 PM
Last Post: ichabod801
  memory issue loading movie to numpy array djf123 1 488 Nov-07-2019, 06:19 AM
Last Post: ThomasL
  Removing hyphens and adding zeros sidney 3 919 Aug-14-2019, 01:48 PM
Last Post: sidney
  numpy.copy / numpy.delete paul18fr 3 914 Jul-26-2019, 01:51 PM
Last Post: paul18fr
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 935 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  Problem with saving data and loading data to mysql kirito85 4 1,187 Feb-08-2019, 10:53 AM
Last Post: kirito85

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020