本文介绍了对象dtype dtype('O')没有等效的本机HDF5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,似乎在堆栈溢出中曾问过几个类似的问题,但似乎没有一个回答正确或正确,也没有描述确切的例子.

Well, it seems like a couple of similar questions were asked here in stack overflow, but none of them seem like answered correctly or properly, nor they described the exact examples.

我在将数组或列表保存到hdf5时遇到问题...

I have a problem with saving array or list into hdf5 ...

我有几个文件包含(n,35)维的列表,其中每个文件中的n可能不同.它们每个都可以使用以下代码保存在hdf5中.

I have a several files contains list of (n, 35) dimensions, where n may be different in each file. Each of them can be saved in hdf5 with code below.

hdf = hf.create_dataset(fname, data=d)

但是,如果我想将它们合并以在3d中制作,则会出现以下错误.

However, if I want to merge them to make in 3d the error occurs as below.

我不知道为什么它会变成dtype对象,因为我所做的只是这个

I have no idea why it turns to dtype object, since what I have done is only this

all_data = list()
for fname in file_list:
    d = np.load(fname)
    all_data.append(d)
hdf = hf.create_dataset('all_data', data=all_data)

如何保存此类数据?我尝试了几次测试,当我用'p'更改它们时,似乎all_data变成了'object'的dtype.

How can I save such data?I tried a couple of tests, and it seems like all_data turns to dtype with 'object' when I change them with

all_data = np.array(all_data)

看起来与保存hdf5有类似的问题.

Which looks it has the similar problem with saving hdf5.

再次,如何将此类数据保存在hdf5中?

Again, how can I save such data in hdf5?

推荐答案

我遇到了与 h5py 类似的问题,并使用 array.astype 为我工作(我相信这会将类型从 dtype('O')更改为您指定的数据类型).请参见下面的代码段:

I was running into a similar issue with h5py, and changing the type of the NumPy array using array.astype worked for me (I believe this changes the type from dtype('O') to the data type you specify). Please see the code snippet below:

import numpy as np

print(X.dtype)
--> dtype('O')

print(X.astype(np.float64).dtype)
--> dtype('float64')

当我使用此数据类型转换运行 h5.create_dataset 时,我能够成功创建h5数据集.希望这会有所帮助!

When I ran h5.create_dataset with this data type conversion, I was able to successfully create a h5 dataset. Hope this helps!

一个附加更新:我相信NumPy对象类型'O'是在NumPy数组本身具有混合元素类型(例如 np.int8 np.float32 ).

ONE ADDITIONAL UPDATE: I believe the NumPy object type 'O' is created when the NumPy array itself has mixed element types (e.g. np.int8 and np.float32).

这篇关于对象dtype dtype('O')没有等效的本机HDF5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:53