繁体   English   中英

使用 dtype 中的重叠字段构建 np.array

[英]Constructing np.array with overlapping fields in dtype

我有一个 dtype 如下:

pose_dtype = np.dtype([('x', np.float64), ('y', np.float64), ('theta', np.float64)])

现在,我可以写:

pose = np.array((1, 2, np.pi), dtype=pose_dtype)

我想添加一个xy字段以使其更易于使用。 我可以这样做:

pose_dtype = np.dtype(dict(
    names=['x', 'y', 'theta', 'xy'],
    formats=[np.float64, np.float64, np.float64, (np.float64, 2)],
    offsets=[0, 8, 16, 0]
))

但是,现在我不能再使用以前的方法构造数组,而不得不求助于:

pose = np.array((1, 2, np.pi, [1, 2]), dtype=pose_dtype)

这是危险的重复。

有什么方法可以将属性标记为彼此的别名,这样我就不必处理这个问题了吗?

按字段而不是按记录填充数组的实验

In [207]: pose_dtype = np.dtype(dict(
    names=['x', 'y', 'theta', 'xy'],
    formats=[np.float64, np.float64, np.float64, (np.float64, 2)],
    offsets=[0, 8, 16, 0]
))

In [209]: A=np.zeros((3,),dtype=pose_dtype)
In [210]: A
Out[210]: 
array([(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={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})
In [211]: A['x']=[1,2,3]
In [212]: A
Out[212]: 
array([(1.0, 0.0, 0.0, [1.0, 0.0]), (2.0, 0.0, 0.0, [2.0, 0.0]),
       (3.0, 0.0, 0.0, [3.0, 0.0])], 
      dtype={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})
In [213]: A['y']=[4,5,6]
In [214]: A
Out[214]: 
array([(1.0, 4.0, 0.0, [1.0, 4.0]), (2.0, 5.0, 0.0, [2.0, 5.0]),
       (3.0, 6.0, 0.0, [3.0, 6.0])], 
      dtype={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})
In [215]: A['xy']
Out[215]: 
array([[ 1.,  4.],
       [ 2.,  5.],
       [ 3.,  6.]])
In [216]: A['xy']=np.arange(10,16).reshape(3,2)
In [217]: A
Out[217]: 
array([(10.0, 11.0, 0.0, [10.0, 11.0]), (12.0, 13.0, 0.0, [12.0, 13.0]),
       (14.0, 15.0, 0.0, [14.0, 15.0])], 
      dtype={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})

In [219]: A['xy'].dot(A['xy'].T)
Out[219]: 
array([[ 221.,  263.,  305.],
       [ 263.,  313.,  363.],
       [ 305.,  363.,  421.]])

另一种将 2 个字段作为浮点数组获取的方法(不漂亮)

In [228]: A[['x','y']].view(float).reshape(-1,2)
Out[228]: 
array([[ 10.,  11.],
       [ 12.,  13.],
       [ 14.,  15.]])

转换 unix 时间戳的 np.array (dtype ' <u21') to np.datetime64< div><div id="text_translate"><p> 我正在寻找处理大量数据,因此我对计算以下内容的最快方法感兴趣:</p><p> 我将以下 np.array 作为 np.ndarray 的一部分,我想将其从“&lt;U21”转换为“np.datetime64”(毫秒)。</p><p> 当我在一个条目上执行以下代码时,它可以工作:</p><pre> tmp_array[:,0][0].astype(int).astype('datetime64[ms]')</pre><p> 结果:numpy.datetime64('2019-10-09T22:54:00.000')</p><p> 当我像这样在子数组上执行相同的操作时:</p><pre> tmp_array[:,0] = tmp_array[:,0].astype(int).astype('datetime64[ms]')</pre><p> 我总是收到以下错误:</p><pre> RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 21</pre><p> numpy 版本 1.22.4</p><pre> array(['1570661640000', '1570661700000', '1570661760000'],dtype='&lt;U21')</pre></div></u21')>

[英]Converting np.array of unix timestamps (dtype '<U21') to np.datetime64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Numpy np.array,带有dtype TypeError 为 np.array(1.) 指定默认 dtype 将具有元组的dtype对象转换为np.array 从 np.array 中删除 dtype np.array(dtype=&#39;str&#39;) 在 np.array(dtype=&#39;datetime&#39;) 中的转换 没有dtype参数的np.array调用会引发错误吗? 当两者之一是 np.array(dtype=np.complex128).real 时,numpy matmul 非常慢 转换 unix 时间戳的 np.array (dtype ' <u21') to np.datetime64< div><div id="text_translate"><p> 我正在寻找处理大量数据,因此我对计算以下内容的最快方法感兴趣:</p><p> 我将以下 np.array 作为 np.ndarray 的一部分,我想将其从“&lt;U21”转换为“np.datetime64”(毫秒)。</p><p> 当我在一个条目上执行以下代码时,它可以工作:</p><pre> tmp_array[:,0][0].astype(int).astype('datetime64[ms]')</pre><p> 结果:numpy.datetime64('2019-10-09T22:54:00.000')</p><p> 当我像这样在子数组上执行相同的操作时:</p><pre> tmp_array[:,0] = tmp_array[:,0].astype(int).astype('datetime64[ms]')</pre><p> 我总是收到以下错误:</p><pre> RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 21</pre><p> numpy 版本 1.22.4</p><pre> array(['1570661640000', '1570661700000', '1570661760000'],dtype='&lt;U21')</pre></div></u21')> 返回 dtype=object 的 np.array 中非 NaN 唯一值的索引 带有dtype = int32的值列表转换为np.array对象的Dict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM