该.h5文件包括了6个groups,每个group包含x_data和y_data两个datasets,其中x_data是三维向量,y_data为标签,是一维向量
对自己数据的整理
import numpy as np
import pandas as pd
import glob
import os
import h5py
import scipy.io
#开始生成第一个站点a
path = r'E:\master\test\data_ly\site1\y_data\label_1.csv'
with open(path, encoding='gbk') as f:
ay = np.loadtxt(f, delimiter=',', skiprows=0,dtype=np.int)
path = r'E:\master\test\data_ly\site1\x_data' #第一个站点的文件路径,是文件夹
file = glob.glob(os.path.join(path, "*.csv")) #文件名相同部分
print(file)
ax = np.ones((len(file), 170, 263), dtype=np.float)#新建一个三维矩阵
i = 0
for m in file:
t1 = np.loadtxt(m, dtype=np.float, delimiter=',', encoding='utf-8')
t1 = t1[30:200]
ax[i, :, :] = t1
i = i+1
print(ax.shape)
print(ay.shape)
#结束
#开始生成第二个站点b
path = r'E:\master\test\data_ly\site2\x_data' #第二站点的文件路径,是文件夹
file = glob.glob(os.path.join(path, "*.csv")) #文件名相同部分
print(file)
path = r'E:\master\test\data_ly\site2\y_data\label_2.csv'
with open(path, encoding='gbk') as f:
by = np.loadtxt(f, delimiter=',', skiprows=0,dtype=np.int)
bx = np.ones((len(file), 170, 263), dtype=np.float)#新建一个三维矩阵
i = 0
for m in file:
t1 = np.loadtxt(m, dtype=np.float, delimiter=',', encoding='utf-8')
t1 =t1[10:180, :]
bx[i, :, :] = t1
i = i+1
print(bx.shape)
print(by.shape)
#结束
#开始生成第三个站点c
path = r'E:\master\test\data_ly\site3\x_data' #第三个站点的文件路径,是文件夹
file = glob.glob(os.path.join(path, "*.csv")) #文件名相同部分
print(file)
path = r'E:\master\test\data_ly\site3\y_data\label_3.csv'
with open(path, encoding='gbk') as f:
cy = np.loadtxt(f, delimiter=',', skiprows=0,dtype=np.int)
cx = np.ones((len(file), 170, 263), dtype=np.float) #新建一个三维矩阵
i = 0
for m in file:
t1 = np.loadtxt(m, dtype=np.float, delimiter=',', encoding='utf-8')
cx[i, :, :] = t1
i = i+1
print(cx.shape)
print(cy.shape)
#结束
#开始生成第四个站点d
path=r'E:\master\test\data_ly\site4\x_data' #第四个站点的文件路径,是文件夹
file=glob.glob(os.path.join(path, "*.csv")) #文件名相同部分
print(file)
path = r'E:\master\test\data_ly\site4\y_data\label_4.csv'
with open(path, encoding='gbk') as f:
dy = np.loadtxt(f, delimiter=',', skiprows=0, dtype=np.int)
dx = np.ones((len(file), 170, 263), dtype=np.float) #新建一个三维矩阵
i = 0
for m in file:
t1 = np.loadtxt(m, dtype=np.float, delimiter=',', encoding='utf-8')
dx[i, :, :] = t1
i = i+1
print(dx.shape)
print(dy.shape)
#结束
#开始生成第五个站点e
path = r'E:\master\test\data_ly\site5\x_data' #第五个站点的文件路径,是文件夹
file = glob.glob(os.path.join(path, "*.csv")) #文件名相同部分
print(file)
path = r'E:\master\test\data_ly\site5\y_data\label_5.csv'
with open(path, encoding='gbk') as f:
ey = np.loadtxt(f, delimiter=',', skiprows=0,dtype=np.int)
ex = np.ones((len(file), 170, 263),dtype=np.float)#新建一个三维矩阵
i = 0
for m in file:
t1 = np.loadtxt(m, dtype=np.float, delimiter=',', encoding='utf-8')
t1 =t1[30:200, :]
ex[i, :, :] = t1
i = i+1
print(ex.shape)
print(ey.shape)
#结束
#开始生成第六个站点g
path=r'E:\master\test\data_ly\site6\x_data' #第六个站点的文件路径,是文件夹
file=glob.glob(os.path.join(path, "*.csv")) #文件名相同部分
print(file)
path = r'E:\master\test\data_ly\site6\y_data\label_6.csv'
with open(path, encoding='gbk') as f:
fy = np.loadtxt(f, delimiter=',', skiprows=0,dtype=np.int)
fx = np.ones((len(file), 170, 263), dtype=np.float) #新建一个三维矩阵
i = 0
for m in file:
t1 = np.loadtxt(m, dtype=np.float, delimiter=',', encoding='utf-8')
fx[i, :, :] = t1
i = i+1
print(fx.shape)
print(fy.shape)
将数据写入.h5
f = h5py.File("myh5py.hdf5", "w") #对h5文件写入
f.create_group("/site1") #新建group “site1”
f.create_group("/site2")
f.create_group("/site3")
f.create_group("/site4")
f.create_group("/site5")
f.create_group("/site6")
h1 = f['site1']
h1.create_dataset("x", data=ax) #为group “site1”新建dataset “x1”
h1.create_dataset("y", data=ay)
h2 = f['site2']
h2.create_dataset("x", data=bx)
h2.create_dataset("y", data=by)
h3 = f['site3']
h3.create_dataset("x", data=cx)
h3.create_dataset("y", data=cy)
h4 = f['site4']
h4.create_dataset("x", data=dx)
h4.create_dataset("y", data=dy)
h5 = f['site5']
h5.create_dataset("x", data=ex)
h5.create_dataset("y", data=ey)
h6 = f['site6']
h6.create_dataset("x", data=fx)
h6.create_dataset("y", data=fy)
print(list(f.keys()))
print(list(h1.keys()))