简体   繁体   English

从另外两个 numpy 数组创建新的 numpy 数组

[英]new numpy array creation from two other numpy array

I have a 2 numpy arrays in the following format我有 2 个 numpy arrays,格式如下

array([[2, 2, 7, 1],
       [5, 0, 3, 1],
       [2, 9, 8, 8],
       [5, 7, 7, 6]])

and

array([[1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12],
       [13, 14, 15, 16]])

I want to combine these to the following format我想将这些组合成以下格式

array([[[2, 2, 7, 1],[1,2,3,4]],
       [[5, 0, 3, 1],[5,6,7,8]],
       [[2, 9, 8, 8],[9,10,11,12]],
       [[5, 7, 7, 6],[13,14,15,16]]])

The real data which I am handling contains very large amount of data(5000 in one row).我正在处理的真实数据包含大量数据(一行 5000 个)。 SO working with pandas doesnt solve the case.所以使用 pandas 并不能解决这个问题。 Is there any efficient method when the data is very huge for creating a format like this当数据非常庞大时,是否有任何有效的方法来创建这样的格式

You are looking for stack您正在寻找堆栈

arr1 = np.array([[2, 2, 7, 1],
                 [5, 0, 3, 1],
                 [2, 9, 8, 8],
                 [5, 7, 7, 6]])

arr2 = np.array([[1, 2, 3, 4],
                 [5, 6, 7, 8],
                 [9, 10, 11, 12],
                 [13, 14, 15, 16]])

arr3 = np.stack((arr1, arr2), axis=1)
print(arr3)

Output Output

[[[ 2  2  7  1]
  [ 1  2  3  4]]

 [[ 5  0  3  1]
  [ 5  6  7  8]]

 [[ 2  9  8  8]
  [ 9 10 11 12]]

 [[ 5  7  7  6]
  [13 14 15 16]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM