将HDF5文件数据集转换为Python中的三维numpy数组时运行缓慢

时间:2018-03-14 21:37:22

标签: python numpy hdf5

我有一个包含三个数据集的HDF5文件;一个包含名称,另外两个包含相关值。数据集很大,每个都有近100,000,000个元素。我想以制表符分隔的格式将前300个名称 - 值对打印到文件中,但是,我在实施我的解决方案时遇到了问题。

我想将三个数据集组合成一个三维的numpy数组,这样我就可以按第二列对值进行排序并拉出前300行。但是,我的程序似乎无法构建三维numpy数组,至少不能在合理的运行时中构建。我的代码如下所示。

#!/usr/bin/env python3

# Importing modules.
import h5py
import numpy as np

# Creating path for HDF5 file.
HDF5_PATH = ('/path/to/hdf5_file.hdf5')

# Creating path for outfile.
OUTFILE_PATH = ('/path/to/outfile.tsv')

# Loading HDF5 file.
hdf5_file = h5py.File(HDF5_PATH, 'r')

# Getting 3D array of datasets.
print('Building array')
hdf5_arr = np.array([hdf5_file['col_1'], hdf5_file['col_2'], hdf5_file['col_3']])

# Getting top 300 rows by second column.
print('Getting top 300 values')
top_300_arr = hdf5_arr[np.argpartition(hdf5_arr, axis=1)]

# Printing top 300 rows.
print('Printing top 300 values')
    with open(OUTFILE_PATH, 'a') as outfile:
        np.savetxt(outfile, top_300_arr, delimiter="\t", fmt='%s')

我已经添加了打印语句来监控进度,目前,我的代码打印出来Building array并且似​​乎没有进展至少一个小时。这意味着我的问题在于hdf5_arr = np.array([hdf5_file['col_1'], hdf5_file['col_2'], hdf5_file['col_3']])行。有什么方法可以改进我的代码,以便它可以在合适的运行时工作吗?

0 个答案:

没有答案
相关问题