Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多维数组中的结构化numpy数组_Python_Arrays_Numpy_Multidimensional Array_Structured Array - Fatal编程技术网

Python 多维数组中的结构化numpy数组

Python 多维数组中的结构化numpy数组,python,arrays,numpy,multidimensional-array,structured-array,Python,Arrays,Numpy,Multidimensional Array,Structured Array,想象一个numpy数组的nxm维度。在每个单元格中,它包含一个带有X元素的结构化数组,每个元素都包含一个X_标签 我想访问一个特定的x_标签,因此它返回一个nxm数组,该数组只包含感兴趣的标签的值 有没有一种方法不必使用for循环(或np.map())函数并创建一个新数组就可以这样做 例如: import numpy as np arr = np.array([[[],[]], [[],[]]]) # Each cell contains: np.array([

想象一个
numpy
数组的
nxm
维度。在每个单元格中,它包含一个带有
X
元素的结构化数组,每个元素都包含一个
X_标签

我想访问一个特定的
x_标签
,因此它返回一个
nxm
数组,该数组只包含感兴趣的标签的值

有没有一种方法不必使用
for
循环(或
np.map()
)函数并创建一个新数组就可以这样做

例如:

import numpy as np
arr = np.array([[[],[]],
                [[],[]]])

# Each cell contains:
np.array([('par1', 'par2', 'par3')], dtype=[('label_1', 'U10'), ('label_2', 'U10'), ('label3', 'U10')])
如何获取仅返回值为
par1
的2x2
np.array
? 我尝试过,但没有成功:

arr['label_1']
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

谢谢大家!

我假设您的外部数组是
Object
dtype,否则应该没有问题:

>>> x = np.array([('par1', 'par2', 'par3')], dtype=[('label_1', 'U10'), ('label_2', 'U10'), ('label3', 'U10')])
>>> Y = np.array(4*[x]+[None])[:-1].reshape(2,2)
>>> Y
array([[array([('par1', 'par2', 'par3')],
      dtype=[('label_1', '<U10'), ('label_2', '<U10'), ('label3', '<U10')]),
        array([('par1', 'par2', 'par3')],
      dtype=[('label_1', '<U10'), ('label_2', '<U10'), ('label3', '<U10')])],
       [array([('par1', 'par2', 'par3')],
      dtype=[('label_1', '<U10'), ('label_2', '<U10'), ('label3', '<U10')]),
        array([('par1', 'par2', 'par3')],
      dtype=[('label_1', '<U10'), ('label_2', '<U10'), ('label3', '<U10')])]],
      dtype=object)
>>x=np.array([('par1','par2','par3')],dtype=[('label_1','U10'),('label_2','U10'),('label3','U10'))
>>>Y=np.数组(4*[x]+[None])[:-1].重塑(2,2)
>>>Y
数组([[array([('par1','par2','par3'))],

dtype=[('label_1','What dtype是您的外部数组?谢谢@paul!我不是自己创建数组的,这是Python包返回函数的方式…我将把数组转换为适当的结构化数组:-)关于这一点的另一个问题:想象数组中的一个值(称为'parX'的参数)是空的。然后,
np.concatenate()
将更改初始数组的大小,
np.reformate()
将给出错误
value错误:无法将大小为##############
的数组重新格式化。@paul paul(arr.shape)
可能会起作用。不幸的是,它不起作用。它会给出相同的
ValueError
…嗯,对不起,这太复杂了,无法用注释来计算。如果字段为空,您可以将其完全删除(在
np.lib.recfunctions
中有一个函数,请再试一次。在任何情况下,我建议您用一个示例提出一个新问题,说明您的问题。
>>> Z = np.concatenate(Y.ravel()).reshape(Y.shape)
>>> Z
array([[('par1', 'par2', 'par3'), ('par1', 'par2', 'par3')],
       [('par1', 'par2', 'par3'), ('par1', 'par2', 'par3')]],
      dtype=[('label_1', '<U10'), ('label_2', '<U10'), ('label3', '<U10')])
>>> Z['label_1']
array([['par1', 'par1'],
       ['par1', 'par1']], dtype='<U10')