我正在尝试组合两个包含整数的数组 (5000,2) 和包含浮点数的数组 (5000,7)。当写入 h5 时,我需要分配名称以充当列标题,但是当我尝试分配名称和数据类型时,数组中的每一列都会重复 9 次。
我的代码如下:
namesList = ['EID', 'Domain', 'LAM ID1', 'LAM ID2','LAM ID3', 'LAM ID4', 'LAM ID5', 'LAM ID6', 'LAM ID7']
formatsList = ['int', 'int', 'float', 'float', 'float', 'float', 'float', 'float', 'float']
ds_dt = np.dtype({'names':namesList, 'formats':formatsList})
Final_Lam_Strength = np.concatenate((LAM_Strength_RFs_Data, LAM_Strength_RFs), axis=1).astype(ds_dt)
谢谢
使用
np.empty()
创建数组,并使用行数定义形状,并使用 dtype
定义 ds_dt
。然后使用命名字段和列引用加载数据。
您的示例使用一些简单的数据进行了修改:
namesList = ['EID', 'Domain', 'LAM ID1', 'LAM ID2','LAM ID3', 'LAM ID4', 'LAM ID5', 'LAM ID6', 'LAM ID7']
formatsList = ['int', 'int', 'float', 'float', 'float', 'float', 'float', 'float', 'float']
ds_dt = np.dtype({'names':namesList, 'formats':formatsList})
nrows, nints, nfloats = 50, 2,7
LAM_Strength_RFs_Data = np.arange(nrows*nints).reshape(nrows,nints)
LAM_Strength_RFs = np.arange(nrows*nfloats).reshape(nrows,nfloats)
Final_Lam_Strength = np.empty(shape=(nrows,),dtype=ds_dt)
print(Final_Lam_Strength.dtype, Final_Lam_Strength.shape)
for i in range(2):
Final_Lam_Strength[namesList[i]] = LAM_Strength_RFs_Data[:,i]
for i in range(6):
Final_Lam_Strength[namesList[i+2]] = LAM_Strength_RFs[:,i]
print(Final_Lam_Strength[0])