Convert byte array back to numpy array [duplicate]

A couple of issues with what you’re doing:

  1. frombuffer will always interpret the input as a 1-dimensional array. It’s the first line of the documentation. So you’d have to reshape to be (28, 28).

  2. The default dtype is float. So if you didn’t serialize out floats, then you’ll have to specify the dtype manually (a priori no one can tell what a stream of bytes means: you have to say what they represent).

  3. If you want to make sure the arrays are equal, you have to use np.array_equal. Using == will do an elementwise operation, and return a numpy array of bools (this presumably isn’t what you want).

How do decode it back from this bytes array to numpy array?

Example:

In [3]: i = np.arange(28*28).reshape(28, 28)

In [4]: k = i.tobytes()

In [5]: y = np.frombuffer(k, dtype=i.dtype)

In [6]: y.shape
Out[6]: (784,)

In [7]: np.array_equal(y.reshape(28, 28), i)
Out[7]: True

HTH.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)