【发布时间】:2015-01-09 10:56:53
【问题描述】:
我想创建一个numpy 数组(大小约为 65000 行 x 17 列)。第一列包含复数,其余包含无符号整数。
我首先创建一个所需大小的numpy.zeros 数组,然后我想用上面描述的复数和 uint 填充它。我查看了dtypes 选项,其中应该是我认为的解决方案,但我无法让它工作。
之后,我想将整个数组保存为 CSV 格式的文本文件,如下所示:
0.25+0.30j,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1
0.30+0.40j,0,1,0,0,0,0,0,0,1,0,1,1,1,1,1,1
等等……
我试过这个,但后来它给了我以下错误:
TypeError: +: 'numpy.ndarray' 和不支持的操作数类型 'numpy.ndarray'
m = 16
dt = numpy.dtype([('comp', numpy.complex), ('f0', numpy.int64), ('f1', numpy.int64),
('f2', numpy.int64), ('f3', numpy.int64), ('f4', numpy.int64), ('f5', numpy.int64),
('f6', numpy.int64), ('f7', numpy.int64), ('f8', numpy.int64), ('f9', numpy.int64),
('f10', numpy.int64), ('f11', numpy.int64), ('f12', numpy.int64), ('f13', numpy.int64),
('f14', numpy.int64), ('f15', numpy.int64)])
fields = numpy.zeros((2**m, m+1), dtype=dt)
for i in range(0, m):
fields[:,0] = fields[:,0] + 1 # for example I add only 1 here
【问题讨论】:
-
请附上您的代码。
-
数据类型现在本身就是一个“行”,所以你只需要
fields = numpy.zeros(2**m, dtype=dt)。 -
在 for 循环中它给了我以下错误:IndexError: too many indices
-
您的
fields现在是 2d,有 17 列,每个元素有 17 个字段。这比您可能想要的要大 17 倍。
标签: python arrays numpy complex-numbers