使用多个数据集对.hdf5文件进行二次采样

使用多个数据集对.hdf5文件进行二次采样,hdf5,h5py,Hdf5,H5py,我试图从一个大的.h5文件中提取一些“行”,以创建一个较小的示例文件 为了确保我的示例看起来像原始文件,我随机抽取行 #Get length of files and prepare samples source_file = h5py.File(args.data_path, "r") dataset = source_file['X'] indices = np.sort(np.random.choice(dataset.shape[0],args.nb_rows)) #checki

我试图从一个大的.h5文件中提取一些“行”,以创建一个较小的示例文件

为了确保我的示例看起来像原始文件,我随机抽取行

#Get length of files and prepare samples
 source_file = h5py.File(args.data_path, "r")
 dataset = source_file['X']
 indices = np.sort(np.random.choice(dataset.shape[0],args.nb_rows))

#checking we're extracting a subsample
if args.nb_rows > dataset.shape[0]:
    raise ValueError("Can't extract more rows than dataset contains. Dataset has %s rows" % dataset.shape[0] )

target_file =  h5py.File(target, "w")
for k in source_file.keys():
    dataset = source_file[k]
    dataset = dataset[indices,:,:,:]
    dest_dataset = target_file.create_dataset(k, shape=(dataset.shape), dtype=np.float32)
dest_dataset.write_direct(dataset)
target_file.close()
source_file.close()

然而,当nb_行数超过(比如10000行)时,我得到了
TypeError(“索引元素的顺序必须是递增的”)
。索引已排序,因此我认为不应出现此错误。我有什么误解吗?

我想你得到的是重复的

显然,在
args.nb_rows>dataset.shape[0]
案例中会出现重复:

In [499]: np.random.choice(10, 20)
Out[499]: array([2, 4, 1, 5, 2, 8, 4, 3, 7, 0, 2, 6, 6, 8, 9, 3, 8, 4, 2, 5])
In [500]: np.sort(np.random.choice(10, 20))
Out[500]: array([1, 1, 1, 2, 2, 2, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 8, 9])
但是,当数字较小时,仍然可以获得重复项:

In [502]: np.sort(np.random.choice(10, 9))
Out[502]: array([0, 0, 1, 1, 1, 5, 5, 9, 9])
关闭
更换

In [504]: np.sort(np.random.choice(10, 9, replace=False))
Out[504]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])

所有版本的h5py和python都是这样吗?我不明白为什么在使用Python3.7的配置上获取重复索引甚至无序索引没有问题,但在另一台使用Python3.5的机器上却没有问题。。。