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

后端 未结 6 1554
甜味超标
甜味超标 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:42

    1) The reason not to prefer a shape of (R, 1) over (R,) is that it unnecessarily complicates things. Besides, why would it be preferable to have shape (R, 1) by default for a length-R vector instead of (1, R)? It's better to keep it simple and be explicit when you require additional dimensions.

    2) For your example, you are computing an outer product so you can do this without a reshape call by using np.outer:

    np.outer(M[:,0], numpy.ones((1, R)))
    

提交回复
热议问题