我想创建一个Python Numpy数组(2,7)指定每列的类型。一些列将是数组。所以我期望的数组应该是这样的:数据类型为2D NumpyArray
[[ (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, 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.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], [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, 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.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], [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.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])]]
我试图
>>> A = np.zeros(shape=(2), dtype= 'int, (3)float, (8)float, (8)float, (8)float, (10)float, (10)float')
,但我得到一维数组:
>>> print A
[ (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, 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.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], [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, 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.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], [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.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])]
如果我定义它是这样的:
>>> A = np.zeros(shape=(2,7), dtype= 'int, (3)float, (8)float, (8)float, (8)float, (10)float, (10)float')
我得到的数组比我想要的大得多;它是(2,7x7)。
虽然这样做,我得到一个错误:
>>> A = np.zeros(shape=([[2],[7]]), dtype= 'int, (3)float, (8)float, (8)float, (8)float, (10)float, (10)float')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
我不明白怎么去我的输出。任何帮助,可能与解释高度赞赏!谢谢!
这真的很酷!我当然对官方文档不甚了解,或者文档和实现之间缺乏联系。我希望这能帮助OP解决他的问题。 – innoSPG
谢谢hpaulj!但是,当我创建一个空的二维数组'形状=(2,7)',然后尝试插入一个列表时,我得到这个错误'ValueError:设置一个数组元素的序列。所以我想我需要指定'A [0] [1]',例如,应该是一个由3个浮点数组成的子数组,其他cols等等。怎么做?可能吗? –
我已经在我的'A'数组中添加了一些设置值的例子。 – hpaulj