NumPy-frombuffer和fromstring有什么区别? [英] NumPy - What is the difference between frombuffer and fromstring?
问题描述
他们似乎给我同样的结果:
They appear to give the same result to me:
In [32]: s
Out[32]: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
In [27]: np.frombuffer(s, dtype="int8")
Out[27]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
In [28]: np.fromstring(s, dtype="int8")
Out[28]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
In [33]: b = buffer(s)
In [34]: b
Out[34]: <read-only buffer for 0x035F8020, size -1, offset 0 at 0x036F13A0>
In [35]: np.fromstring(b, dtype="int8")
Out[35]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
In [36]: np.frombuffer(b, dtype="int8")
Out[36]:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
什么时候应该使用另一个?
When should one be used vs. the other?
推荐答案
从实际的角度来看,区别在于:
From a practical standpoint, the difference is that:
x = np.fromstring(s, dtype='int8')
将在内存中复制字符串,同时:
Will make a copy of the string in memory, while:
x = np.frombuffer(s, dtype='int8')
或
x = np.frombuffer(buffer(s), dtype='int8')
将直接使用字符串的内存缓冲区,并且不会使用任何*额外的内存.如果buffer
的输入是字符串,则使用frombuffer
还将导致只读数组,因为字符串在python中是不可变的.
Will use the memory buffer of the string directly and won't use any* additional memory. Using frombuffer
will also result in a read-only array if the input to buffer
is a string, as strings are immutable in python.
(* *忽略了用于其他python ndarray
对象的几个字节的内存-数据的基础内存将被共享.)
(*Neglecting a few bytes of memory used for an additional python ndarray
object -- The underlying memory for the data will be shared.)
如果您不熟悉 buffer
对象(python3.x中的memoryview
),从本质上讲,它们是C级库公开用于python的内存块的一种方式.基本上,这是一个用于对原始内存进行托管访问的python接口.
If you're not familiar with buffer
objects (memoryview
in python3.x), they're essentially a way for C-level libraries to expose a block of memory for use in python. It's basically a python interface for managed access to raw memory.
如果您正在使用暴露缓冲区接口的工具,那么您可能要使用frombuffer
. (Python 2.x字符串和python 3.x bytes
公开了缓冲区接口,但是您将获得一个只读数组,因为python字符串是不可变的.)
If you were working with something that exposed the buffer interface, then you'd probably want to use frombuffer
. (Python 2.x strings and python 3.x bytes
expose the buffer interface, but you'll get a read-only array, as python strings are immutable.)
否则,请使用fromstring
从字符串创建一个numpy数组. (除非您知道自己在做什么,并且想严格控制内存使用情况,等等.)
Otherwise, use fromstring
to create a numpy array from a string. (Unless you know what you're doing, and want to tightly control memory use, etc.)
这篇关于NumPy-frombuffer和fromstring有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!