julia - 保存数组的数组、HDF5、Julia

标签 julia hdf5

交叉发布here ,但是如何使用 HDF5 在 Julia 中保存数组数组?

在我的特定情况下,我有一个包含 10,000 个不同长度的数组的数组。我希望 10,000 个数组成为“组”的一部分,但为每个数组创建新的数据集/组会使读取文件变得非常慢,因此我正在寻找替代方案。

最佳答案

您可以将数组数组展平为一个数组,其中一列包含原始数据,另一列表示该数据最初来自第 i 个数组。

using HDF5
# Define your array of arrays.
arr = [[1,2],[3,4,5]]

# Open your hdf5 file
h5open("data.hdf5", "w") do f
    # Create a dataset with the length of all your arrays combined.
    N = sum(length.(arr))
    d_create(f, "X", Int, ((2,N),(2,-1)), "chunk", (1,1000))

    n = 1
    for i in 1:length(arr)
        m = length(arr[i])
        f["X"][1, n:n+m-1] = fill(i, m)
        f["X"][2, n:n+m-1] = arr[i]
        n+=m
    end
    print(f["X"][:,:])
end

然后数组存储如下:

> [1 1 2 2 2; 1 2 3 4 5]

关于julia - 保存数组的数组、HDF5、Julia,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58983994/

相关文章:

Julia - 基于值数组过滤的最快方法?

julia - 如何从源代码构建 Julia?

julia - 如何在 Julia 中构建直方图

HDF5: "file buffer"和 "file cache"之间有什么区别?

julia - 在文件上使用 cmd 或 linux 终端时如何启用颜色?

performance - 在 Julia 中并行操作大型常量数据结构

python - 如何将属性添加到作为组存储在 HDF5 文件中的 Pandas 数据帧?

python - 用于 Python 的 HDF5 : high level vs low level interfaces. h5py

java - 结合 Java、Python、PyTables 和 HDF5 的简单有效的解决方案

读取存储在 HDF5 中的数据帧