本文介绍了Python:如何在 PyTables 中存储一个 numpy 多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 PyTables 在 HDF5 文件中放置一个 numpy 多维数组?

How can I put a numpy multidimensional array in a HDF5 file using PyTables?

据我所知,我不能将数组字段放入 pytables 表中.

From what I can tell I can't put an array field in a pytables table.

我还需要存储一些关于这个数组的信息,并能够对其进行数学计算.

I also need to store some info about this array and be able to do mathematical computations on it.

有什么建议吗?

推荐答案

可能有更简单的方法,但据我所知,这就是你的做法:

There may be a simpler way, but this is how you'd go about doing it, as far as I know:

import numpy as np
import tables

# Generate some data
x = np.random.random((100,100,100))

# Store "x" in a chunked array...
f = tables.open_file('test.hdf', 'w')
atom = tables.Atom.from_dtype(x.dtype)
ds = f.createCArray(f.root, 'somename', atom, x.shape)
ds[:] = x
f.close()

如果您想指定要使用的压缩,请查看tables.Filters.例如

If you want to specify the compression to use, have a look at tables.Filters. E.g.

import numpy as np
import tables

# Generate some data
x = np.random.random((100,100,100))

# Store "x" in a chunked array with level 5 BLOSC compression...
f = tables.open_file('test.hdf', 'w')
atom = tables.Atom.from_dtype(x.dtype)
filters = tables.Filters(complib='blosc', complevel=5)
ds = f.createCArray(f.root, 'somename', atom, x.shape, filters=filters)
ds[:] = x
f.close()

可能有一种更简单的方法来解决这个问题......我已经很长一段时间没有将 pytables 用于除类似表格的数据之外的任何其他内容.

There's probably a simpler way for a lot of this... I haven't used pytables for anything other than table-like data in a long while.

注意:在 pytables 3.0 中,f.createCArray 被重命名为 f.create_carray.也可以直接接受数组,不指定atom,

Note: with pytables 3.0, f.createCArray was renamed to f.create_carray. It can also accept the array directly, without specifying the atom,

f.create_carray('/', 'somename', obj=x, filters=filters)

这篇关于Python:如何在 PyTables 中存储一个 numpy 多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:57