簡體   English   中英

如何在HDF5中制作多維度數據集?

[英]How to make a multi-dims dataset in HDF5?

例如,我要制作兩個數據集,一個是Input ,另一個是Output

InputOutput中的數據是多維的。

在此處輸入圖片說明

但是我注意到在h5pyinput_nodeoutput_node是固定的。

Input =  f.create_dataset('Input',  (3,input_node ),dtype='float', chunks=True)
Output = f.create_dataset('Output', (3,output_node),dtype='float', chunks=True)

但是hdf5無法處理此問題,此代碼可以證明這一點

import h5py

X = [[1,2,3,4],[1,2],[1,2,3,4,5,6]]

with h5py.File('myfile.hdf5', "w") as ofile:
    ofile.create_dataset("X", data=X)

TypeError:對象dtype dtype('O')沒有等效的本機HDF5

那么如何在h5py制作一個多維數據集呢?

我不太理解您的{...}表示的意思。 在Python中,這些用於字典和集合。 []用於列表, ()用於元組。 數組形狀表示為元組。

無論如何,您的代碼會產生

In [68]: X
Out[68]: 
array([ list([0.6503719194043309, 0.8703218883225239, -1.4139639093161405, 2.3288987644271835, -1.7957516518177206]),
       list([-0.1781710442823114, 0.9591992379396287, -0.6319292685053243]),
       list([0.7104492662861611, -0.8951817329357393, -0.8925882332063567, 1.5587934871464815]),
       list([-1.2384976614455354, 0.9044140291496179, 1.1277220227448401]),
       list([1.1386910680393805, -0.1775792543137636, 1.0567836199711476]),
       list([2.7535019220459707, 0.29518918092088386, -0.32166742909305196, 1.5269788560083497, 0.29633276686886767]),
       list([1.6397535315116918, -0.8839570613086122, -0.4491121599234047, -2.4461439611764333, -0.6884616200199412, -1.1920165045444608]),
       list([1.3240629024597295, 1.170019287452736, 0.5999977019629572, -0.38338543090263366, 0.6030856099472732]),
       list([-0.013529997305716175, -0.7093551284624415, -1.8611980839518099, 0.9165791506693297]),
       list([2.384081118320432, -0.6158201308053464, 0.8802896893269192, -0.7636283160361232])], dtype=object)
In [69]: y
Out[69]: array([1, 1, 0, 0, 0, 1, 1, 0, 1, 0])

y是一個簡單的數組。 h5py保存該h5py應該沒有問題。

X是對象dtype數組,包含大小可變的列表

In [72]: [len(l) for l in X]
Out[72]: [5, 3, 4, 3, 3, 5, 6, 5, 4, 4]

h5py無法保存這種數組。 充其量您可以將每個元素寫入不同的dataset 它將每個保存為數組。

....
   for i, item in enumerate(X):
      ofile.create_dataset('name%s'%i, data=item)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM