代码之家  ›  专栏  ›  技术社区  ›  maximusdooku

如何附加、连接或合并2个以上的numpy数组?

  •  0
  • maximusdooku  · 技术社区  · 7 年前

    (1, 2, 1, 1, 1, 5, 2, 14)
    (1, 2, 1, 1, 1, 5, 2, 14)
    (1, 2, 1, 1, 1, 5, 2, 14)
    (1, 2, 1, 1, 1, 5, 2, 14)
    

    我想把它们合并成一个数组。形状将为:

    (4, 2, 1, 1, 1, 5, 2, 14)
    

    试验1

    np.append(f1, f2, axis=0) 它的形状是 (2, 2, 1, 1, 1, 5, 2, 14)

    或者是否有其他方法来管理这些数据?

    我唯一确定的是,这4个阵列的形状相同。

    试验2

    np.concatenate(f1, f2, f3)
    

    ----> 1 np.concatenate(f1, f2, f3)
    
    TypeError: only integer scalar arrays can be converted to a scalar index
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Kevin Fang    7 年前

    将数组放入列表中,然后使用 np.concatenate :

    import numpy as np
    l = [np.ones((1, 2, 1, 1, 1, 5, 2, 14))] * 4
    a = np.concatenate(l, axis=0)
    a.shape
    Out[9]: (4, 2, 1, 1, 1, 5, 2, 14)