[英]numpy structured array inconsistency
我正在編寫一個使用 NumPy arrays 的庫,並且我想在任何dtype 上執行標量操作。 這適用於大多數結構化 arrays,但是在為結構化元素創建具有多個維度的結構化 arrays 時遇到問題。 舉個例子,
x = np.zeros(10, np.dtype('3float32,int8'))
print(x.dtype)
print(x.shape)
節目
[('f0', '<f4', (3,)), ('f1', 'i1')]
(10,)
但
x = np.zeros(10, np.dtype('3float32'))
print(x.dtype)
print(x.shape)
產量
float32
(10, 3)
也就是說,創建具有單個多維字段的結構化數組似乎反而擴展了數組形狀。 這意味着最后一個示例的維數是 2,而不是我預期的 1。 我在這里有什么遺漏或已知的解決方法嗎?
使用與第一個工作示例中顯示的相同的dtype
表示法:
In [92]: x = np.zeros(3, np.dtype([('f0','<f4',(3,))]))
In [93]: x
Out[93]:
array([([0., 0., 0.],), ([0., 0., 0.],), ([0., 0., 0.],)],
dtype=[('f0', '<f4', (3,))])
我通常不使用字符串速記,
In [99]: np.dtype('3float32')
Out[99]: dtype(('<f4', (3,))) # no field name assigned
In [100]: np.zeros(3,_)
Out[100]:
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]], dtype=float32)
幾個逗號分隔的字符串創建命名字段:
In [102]: np.dtype('3float32,i4')
Out[102]: dtype([('f0', '<f4', (3,)), ('f1', '<i4')])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.