zoukankan      html  css  js  c++  java
  • concatenate of numpy

    You may first use np.concatenate

    >>> a = np.array([[1, 2], [3, 4]])
    >>> b = np.array([[5, 6]])
    >>> np.concatenate((a, b), axis=0)
    array([[1, 2],
           [3, 4],
           [5, 6]])
    >>> np.concatenate((a, b.T), axis=1)
    array([[1, 2, 5],
           [3, 4, 6]])
    >>> np.concatenate((a, b), axis=None)
    array([1, 2, 3, 4, 5, 6])
    

    Don't forget np.append:

    np.append is usually used for concatenating vectors:
    Examples

    >>>
    >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
    array([1, 2, 3, ..., 7, 8, 9])
    

    When axis is specified, values must have the correct shape.

    >>>
    >>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    >>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
    Traceback (most recent call last):
        ...
    ValueError: all the input arrays must have same number of dimensions
    

    The vstack and hstack are ituitive and good to use

    numpy.vstack(tup) : Stack arrays in sequence vertically (row wise).

    >>> a = np.array([1, 2, 3])
    >>> b = np.array([2, 3, 4])
    >>> np.vstack((a,b))
    array([[1, 2, 3],
           [2, 3, 4]])
    >>>
    >>> a = np.array([[1], [2], [3]])
    >>> b = np.array([[2], [3], [4]])
    >>> np.vstack((a,b))
    array([[1],
           [2],
           [3],
           [2],
           [3],
           [4]])
    

    numpy.hstack(tup) : Stack arrays in sequence horizontally (column wise).

    >>> a = np.array((1,2,3))
    >>> b = np.array((2,3,4))
    >>> np.hstack((a,b))
    array([1, 2, 3, 2, 3, 4])
    >>> a = np.array([[1],[2],[3]])
    >>> b = np.array([[2],[3],[4]])
    >>> np.hstack((a,b))
    array([[1, 2],
           [2, 3],
           [3, 4]])
    

    reference: g: "numpy concatenate" and "numpy append"

  • 相关阅读:
    2019.9.4 二维树状数组
    2019.9.4 简单题
    0052-YH的计算器
    0051-打乱顺序的三位数
    0050-计算天数
    0049-学校的上网费
    0048-三角形的判断
    0047-月份转换
    0046-简单的分段函数(二)
    0045-简单的分段函数(一)
  • 原文地址:https://www.cnblogs.com/sonictl/p/12614252.html
Copyright © 2011-2022 走看看