0

When we initialize numpy arrays using np.zeros or np.ones, we may leave the last dimension unspecified.

The following are examples:

 x1 = np.zeros((4, ))
 x2 = np.zeros((4, 3, ))

But the following doesn't work

 x3 = np.zeros(( , 4))
 x4 = np.zeros(( 4, , ))

I'm not quite sure what the purpose of leaving the last dimension unspecified is. It seems like there is no difference between np.zeros((4, )) and np.zeros((4)), so I don't clearly understand the reason of leaving the last dimenion unspecified. Could anyone clarify on this?

chanwcom
  • 4,420
  • 8
  • 37
  • 49
  • That's not "leaving the last dimension unspecified". There **is no** second dimension in that array; `(4,)` is Python syntax for a length-1 tuple with single element `4`, and passing that to `numpy.zeros` produces a 1-dimensional array of length 4. – user2357112 Nov 26 '17 at 22:44
  • The docs say this parameter can be a `sequence` or integer. `(1,)` is a 1 element sequence. So is `[1]`. `1` is an integer. All 3 produce an array with shape `(1,)` (shape is always a tuple). Evidently there's some sort of internal tweaking of inputs to a common logical form. – hpaulj Nov 26 '17 at 23:26

0 Answers0