17

What is the difference between these two numpy objects?

import numpy as np
np.array([[0,0,0,0]])
np.array([0,0,0,0])
Char
  • 1,635
  • 7
  • 27
  • 38

3 Answers3

33
In [71]: np.array([[0,0,0,0]]).shape
Out[71]: (1, 4)

In [72]: np.array([0,0,0,0]).shape
Out[72]: (4,)

The former is a 1 x 4 two-dimensional array, the latter a 4 element one-dimensional array.

Jan Christoph Terasa
  • 5,781
  • 24
  • 34
  • 1
    What does the notation (4,) mean? 4 rows and undefined number of columns? Why is the result of the second operation not (,4)? – bkr879 Jan 24 '17 at 15:01
  • 2
    The `shape` attribute returns a standard python `tuple`, where the numbers are the number of elements per dimension. So `(4,)` is a standard 1-element python `tuple`, with the element having the value `4`. `(,4)` is not a valid tuple. – Jan Christoph Terasa Jan 25 '17 at 13:03
  • 1
    I'm not sure but doesn't (4,) mean that there is just 4 rows and one column, ie, it is a one dimensional array? The second output of (1,4) would mean 1 row and 4 column, so shouldn't take mean the output has 4 dimensions as it has 4 columns? – Simon Jul 22 '17 at 07:03
  • The latter `(4,)` array does not have any "columns", since it is 1d. Having columns implies that it is 2d. – Jan Christoph Terasa Mar 23 '18 at 09:37
12

The difference between single and double brackets starts with lists:

In [91]: ll=[0,1,2]
In [92]: ll1=[[0,1,2]]
In [93]: len(ll)
Out[93]: 3
In [94]: len(ll1)
Out[94]: 1
In [95]: len(ll1[0])
Out[95]: 3

ll is a list of 3 items. ll1 is a list of 1 item; that item is another list. Remember, a list can contain a variety of different objects, numbers, strings, other lists, etc.

Your 2 expressions effectively make arrays from two such lists

In [96]: np.array(ll)
Out[96]: array([0, 1, 2])
In [97]: _.shape
Out[97]: (3,)
In [98]: np.array(ll1)
Out[98]: array([[0, 1, 2]])
In [99]: _.shape
Out[99]: (1, 3)

Here the list of lists has been turned into a 2d array. In a subtle way numpy blurs the distinction between the list and the nested list, since the difference between the two arrays lies in their shape, not a fundamental structure. array(ll)[None,:] produces the (1,3) version, while array(ll1).ravel() produces a (3,) version.

In the end result the difference between single and double brackets is a difference in the number of array dimensions, but we shouldn't loose sight of the fact that Python first creates different lists.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
4

When you defined an array with two brackets, what you were really doing was declaring an array with an array with 4 0's inside. Therefore, if you wanted to access the first zero you would be accessing your_array[0][0] while in the second array you would just be accessing your array[0]. Perhaps a better way to visualize it is

array: [
[0,0,0,0],
]

vs

array: [0,0,0,0]
Avi Mosseri
  • 1,258
  • 1
  • 18
  • 35
  • With `np.array`, it is better to index with `x[0, 0]`. `numpy` arrays have more powerful indexing methods than nested lists. – hpaulj Sep 26 '16 at 04:16
  • true but for someone asking this kind of a question I thought that this might be an easier way to explain it – Avi Mosseri Sep 26 '16 at 04:19