0

In Numpy, what is the difference between an array with shape (4,) and an array with shape (4,1)?

Is it that the latter is a vector?

  • The first one is a unidimensional array, the second one is a 2D array. The difference between `(4,1)` and `(4,1,1)` is that the latter is 3D array, etc etc etc – rafaelc Oct 10 '19 at 17:46
  • They are both technically vectors. But the latter has two dimensions i.e. you have two axes for the latter. Matrix multiplication for example requires specific shape alignment and that can impact your code. `a = np.array([1,2,3])`, `b = np.array([[1],[2],[3]])` are both vectors but I cannot do `np.dot(b,b)` yet I can do `np.dot(a,a)` and `np.dot(a,b)`. But I cannot do `np.dot(b,a)`. This is because you can think of `b` as a matrix of shape (3,1) and so `b*v` requires `v` to be of shape (1,?) but `v*b` , v of shape (?,3). In the case of a shape (3,) array, it can act like a (?,3) shape array – Buckeye14Guy Oct 10 '19 at 17:51
  • Technically `numpy` doesn't have `vectors`. Its arrays can have any dimension from 0 up (to 32). A (4,1) array can have the same data elements as a (4,) (or a (1,4,1,1)), but the size 1 dimensions do make a difference in many operations. – hpaulj Oct 10 '19 at 18:40

0 Answers0