1我目前正在使用结构化阵列来保存传感器的一些测量值。数组(名为“data”)的维度为2000x3,包含三个字段:“samples”,“timestamp”和“labels”,其中samples是6个元素的向量。例如,一行如下所示:
([-19.837763650963275, -19.61692779005053, -18.5301618270122, -13.413484985874076, -13.192649124961326, -12.105883161923], 0.0, 0)
[('samples', '<f8', (6,)), ('timestamp', '<f8'), ('labels', '<i4')]
如果我现在想拥有一行的所有样本,我可以访问这样的行:
data["samples"][10]
工作正常,但如果我把它转过来写这样:
data[10]["samples"]
我收到以下错误:
ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()
有谁知道为什么会这样?
编辑:为了更好地理解这里是“数据”数组的前10行:
[ ([-19.837763650963275, -19.61692779005053, -18.5301618270122, -13.413484985874076, -13.192649124961326, -12.105883161923], 0.0, 0) ([-18.66282477705446, -18.449317421024432, -17.369283339067675, -12.357118609142269, -12.145937637117552, -11.068086720774204], 0.0, 0) ([-17.69388207920866, -17.49198382816449, -16.417085567075173, -11.53193799727324, -11.33976221074188, -10.268890664091229], 0.0, 0) ([-16.868088042481606, -16.683019283158636, -15.610803357043569, -10.88697503359368, -10.732729221349611, -9.663174869208511], 0.0, 0) ([-16.152597338007514, -15.99074244228478, -14.917542852993487, -10.420257243109129, -10.371835643625495, -9.274470774420056], 0.0, 0) ([-15.527885804583931, -15.39736918876727, -14.317660328685264, -10.451759205557037, -10.011944429521288, -9.006031495483906], 0.0, 0) ([-14.981993405744573, -14.894234811642814, -13.799157258392123, -10.01393043959338, -9.563854671143899, -8.587392657229502], 0.0, 0) ([-14.508475562047407, -14.48315918653869, -13.355984261155621, -9.577456476813333, -9.16920566037696, -8.205174149255923], 0.0, 0) ([-14.106856958780497, -14.199089434545343, -12.989626950396643, -9.214711746255777, -8.944244361010687, -7.942834820090279], 0.0, 0) ([-13.789298779585817, -13.943732248940886, -12.72321280228509, -9.208476598556874, -8.629970466866272, -7.690122078593869], 0.0, 0)]
编辑2: 我正在使用numpy版本1.10.4
而我实际上并没有使用
data[0]["samples"]
但我尝试了它并没有效果。我这样用它:
for row in data: print(row["samples"])
我做了一个我能想到的最基本的例子,而且这令人惊讶:
我做了一个我能想到的最基本的例子,而且这令人惊讶:
a = np.zeros(10, dtype = [("a", "5f8"), ("b", "i4")])
print(a["a"][5]) # Works
print(a[5]["a"]) # Surprisingly works as well