Python 如何在简单numpy阵列上移动轴
我无法将3轴移动到1位置。我想将3移动到第一个69位置。这是一个机器学习数据集,PyTorch只接受3x69x69格式的数据。谢谢你的帮助Python 如何在简单numpy阵列上移动轴,python,numpy,pytorch,h5py,np,Python,Numpy,Pytorch,H5py,Np,我无法将3轴移动到1位置。我想将3移动到第一个69位置。这是一个机器学习数据集,PyTorch只接受3x69x69格式的数据。谢谢你的帮助 # To get the images and labels from file with h5py.File(r"C:\Users\ajbur\Downloads\Galaxy10.h5", 'r') as F: images = np.array(F['images']) labels = np.array(F['ans']) np.s
# To get the images and labels from file
with h5py.File(r"C:\Users\ajbur\Downloads\Galaxy10.h5", 'r') as F:
images = np.array(F['images'])
labels = np.array(F['ans'])
np.shape(images)
np.moveaxis(images,0,-1).shape
np.shape(images)
产量为[20000,69,69,3]
我希望它是[20000,3,69,69]moveaxis的第二个和第三个参数是源和目标。要将最后一个轴移动到第二个位置,可以执行以下操作:
a = np.empty([20000, 69, 69, 3])
np.moveaxis(a, -1, 1).shape
>>> (20000, 3, 69, 69)
请尝试编辑您的问题,以描述您得到了什么以及您期望得到什么。注意:您可以使用
images=F['images'][:]
简化代码,以np数组的形式返回数据。另外,moveaxis()
将修改np数组,但不会更改原始HDF5数据集。我喜欢转置,x=images.transpose(0,3,1,2)
。