结构化数组中数组的NumPy数据类型的适当格式

2024-10-02 00:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个numpy结构化数组,但我无法找到正确的方法来格式化数组中数组的列标题/列类型。我一直在获取setting an array element with a sequence消息,但是我可以将列表转换为非结构化数组而没有问题,因此问题在于子数组中数据类型的格式

代码

#Number of People
numOfP=5
#Array of people's ids
ids=np.array(range(0,numOfP),dtype='int64')
#People object
temp=[];
peoType=np.dtype({
    'names':
    ['id','value','ability','helpNeeded','helpOut','helpIn'],
    'formats':
    ['int64','float64','float32','float32','object','object'],
    'aligned':True
});
#Populate people with attributes
for id in ids:
    temp.append([
        #0 - id
        id,
        #1 - people's value
        sts.lognorm.rvs(.5)*100000,
        #2 - people's ability
        (1/(sts.lognorm.rvs(.99)+1)),
        #3 - help needed
        ((sts.lognorm.rvs(.99))*100),
        #4 - people helped
#This is where the problem is, if I get rid of these arrays, and the associated dtypes, there are no errors
        np.zeros(numOfP),
        #5 - people who helped you
        np.zeros(numOfP)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ])
peoType
temp
#doing np.array(temp), without the dtype works
temp=np.asarray(temp)      #doesn't change anything
temp
peo=np.array(temp,peoType) #where things break

dtype

{'names': ['id', 'value', 'ability', 'helpNeeded', 'helpOut', 'helpIn'],
 'formats': ['int64', 'float64', 'float32', 'float32', 'object', 'object'],
 'aligned': True}

错误消息

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
e:\xampp\htdocs\Math2Code\cooperate.py in 
     52     ])
     53 peoType
---> 54 peo=np.array(temp,peoType)

ValueError: setting an array element with a sequence.

列表的内容temp

[[0,
  86381.14170220899,
  0.12974876676966007,
  49.537761763004056,
  array([0., 0., 0., 0., 0.]),
  array([0., 0., 0., 0., 0.])],
 [1,
  95532.94886721167,
  0.3886984384013719,
  49.9244719570076,
  array([0., 0., 0., 0., 0.]),
  array([0., 0., 0., 0., 0.])],
 [2,
  53932.09250542036,
  0.6518993291826463,
  92.72979425242384,
  array([0., 0., 0., 0., 0.]),
  array([0., 0., 0., 0., 0.])],
 [3,
  161978.14156816195,
  0.49130827569636754,
  56.44742176255372,
  array([0., 0., 0., 0., 0.]),
  array([0., 0., 0., 0., 0.])],
 [4,
  38679.21128565417,
  0.6979042712239539,
  132.35562828412765,
  array([0., 0., 0., 0., 0.]),
  array([0., 0., 0., 0., 0.])]]

转换为非结构化数组后temp的内容

array([[0, 119297.86954924025, 0.38806815548557444, 487.4877681755314,
        array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])],
       [1, 75215.69897153028, 0.5387632600167043, 83.27487024641633,
        array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])],
       [2, 88986.345811315, 0.2533847055636237, 48.52795408229029,
        array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])],
       [3, 80539.81607335186, 0.27683829962996226, 226.25682883690638,
        array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])],
       [4, 40429.11615682778, 0.5748035151329913, 226.69671215072958,
        array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])]],
      dtype=object)

在2x2 np.zeros数组中使用时,peoType np.dtype变量的输出:

输入

np.zeros(2, peoType)

输出

array([(0, 0., 0., 0., 0, 0), (0, 0., 0., 0., 0, 0)],
      dtype={'names':['id','value','ability','helpNeeded','helpOut','helpIn'], 'formats':['<i8','<f8','<f4','<f4','O','O'], 'offsets':[0,8,16,20,24,32], 'itemsize':40, 'aligned':True})

为什么行以元组形式敲打


Tags: idobjectvaluenpzeros数组peoplearray
2条回答

您的复合数据类型:

In [33]: peoType=np.dtype({
    ...:     'names':
    ...:     ['id','value','ability','helpNeeded','helpOut','helpIn'],
    ...:     'formats':
    ...:     ['int64','float64','float32','float32','object','object'],
    ...:     'aligned':True
    ...: })

具有该数据类型的示例结构化数组:

In [34]: arr = np.zeros(2, peoType)
In [35]: arr
Out[35]: 
array([(0, 0., 0., 0., 0, 0), (0, 0., 0., 0., 0, 0)],
      dtype={'names':['id','value','ability','helpNeeded','helpOut','helpIn'], 'formats':['<i8','<f8','<f4','<f4','O','O'], 'offsets':[0,8,16,20,24,32], 'itemsize':40, 'aligned':True})
In [36]: arr['id']
Out[36]: array([0, 0])
In [37]: arr['helpOut']
Out[37]: array([0, 0], dtype=object)

()用于标记单个记录。这是一个1d数组,包含记录,而不是行和列。符号试图说明这一点。像reshapebroadcasting这样的操作不会跨越record边界

列出你的temp列表:

In [39]: array = np.array
In [40]: temp=[[0,
    ...:   86381.14170220899,
    ...:   0.12974876676966007,
    ...:   49.537761763004056,
    ...:   array([0., 0., 0., 0., 0.]),
    ...:   array([0., 0., 0., 0., 0.])],
    ...:  [1,
    ...:   95532.94886721167,
    ...:   0.3886984384013719,
    ...:   49.9244719570076,
    ...:   array([0., 0., 0., 0., 0.]),
    ...:   array([0., 0., 0., 0., 0.])],
    ...:  [2,
    ...:   53932.09250542036,
    ...:   0.6518993291826463,
    ...:   92.72979425242384,
    ...:   array([0., 0., 0., 0., 0.]),
    ...:   array([0., 0., 0., 0., 0.])],
    ...:  [3,
    ...:   161978.14156816195,
    ...:   0.49130827569636754,
    ...:   56.44742176255372,
    ...:   array([0., 0., 0., 0., 0.]),
    ...:   array([0., 0., 0., 0., 0.])],
    ...:  [4,
    ...:   38679.21128565417,
    ...:   0.6979042712239539,
    ...:   132.35562828412765,
    ...:   array([0., 0., 0., 0., 0.]),
    ...:   array([0., 0., 0., 0., 0.])]]

从列表中生成结构化数组-首先根据结构化数组的要求将其转换为元组列表:

In [42]: arr = np.array([tuple(row) for row in temp], peoType)
In [43]: arr
Out[43]: 
array([(0,  86381.14170221, 0.12974876,  49.53776 , array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])),
       (1,  95532.94886721, 0.38869843,  49.924473, array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])),
       (2,  53932.09250542, 0.65189934,  92.7298  , array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])),
       (3, 161978.14156816, 0.49130827,  56.447422, array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.])),
       (4,  38679.21128565, 0.6979043 , 132.35562 , array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.]))],
      dtype={'names':['id','value','ability','helpNeeded','helpOut','helpIn'], 'formats':['<i8','<f8','<f4','<f4','O','O'], 'offsets':[0,8,16,20,24,32], 'itemsize':40, 'aligned':True})
In [44]: arr['helpOut']
Out[44]: 
array([array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.]),
       array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0.]),
       array([0., 0., 0., 0., 0.])], dtype=object)

对象数据类型字段是一个1d对象数组-数组

如果所有这些对象字段都包含相同大小的数组,我们可以将其替换为多项目字段:

In [50]: dt=np.dtype({
    ...:     'names':
    ...:     ['id','value','ability','helpNeeded','helpOut','helpIn'],
    ...:     'formats':
    ...:     ['int64','float64','float32','float32','5float','5float'],
    ...:     'aligned':True
    ...: })
In [51]: arr = np.array([tuple(row) for row in temp], dt)
In [52]: arr
Out[52]: 
array([(0,  86381.14170221, 0.12974876,  49.53776 , [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]),
       (1,  95532.94886721, 0.38869843,  49.924473, [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]),
       (2,  53932.09250542, 0.65189934,  92.7298  , [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]),
       (3, 161978.14156816, 0.49130827,  56.447422, [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]),
       (4,  38679.21128565, 0.6979043 , 132.35562 , [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.])],
      dtype={'names':['id','value','ability','helpNeeded','helpOut','helpIn'], 'formats':['<i8','<f8','<f4','<f4',('<f8', (5,)),('<f8', (5,))], 'offsets':[0,8,16,20,24,64], 'itemsize':104, 'aligned':True})
In [53]: arr['helpOut']
Out[53]: 
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.]])

现在,该字段将生成一个二维数组

对于注释来说太大了,但这演示了用于生成结构化数组的输入元组。如果VAL是一个列表,那么您将得到一个错误。下面的示例正在使用您的输入之一

vals = (2,
  53932.09250542036,
  0.6518993291826463,
  92.72979425242384,
  np.array([0., 0., 0., 0., 0.]),
  np.array([0., 0., 0., 0., 0.]))

dt={'names':['id','value','ability','helpNeeded','helpOut','helpIn'], 'formats':['<i8','<f8','<f4','<f4','O','O']}

a = np.asarray(vals, dtype=dt)

a
array((2,  53932.09,  0.65,  92.73, array([ 0.00,  0.00,  0.00,  0.00,  0.00]), array([ 0.00,  0.00,  0.00,  0.00,  0.00])),
      dtype=[('id', '<i8'), ('value', '<f8'), ('ability', '<f4'), ('helpNeeded', '<f4'), ('helpOut', 'O'), ('helpIn', 'O')])

相关问题 更多 >

    热门问题