使用结构化数组命名 numpy 数组中的轴
Use structured array to name axis in numpy array
我一定是犯了一些非常微不足道的错误,但我正在尝试创建一个结构化数组,其中包含一个轴的名称,例如,我有一个数组 data
,形状为 (2, 3, 4)
,我想命名第一个轴,以便我可以访问 data['a']
和 data['b']
在这两种情况下都得到 (3, 4)
形状的切片。我试过了:
shape = (2, 3, 4)
data = np.arange(np.product(shape)).reshape(shape)
dtype = [(nn, float) for nn in ['a', 'b']]
data = np.array(data, dtype=dtype)
但这似乎将所有数据都复制到 'a' 和 'b' 中,例如
print(data.shape)
print(data['a'].shape)
> (2, 3, 4)
> (2, 3, 4)
我尝试指定形状(在 dtype 规范中)应该是 (3, 4)
但是重复了数据 12 次...我尝试将轴顺序更改为 (3, 4, 2)
,但是那什么都不做。任何帮助表示赞赏!
In [263]: shape = (2, 3, 4)
...: data = np.arange(np.product(shape)).reshape(shape)
...:
...: dtype = [(nn, float) for nn in ['a', 'b']]
虽然可以转换 data
,但更可靠的方法是制作所需的目标数组,并将值复制到其中:
In [264]: res = np.zeros(shape[1:], dtype)
In [265]: res['a'] = data[0]
In [266]: res['b'] = data[1]
In [267]: res
Out[267]:
array([[( 0., 12.), ( 1., 13.), ( 2., 14.), ( 3., 15.)],
[( 4., 16.), ( 5., 17.), ( 6., 18.), ( 7., 19.)],
[( 8., 20.), ( 9., 21.), (10., 22.), (11., 23.)]],
dtype=[('a', '<f8'), ('b', '<f8')])
In [268]: res['a'].shape
Out[268]: (3, 4)
在此结构化数组中,一条记录由 2 个浮点数和数据缓冲区组成,包含:
In [272]: res.view(float).ravel()
Out[272]:
array([ 0., 12., 1., 13., 2., 14., 3., 15., 4., 16., 5., 17., 6.,
18., 7., 19., 8., 20., 9., 21., 10., 22., 11., 23.])
这与data
、[0,1,2,3,...]
不同。因此,没有任何类型的重塑、视图或 astype 可以将一个转换为另一个。
因此存在从结构化数组到 (3,4,2) 数组的简单映射,但不是您的来源。
In [273]: res.view(float).reshape(3,4,2)
Out[273]:
array([[[ 0., 12.],
[ 1., 13.],
[ 2., 14.],
[ 3., 15.]],
[[ 4., 16.],
[ 5., 17.],
[ 6., 18.],
[ 7., 19.]],
[[ 8., 20.],
[ 9., 21.],
[10., 22.],
[11., 23.]]])
我一定是犯了一些非常微不足道的错误,但我正在尝试创建一个结构化数组,其中包含一个轴的名称,例如,我有一个数组 data
,形状为 (2, 3, 4)
,我想命名第一个轴,以便我可以访问 data['a']
和 data['b']
在这两种情况下都得到 (3, 4)
形状的切片。我试过了:
shape = (2, 3, 4)
data = np.arange(np.product(shape)).reshape(shape)
dtype = [(nn, float) for nn in ['a', 'b']]
data = np.array(data, dtype=dtype)
但这似乎将所有数据都复制到 'a' 和 'b' 中,例如
print(data.shape)
print(data['a'].shape)
> (2, 3, 4)
> (2, 3, 4)
我尝试指定形状(在 dtype 规范中)应该是 (3, 4)
但是重复了数据 12 次...我尝试将轴顺序更改为 (3, 4, 2)
,但是那什么都不做。任何帮助表示赞赏!
In [263]: shape = (2, 3, 4)
...: data = np.arange(np.product(shape)).reshape(shape)
...:
...: dtype = [(nn, float) for nn in ['a', 'b']]
虽然可以转换 data
,但更可靠的方法是制作所需的目标数组,并将值复制到其中:
In [264]: res = np.zeros(shape[1:], dtype)
In [265]: res['a'] = data[0]
In [266]: res['b'] = data[1]
In [267]: res
Out[267]:
array([[( 0., 12.), ( 1., 13.), ( 2., 14.), ( 3., 15.)],
[( 4., 16.), ( 5., 17.), ( 6., 18.), ( 7., 19.)],
[( 8., 20.), ( 9., 21.), (10., 22.), (11., 23.)]],
dtype=[('a', '<f8'), ('b', '<f8')])
In [268]: res['a'].shape
Out[268]: (3, 4)
在此结构化数组中,一条记录由 2 个浮点数和数据缓冲区组成,包含:
In [272]: res.view(float).ravel()
Out[272]:
array([ 0., 12., 1., 13., 2., 14., 3., 15., 4., 16., 5., 17., 6.,
18., 7., 19., 8., 20., 9., 21., 10., 22., 11., 23.])
这与data
、[0,1,2,3,...]
不同。因此,没有任何类型的重塑、视图或 astype 可以将一个转换为另一个。
因此存在从结构化数组到 (3,4,2) 数组的简单映射,但不是您的来源。
In [273]: res.view(float).reshape(3,4,2)
Out[273]:
array([[[ 0., 12.],
[ 1., 13.],
[ 2., 14.],
[ 3., 15.]],
[[ 4., 16.],
[ 5., 17.],
[ 6., 18.],
[ 7., 19.]],
[[ 8., 20.],
[ 9., 21.],
[10., 22.],
[11., 23.]]])