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
  [Numpy] How to store different data type in one numpy array? water 7 550 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,605 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  getting trailing zeros with 1 during pandas read fullstop 1 3,602 Jan-05-2020, 04:01 PM
Last Post: ichabod801
  How to use Scipy.special to retrieve complex Poles / Zeros of Bessel Polynominal Detzi 2 1,789 Dec-31-2019, 10:45 AM
Last Post: Detzi
  memory issue loading movie to numpy array djf123 1 2,293 Nov-07-2019, 06:19 AM
Last Post: ThomasL
  Removing hyphens and adding zeros sidney 3 2,945 Aug-14-2019, 01:48 PM
Last Post: sidney
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,979 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  Problem with saving data and loading data to mysql kirito85 4 3,913 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