Difference between numpy.array shape (R, 1) and (R,)

后端 未结 6 1560
甜味超标
甜味超标 2020-11-22 04:25

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 04:26

    The shape is a tuple. If there is only 1 dimension the shape will be one number and just blank after a comma. For 2+ dimensions, there will be a number after all the commas.

    # 1 dimension with 2 elements, shape = (2,). 
    # Note there's nothing after the comma.
    z=np.array([  # start dimension
        10,       # not a dimension
        20        # not a dimension
    ])            # end dimension
    print(z.shape)
    

    (2,)

    # 2 dimensions, each with 1 element, shape = (2,1)
    w=np.array([  # start outer dimension 
        [10],     # element is in an inner dimension
        [20]      # element is in an inner dimension
    ])            # end outer dimension
    print(w.shape)
    

    (2,1)

提交回复
热议问题