当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Add constant value to an array which is stored in an hdf5 file

问题描述:

I want to add a constant value to an array. The array is stored in an hdf5 file.

f = h5py.File(fileName)

f['numbers'] = f['numbers'] + 5

gives me an error that TypeError: unsupported operand type(s) for +: 'Dataset' and 'int'

how should I do it?

网友答案:

f['numbers'][:]+=5 works.

f['numbers'] + 5 does not work because the Dataset object does not have methods like __add__. So the Python interpreter gives you the unsupported error.

Adding the [:] gives an ndarray, with the full set of numpy methods.

Doesn't the documentation talk about loading slices of the data into memory?

`f['numbers'][:10] += 5

might work as way to change a portion. The addition is still being done in memory.

See earlier questions like How to store an array in hdf5 file which is too big to store in memory

Another option is to look at the compiled h5 code. There probably are Fortran or C based scripts which will make changes to the data like this. You could easily call those from Python.

网友答案:

You have to use the actual numpy.add function:

ds = f['numbers']
ds[:] = np.add(ds, 5)

(Though I do like your syntax better. Maybe that's worth suggesting to the h5py people.)

分享给朋友:
您可能感兴趣的文章:
随机阅读: