Object dtype dtype('O') has no native HDF5 equivalent

前提是你 提交于 2021-01-27 11:51:46

问题


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.

I have a problem with saving array or list into 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)

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

Object dtype dtype('O') has no native HDF5 equivalent

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)

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)

Which looks it has the similar problem with saving hdf5.

Again, how can I save such data in hdf5?


回答1:


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')

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

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).



来源:https://stackoverflow.com/questions/53358689/object-dtype-dtypeo-has-no-native-hdf5-equivalent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!