-6

In following code, shape of size & randm is same only, 1-D Array (3,) Then why size=(3, 3, 2) & randm=[-1.10343097 -1.31819984 0.20597956]

Q.1 Why different bracket () vs [] Q.2 What is significant of each bracket () & []

 image = np.array([[[ 0.67826139,  0.29380381],
        [ 0.90714982,  0.52835647],
        [ 0.4215251 ,  0.45017551]],

       [[ 0.92814219,  0.96677647],
        [ 0.85304703,  0.52351845],
        [ 0.19981397,  0.27417313]],

       [[ 0.60659855,  0.00533165],
        [ 0.10820313,  0.49978937],
        [ 0.34144279,  0.94630077]]])

size = np.shape(image)
print (str (size))
print (np.shape(size))

randm = np.random.randn(3)
print (randm)
print (np.shape(randm)) 

Output-

(3, 3, 2)
(3,)
[-1.10343097 -1.31819984  0.20597956]
(3,)
Maulik
  • 1
  • 2
    () -> This is a tuple data structure, [] -> This is a list – Ezio Jan 16 '18 at 04:32
  • 1
    An even better duplicate: [Python: what is the difference between (1,2,3) and \[1,2,3\], and when should I use each?](https://stackoverflow.com/questions/1983/python-what-is-the-difference-between-1-2-3-and-1-2-3-and-when-should-i-us) – Mateen Ulhaq Jan 16 '18 at 04:40
  • That literally shows up if you copy paste your title into google... o_o – Mateen Ulhaq Jan 16 '18 at 04:41
  • 2
    Possible duplicate of [Python: what is the difference between (1,2,3) and \[1,2,3\], and when should I use each?](https://stackoverflow.com/questions/1983/python-what-is-the-difference-between-1-2-3-and-1-2-3-and-when-should-i-us) – Praveen Jan 16 '18 at 07:48

3 Answers3

1

image is a multidimensional numpy array. It is defined via a nested list of lists (the brackets and commas).

image.shape is a tuple, and so displays with ().

The other answers, and the supposed duplicate, focus on Python's distinction between lists and tuples. But this needs a numpy focused answer.

A simple 3d array:

In [244]: x = np.array([[[1,2],[3,4]]])
In [245]: x
Out[245]: 
array([[[1, 2],
        [3, 4]]])

You can get the shape as a property, or via a function call

In [246]: x.shape
Out[246]: (1, 2, 2)
In [247]: np.shape(x)
Out[247]: (1, 2, 2)

But the shape does not have a shape property itself. len(x.shape) would work, since it is a tuple.

In [248]: (x.shape).shape
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-248-b00338a7f4bf> in <module>()
----> 1 (x.shape).shape

AttributeError: 'tuple' object has no attribute 'shape'

np.shape(...shape) is confusing. np.shape() will first turn its input into an array (if it isn't already) and return the shape of that:

In [249]: np.shape(x.shape)
Out[249]: (3,)

So I wouldn't normally take this shape of a shape. However it does demonstrate a key point. (3,) is a 1 element tuple. The , is important. The shape tuple for an 0d array is ().

The next part makes a 3 element array

In [250]: np.random.randn(3)
Out[250]: array([ 2.06265058,  0.87906775, -0.96525837])
In [251]: _.shape
Out[251]: (3,)
In [252]: print(__)
[ 2.06265058  0.87906775 -0.96525837]

Again, a (3,) shape tuple. The str display of an array uses the brackets, but omits the commas. That helps distinguish it from a regular list.

(There is also an array type that displays with (), a structured array. But that's a more advanced topic.)

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

The () notation means that the data type is of tuple while the [] denotes a list.

Additionally tuples use less memory and are immutable while lists are mutable.

Mike Tung
  • 4,735
  • 1
  • 17
  • 24
0
(1,2,3) -Tuple
[1,2,3] -List

Read This for more information about tuple and list

Rohit-Pandey
  • 2,039
  • 17
  • 24