我正在尝试创建一个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})
为什么行以元组形式敲打
您的复合数据类型:
具有该数据类型的示例结构化数组:
()
用于标记单个记录。这是一个1d数组,包含记录,而不是行和列。符号试图说明这一点。像reshape
和broadcasting
这样的操作不会跨越record
边界列出你的
temp
列表:从列表中生成结构化数组-首先根据结构化数组的要求将其转换为元组列表:
对象数据类型字段是一个1d对象数组-数组
如果所有这些对象字段都包含相同大小的数组,我们可以将其替换为多项目字段:
现在,该字段将生成一个二维数组
对于注释来说太大了,但这演示了用于生成结构化数组的输入元组。如果VAL是一个列表,那么您将得到一个错误。下面的示例正在使用您的输入之一
相关问题 更多 >
编程相关推荐