合并多个numpy数组 [英] merging multiple numpy arrays
本文介绍了合并多个numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个numpy数组,它们存储形状为(4,100,100)的图像数据。
i have 3 numpy arrays which store image data of shape (4,100,100).
arr1= np.load(r'C:\Users\x\Desktop\py\output\a1.npy')
arr2= np.load(r'C:\Users\x\Desktop\py\output\a2.npy')
arr3= np.load(r'C:\Users\x\Desktop\py\output\a3.npy')
我想将所有3个数组合并为1个数组。
我已尝试过这种方式:
I want to merge all 3 arrays into 1 array. I have tried in this way:
merg_arr = np.zeros((len(arr1)+len(arr2)+len(arr3), 4,100,100), dtype=input_img.dtype)
现在这构成一个数组所需的长度,但我不知道如何复制此数组中的所有数据。
now this make an array of the required length but I don't know how to copy all the data in this array. may be using a loop?
推荐答案
这可以解决问题:
merge_arr = np.concatenate([arr1, arr2, arr3], axis=0)
np .stack
沿新维度排列数组。
np.stack
arranges arrays along a new dimension. Their dimensions (except for the first) need to match.
Demo:
arr1 = np.empty((60, 4, 10, 10))
arr2 = np.empty((14, 4, 10, 10))
arr3 = np.empty((6, 4, 10, 10))
merge_arr = np.concatenate([arr1, arr2, arr3], axis=0)
print(merge_arr.shape) # (80, 4, 10, 10)
这篇关于合并多个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文