将numpy recarray复制到ndarray [英] copy numpy recarray to ndarray

查看:80
本文介绍了将numpy recarray复制到ndarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过程,要求将数据从numpy recarray提取到ndarray,然后在其中进行一些矢量数学运算. (recarray来自pytables table.read()函数.)我想将数学输出(另一个ndarray)映射回原始Recarray中的相同字段/列.我找到了一种逐列进行的方法.寻找一种更好的方式来回处理数据.我的代码:

I have a process that requires extracting data from a numpy recarray to a ndarray where I then do some vector math. (The recarray comes from a pytables table.read() function.) I want to map the math output (another ndarray) back to the same fields/columns in the original recarray. I found a way to do it column by column. Looking for a better way to go back and forth with the data. my code:

node_eigen_array = eigenvb_table.read_coordinates(node_rows)
node_eigen_array.shape[0]
10
node_eigen_array.dtype
dtype([('ID', '<i8'), ('X', '<f8'), ('Y', '<f8'), ('Z', '<f8'),  ('FREQ', '<i8')])
resvec_array[:,0]=node_eigen_array['X']
resvec_array[:,1]=node_eigen_array['Y']
resvec_array[:,2]=node_eigen_array['Z']

# do some stuff that returns ndarray c_dot...

node_eigen_array['X']=cdot[:,0]
node_eigen_array['Y']=cdot[:,1]
node_eigen_array['Z']=cdot[:,2]

我尝试过将第一个Recarray跳过到ndarray:

I tried this to skip the first recarray to ndarray:

resvec_array=node_eigen_array[['X','Y','Z']].view('float64').reshape((10,3))

numpy抱怨:

This code may break in numpy 1.13 because this will return a view instead of a copy -- see release notes for details.

此外,正在寻找简化ndarray数据返回recarray的方法. 谢谢.

Also, looking for something that simplifies ndarray data back to recarray. Thanks.

推荐答案

这是将来的警告,不是错误.更改已推迟到1.16.它与您的[['X','Y','Z']]步骤的多字段索引有关.

This is a future warning, not an error. The change has been postponed to 1.16. It has to do with multifield indexing, your [['X','Y','Z']] step.

In [56]: dt = np.dtype([('ID', '<i8'), ('X', '<f8'), ('Y', '<f8'), ('Z', '<f8'), ('FREQ', '<i8')])
In [57]: arr = np.ones(3, dtype=dt)
In [58]: arr       # a structured array, recarray is just variation
Out[58]: 
array([(1, 1., 1., 1., 1), (1, 1., 1., 1., 1), (1, 1., 1., 1., 1)],
      dtype=[('ID', '<i8'), ('X', '<f8'), ('Y', '<f8'), ('Z', '<f8'), ('FREQ', '<i8')])

仅查看字段时安静:

In [59]: arr[['X','Y','Z']]
Out[59]: 
array([(1., 1., 1.), (1., 1., 1.), (1., 1., 1.)],
      dtype=[('X', '<f8'), ('Y', '<f8'), ('Z', '<f8')])

但是当您尝试对它们进行操作时,它会警告您有所更改.请注意,它仍然会执行操作.

But it warns of a change when you try to do something with them. Note it still does the action.

In [60]: arr[['X','Y','Z']].view('float64')
/usr/local/bin/ipython3:1: FutureWarning: Numpy has detected that you may be viewing or writing to an array returned by selecting multiple fields in a structured array. 

This code may break in numpy 1.16 because this will return a view instead of a copy -- see release notes for details.
  #!/usr/bin/python3
Out[60]: array([1., 1., 1., 1., 1., 1., 1., 1., 1.])

使警告静音的一种方法是在索引之后添加copy():

A way to silence the warning is to add copy() after the indexing:

In [62]: arr[['X','Y','Z']].copy().view('float64')
Out[62]: array([1., 1., 1., 1., 1., 1., 1., 1., 1.])

当前,此view更改有效.但是在计划的更改中,arr[['X','Y','Z']]的数据布局将有所不同,并且view将不起作用.关于补偿有一些复杂的事情.

Currently this view change works. But in the planned change, the arr[['X','Y','Z']] data layout will be different, and the view won't work. There's some complex business about offsets.

这篇关于将numpy recarray复制到ndarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆