Numpy transpose of 1D array not giving expected result

前端 未结 4 1525
暗喜
暗喜 2020-12-05 18:20

I am trying a very basic example in Python scipy module for transpose() method but it\'s not giving expected result. I am using Ipython with pylab mode.

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 18:33

    Transpose is a noop for one-dimensional arrays.

    Add new axis and transpose:

    >>> a[None].T
    array([[1],
           [2],
           [3]])
    >>> np.newaxis is None
    True
    

    Or reshape:

    >>> a.reshape(a.shape+(1,))
    array([[1],
           [2],
           [3]])
    

    Or as @Sven Marnach suggested in comments, add new axis at the end:

    >>> a[:,None]
    array([[1],
           [2],
           [3]])
    

提交回复
热议问题