Python 将 numpy.ndarray 转换为字符串(或字节)并将其转换回 numpy.ndarray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30167538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a numpy.ndarray to string(or bytes) and convert it back to numpy.ndarray
提问by Ampo
I'm having a little trouble here,
我这里有点麻烦
I'm trying to convert a numpy.ndarray to string, I've already done that like this:
我正在尝试将 numpy.ndarray 转换为字符串,我已经这样做了:
randomArray.tostring()
It works, but I'm wondering if I can transform it back to a numpy.ndarray.
它有效,但我想知道是否可以将其转换回 numpy.ndarray。
What's the best way to do this?
做到这一点的最佳方法是什么?
I'm using numpy 1.8.1
我正在使用 numpy 1.8.1
Context: The objective is to send the numpy.ndarray as a message in rabbitmq (pika library)
上下文:目标是在rabbitmq(pika库)中将numpy.ndarray作为消息发送
采纳答案by ajsp
You can use the fromstring() method for this:
您可以为此使用 fromstring() 方法:
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:
从 numpy 1.14 开始,关于 `fromstring' 的注意事项:
sep : str, optional
sep : str, 可选
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.
1.14 版后已弃用:不推荐使用默认值 sep='',因为它将触发此函数的已弃用二进制模式。这种模式将字符串解释为二进制字节,而不是带有十进制数的 ASCII 文本,这种操作更好地拼写为 frombuffer(string, dtype, count)。如果 string 包含 unicode 文本,则 fromstring 的二进制模式将首先使用 utf-8 (python 3) 或默认编码 (python 2) 将其编码为字节,这两种编码都不会产生合理的结果。
回答by Julien Spronck
Imagine you have a numpy array of integers (it works with other types but you need some slight modification). You can do this:
想象一下,您有一个 numpy 整数数组(它适用于其他类型,但您需要稍作修改)。你可以这样做:
a = np.array([0, 3, 5])
a_str = ','.join(str(x) for x in a) # '0,3,5'
a2 = np.array([int(x) for x in a_str.split(',')]) # np.array([0, 3, 5])
If you have an array of float, be sure to replace int
by float
in the last line.
如果您有一个浮点数组,请务必在最后一行替换int
为float
。
You can also use the __repr__()
method, which will have the advantage to work for multi-dimensional arrays:
您还可以使用该__repr__()
方法,该方法适用于多维数组:
from numpy import array
numpy.set_printoptions(threshold=numpy.nan)
a = array([[0,3,5],[2,3,4]])
a_str = a.__repr__() # 'array([[0, 3, 5],\n [2, 3, 4]])'
a2 = eval(a_str) # array([[0, 3, 5],
# [2, 3, 4]])
回答by simleo
If you use tostring
you lose information on both shape and data type:
如果使用,tostring
则会丢失有关形状和数据类型的信息:
>>> 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:
这意味着您必须将元数据与数据一起发送给收件人。要交换自动一致的对象,请尝试 cPickle:
>>> import cPickle
>>> s = cPickle.dumps(a)
>>> cPickle.loads(s)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
回答by Max Kleiner
Imagine you have a numpy array of text like in a messenger
想象一下,你有一个像信使一样的 numpy 文本数组
>>> stex[40]
array(['Know the famous thing ...
and you want to get statistics from the corpus (text col=11) you first must get the values from dataframe (df5) and then join all records together in one single corpus:
并且您想从语料库中获取统计信息(文本 col=11),您首先必须从数据帧(df5)中获取值,然后将所有记录连接到一个语料库中:
>>> stex = (df5.ix[0:,[11]]).values
>>> a_str = ','.join(str(x) for x in stex)
>>> a_str = a_str.split()
>>> fd2 = nltk.FreqDist(a_str)
>>> fd2.most_common(50)
回答by Sudheer Raja
This is a slightly improvised answer to ajspanswer using XML-RPC.
这是对使用 XML-RPC 的ajsp回答的稍微即兴的回答。
On the server-side when you convert the data, convert the numpy data to a string using the '.tostring()'method. This encodes the numpy ndarray as bytes string. On the client-side when you receive the data decode it using '.fromstring()'method. I wrote two simple functions for this. Hope this is helpful.
在服务器端转换数据时,使用'.tostring()'方法将 numpy 数据转换为字符串 。这将 numpy ndarray 编码为字节字符串。在客户端,当您收到数据时,使用'.fromstring()'方法对其进行解码。我为此编写了两个简单的函数。希望这是有帮助的。
- ndarray2str -- Converts numpy ndarray to bytes string.
- str2ndarray -- Converts binary str back to numpy ndarray.
- ndarray2str -- 将 numpy ndarray 转换为字节字符串。
- str2ndarray -- 将二进制 str 转换回 numpy ndarray。
def ndarray2str(a):
# Convert the numpy array to string
a = a.tostring()
return a
On the receiver side, the data is received as a 'xmlrpc.client.Binary'object. You need to access the data using '.data'.
在接收方,数据作为“xmlrpc.client.Binary”对象接收。您需要使用“ .data”访问数据。
def str2ndarray(a):
# Specify your data type, mine is numpy float64 type, so I am specifying it as np.float64
a = np.fromstring(a.data, dtype=np.float64)
a = np.reshape(a, new_shape)
return a
Note:Only problem with this approach is that XML-RPC is very slow while sending large numpy arrays. It took me around 4 secs to send and receive a (10, 500, 500, 3) size numpy array for me.
注意:这种方法的唯一问题是 XML-RPC 在发送大型 numpy 数组时非常慢。我花了大约 4 秒来为我发送和接收 (10, 500, 500, 3) 大小的 numpy 数组。
I am using python 3.7.4.
我正在使用 python 3.7.4。
回答by Jadiel de Armas
This is a fast way to encode the array, the array shape and the array dtype:
这是对数组、数组形状和数组 dtype 进行编码的快速方法:
def numpy_to_bytes(arr: np.array) -> str:
arr_dtype = bytearray(str(arr.dtype), 'utf-8')
arr_shape = bytearray(','.join([str(a) for a in arr.shape]), 'utf-8')
sep = bytearray('|', 'utf-8')
arr_bytes = arr.ravel().tobytes()
to_return = arr_dtype + sep + arr_shape + sep + arr_bytes
return to_return
def bytes_to_numpy(serialized_arr: str) -> np.array:
sep = '|'.encode('utf-8')
i_0 = serialized_arr.find(sep)
i_1 = serialized_arr.find(sep, i_0 + 1)
arr_dtype = serialized_arr[:i_0].decode('utf-8')
arr_shape = tuple([int(a) for a in serialized_arr[i_0 + 1:i_1].decode('utf-8').split(',')])
arr_str = serialized_arr[i_1 + 1:]
arr = np.frombuffer(arr_str, dtype = arr_dtype).reshape(arr_shape)
return arr
To use the functions:
要使用这些功能:
a = np.ones((23, 23), dtype = 'int')
a_b = numpy_to_bytes(a)
a1 = bytes_to_numpy(a_b)
np.array_equal(a, a1) and a.shape == a1.shape and a.dtype == a1.dtype
回答by aman5319
I know, I am late but here is the correct way of doing it. using base64. This technique will convert the array to string.
我知道,我迟到了,但这是正确的做法。使用base64。这种技术会将数组转换为字符串。
import base64
import numpy as np
random_array = np.random.randn(32,32)
string_repr = base64.binascii.b2a_base64(random_array).decode("ascii")
array = np.frombuffer(base64.binascii.a2b_base64(string_repr.encode("ascii")))
For array to string
对于数组到字符串
Convert binary data to a line of ASCII characters in base64 coding and decode to ASCII to get string repr.
将二进制数据转换为base64编码的一行ASCII字符,解码为ASCII得到字符串repr。
For string to array
用于字符串到数组
First, encode the string in ASCII format then Convert a block of base64 data back to binary and return the binary data.
首先,以 ASCII 格式对字符串进行编码,然后将一块 base64 数据转换回二进制并返回二进制数据。