Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a numpy.ndarray to string(or bytes) and convert it back to numpy.ndarray

I'm having a little trouble here,

I'm trying to convert a numpy.ndarray to string, I've already done that like this:

randomArray.tostring() 

It works, but I'm wondering if I can transform it back to a numpy.ndarray.

What's the best way to do this?

I'm using numpy 1.8.1

Context: The objective is to send the numpy.ndarray as a message in rabbitmq (pika library)

like image 308
Ampo Avatar asked May 11 '15 12:05

Ampo


People also ask

How do you convert Ndarray to Ndarray?

You can convert a list to a NumPy array by passing a list to numpy. array() . The data type dtype of generated numpy. ndarray is automatically determined from the original list but can also be specified with the dtype parameter.

How do I change the type of Ndarray?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array. We can check the type of numpy array using the dtype class.

Is Ndarray same as NumPy array?

The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array. It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point values.


2 Answers

You can use the fromstring() method for this:

arr = np.array([1, 2, 3, 4, 5, 6]) ts = arr.tostring() print(np.fromstring(ts, dtype=int))  >>> [1 2 3 4 5 6] 

Sorry for the short answer, not enough points for commenting. Remember to state the data types or you'll end up in a world of pain.

Note on fromstring from numpy 1.14 onwards:

sep : str, optional

The string separating numbers in the data; extra whitespace between elements is also ignored.

Deprecated since version 1.14: Passing sep='', the default, is deprecated since it will trigger the deprecated binary mode of this function. This mode interprets string as binary bytes, rather than ASCII text with decimal numbers, an operation which is better spelt frombuffer(string, dtype, count). If string contains unicode text, the binary mode of fromstring will first encode it into bytes using either utf-8 (python 3) or the default encoding (python 2), neither of which produce sane results.

like image 175
ajsp Avatar answered Sep 28 '22 03:09

ajsp


If you use tostring you lose information on both shape and data type:

>>> import numpy as np >>> a = np.arange(12).reshape(3, 4) >>> a array([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11]]) >>> s = a.tostring() >>> aa = np.fromstring(a) >>> aa array([  0.00000000e+000,   4.94065646e-324,   9.88131292e-324,          1.48219694e-323,   1.97626258e-323,   2.47032823e-323,          2.96439388e-323,   3.45845952e-323,   3.95252517e-323,          4.44659081e-323,   4.94065646e-323,   5.43472210e-323]) >>> aa = np.fromstring(a, dtype=int) >>> aa array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]) >>> aa = np.fromstring(a, dtype=int).reshape(3, 4) >>> aa array([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11]]) 

This means you have to send the metadata along with the data to the recipient. To exchange auto-consistent objects, try cPickle:

>>> import cPickle >>> s = cPickle.dumps(a) >>> cPickle.loads(s) array([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11]]) 
like image 26
simleo Avatar answered Sep 28 '22 03:09

simleo