Good day,
I've searched for this but haven't come up with any responses. I wish to send a multi dimensional numpy array over a socket. Hence, I decided to convert it to a string:
However, it destroys the representation of the array:
>>> import numpy as np
>>> x = np.array([[0, 1], [2, 3]])
>>> xstring = x.tostring()
>>> print xstring
>>> print x
[[0 1]
[2 3]]
>>> print xstring
>>> nparr = np.fromstring(xstring, dtype=np.uint8)
>>> print nparr
[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]
Is there anyway I can get the conversion to string to somehow, save the dimension of it?
dtypeis notnp.uint8- this loads the buffer as bytes (and it is serialized fromint64). You need to use the correctdtypeinfromstring, and then.reshape()the result to the original shape - in your case(2, 2). If you're sending it e.g. over a socket, the shape needs to be provided separately.