[英]h5py stores only 0s in datasets
I'm trying to store some values in an h5py file, but every time I try to store a matrix in a dataset, all the matrix elements are replaced by 0s.我试图将一些值存储在 h5py 文件中,但每次我尝试将矩阵存储在数据集中时,所有矩阵元素都被替换为 0。 Here's an example这是一个例子
I create the file like this:我这样创建文件:
output_file=h5py.File('output_file', 'w')
dset=output_file.create_dataset('dset', (3,3))
for k in range(3):
for l in range(3):
dset[k][l]=1.
I then read the file and try to print the output然后我读取文件并尝试打印 output
file=h5py.File('output_file', 'r')
print(file['dset'][:])
the output is output 是
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
all the 1s have been turned into 0s.所有的 1 都变成了 0。 What am I doing wrong?我究竟做错了什么?
This is explicitly covered in the manual .这在手册中有明确说明。 When you do dset[k]
, you create a temporary array.当您执行dset[k]
时,您会创建一个临时数组。 It is this array's l
'th element that you set when you do dset[k][l] = 1.0
.这是您在执行dset[k][l] = 1.0
时设置的数组的第l
个元素。 That temporary array is not the h5py dataset that you want to be referring to – you aren't touching the latter at all.该临时数组不是您想要引用的 h5py 数据集——您根本没有触及后者。
In short: index with dset[k, l]
instead.简而言之:用dset[k, l]
代替索引。
Try with试试
dset[k,l]=matrix[k][l]
instead.反而。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.