怎么把numpy.recarray转换成numpy.array? [英] How to convert numpy.recarray to numpy.array?
问题描述
将numpy的recarray
转换为普通数组的最佳方法是什么?
What's the best way to convert numpy's recarray
to a normal array?
我可以先做.tolist()
然后再做array()
,但这似乎效率不高.
i could do a .tolist()
first and then do an array()
again, but that seems somewhat inefficient..
示例:
import numpy as np
a = np.recarray((2,), dtype=[('x', int), ('y', float), ('z', int)])
>>> a
rec.array([(30408891, 9.2944097561804909e-296, 30261980),
(44512448, 4.5273310988985789e-300, 29979040)],
dtype=[('x', '<i4'), ('y', '<f8'), ('z', '<i4')])
>>> np.array(a.tolist())
array([[ 3.04088910e+007, 9.29440976e-296, 3.02619800e+007],
[ 4.45124480e+007, 4.52733110e-300, 2.99790400e+007]])
推荐答案
通过普通数组",我认为您的意思是同类dtype的NumPy数组.给定一个rearray,例如:
By "normal array" I take it you mean a NumPy array of homogeneous dtype. Given a recarray, such as:
>>> a = np.array([(0, 1, 2),
(3, 4, 5)],[('x', int), ('y', float), ('z', int)]).view(np.recarray)
rec.array([(0, 1.0, 2), (3, 4.0, 5)],
dtype=[('x', '<i4'), ('y', '<f8'), ('z', '<i4')])
我们必须首先使每个列具有相同的dtype.然后,我们可以通过使用相同的dtype查看数据,将其转换为普通数组":
we must first make each column have the same dtype. We can then convert it to a "normal array" by viewing the data by the same dtype:
>>> a.astype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')]).view('<f8')
array([ 0., 1., 2., 3., 4., 5.])
astype 返回一个新的numpy数组.因此,以上内容需要与a
的大小成比例的额外内存. a
的每一行需要4 + 8 + 4 = 16字节,而a.astype(...)
则需要8 * 3 = 24字节.调用视图不需要新的内存,因为view
只是更改了基础数据的解释方式.
astype returns a new numpy array. So the above requires additional memory in an amount proportional to the size of a
. Each row of a
requires 4+8+4=16 bytes, while a.astype(...)
requires 8*3=24 bytes. Calling view requires no new memory, since view
just changes how the underlying data is interpreted.
a.tolist()
返回一个新的Python列表.每个Python数字都是一个对象,比其在numpy数组中的等效表示形式需要更多的字节.因此a.tolist()
比a.astype(...)
需要更多的内存.
a.tolist()
returns a new Python list. Each Python number is an object which requires more bytes than its equivalent representation in a numpy array. So a.tolist()
requires more memory than a.astype(...)
.
呼叫a.astype(...).view(...)
也比np.array(a.tolist())
快:
In [8]: a = np.array(zip(*[iter(xrange(300))]*3),[('x', int), ('y', float), ('z', int)]).view(np.recarray)
In [9]: %timeit a.astype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')]).view('<f8')
10000 loops, best of 3: 165 us per loop
In [10]: %timeit np.array(a.tolist())
1000 loops, best of 3: 683 us per loop
这篇关于怎么把numpy.recarray转换成numpy.array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!