Numpy Vector (N,1) dimension -> (N,) dimension conversion

后端 未结 5 2057
耶瑟儿~
耶瑟儿~ 2020-12-01 17:35

I have a question regarding the conversion between (N,) dimension arrays and (N,1) dimension arrays. For example, y is (2,) dimension.

A=np.array([[1,2],[3,4         


        
5条回答
  •  执笔经年
    2020-12-01 18:24

    reshape works for this

    a  = np.arange(3)        # a.shape  = (3,)
    b  = a.reshape((3,1))    # b.shape  = (3,1)
    b2 = a.reshape((-1,1))   # b2.shape = (3,1)
    c  = b.reshape((3,))     # c.shape  = (3,)
    c2 = b.reshape((-1,))    # c2.shape = (3,)
    

    note also that reshape doesn't copy the data unless it needs to for the new shape (which it doesn't need to do here):

    a.__array_interface__['data']   # (22356720, False)
    b.__array_interface__['data']   # (22356720, False)
    c.__array_interface__['data']   # (22356720, False)
    

提交回复
热议问题