user10523691
user10523691

Reputation: 41

What is difference between parenthesis and bracket in Numpy?

Could somebody explain the difference between () and [] operations in Numpy?

For example, I have run the following codes:

import numpy as np

x1 = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)
print(x1)
print(type(x1))
x2 = np.array([[2, 9], [1, 5], [3, 6]], dtype=float)
print(x2)
print(type(x2))

y1 = np.array(([2, 9]), dtype=float)
print(y1)
print(type(y1))

y2 = np.array([[2, 9]], dtype=float)
print(y2)
print(type(y2))

Both x1 and x2 have the same data and type, but y1 and y2 are different. I think that y1 and y2 should be same. Could some one explain the reason why y1 and y2 are different ?

Upvotes: 3

Views: 3798

Answers (4)

Khan
Khan

Reputation: 1498

to my understanding, entering a tuple is in np.array is also termed as a list, for example

a=np.zeros((3,3))

array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])

same as if we input a list in np.array for zero's initialization

a=np.zeros([3,3])

array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])

but if you initialize using the same pattern in simple python it will count them as tuple and list respectively.

a=([2,6],[4,5])

([2, 6], [4, 5])

class 'tuple'

b=[[2,6],[4,5]]

type(b)

class 'list'

Upvotes: 1

Bal Krishna Jha
Bal Krishna Jha

Reputation: 7206

That additional bracket doesn't make any change. It is effectively [2,9].

There is also another example you can look :

np.random.randn((((((((((((((((((((((1))))))))))))))))))))))

It is effectively :

np.random.randn(1)

This is not the speciality of Numpy. Additional parentheses can be added almost anywhere as long as it contain single expression.

In contrast if Python find any comma-seperated value inside a parentheses or empty parentheses then it will try to convert to tuple object.

Upvotes: 1

bruno desthuilliers
bruno desthuilliers

Reputation: 77892

This has nothing to do with Numpy actually.

This:

([2, 9], [1, 5], [3, 6])

is a tuple of 3 lists.

This:

[[2, 9], [1, 5], [3, 6]]

is a list of 3 lists.

Since tuples and lists are both ordered sequences, numpy treat them the same.

Now this:

([2, 9])

is actually a list of two integers - the parens only force evaluation order of the expression - so what numpy gets is really

[2, 9] 

while this:

[[2, 9]]

is a list of one list (of two integers). So of course numpy won't treat them the same since they really are different.

The point here is that what makes a tuple is not the parens but the coma, so the first example:

([2, 9], [1, 5], [3, 6])

is really:

[2, 9], [1, 5], [3, 6]

TL;DR:

for the third example, you want:

([2, 9],)

not

([2, 9])

Upvotes: 1

Matphy
Matphy

Reputation: 1124

Arrays y1 and y2 have different shapes. First one is one-dimensional, the second one is 2-dimensional. Parentheses around [2, 9] have no meaning (because there is only one element inside it and comma). See below and the first comment as well.

y1 = np.array(([2, 9]), dtype=float)
y1.shape  # (2,)
y2 = np.array([[2, 9]], dtype=float)
y2.shape  # (1, 2)

Something about parentheses:

a = (3)
type(a)  # int
b = (3, )
type(b)  # tuple
c = (3, 4)
type(c)  # tuple

Upvotes: 1

Related Questions