user836026
user836026

Reputation: 11350

What is the difference between numpy array with dimension of size 1 and without that dimension

I'm might be a bit confused. But I wonder what is the difference between say x[2,3] and y[2,3,1] (same array but have extra dimension with size 1).

Are they the same or there is difference between them.

Upvotes: 2

Views: 244

Answers (1)

mozway
mozway

Reputation: 260735

Let's take a 2D example

# shape (2,)
a = np.array([0,1])
# shape (2,1)
b = np.array([[3],[4]])

You can consider a to be a single row with 2 columns (actually a 1D vector), and b array to be 2 rows with one column.

Let's try to add them:

a+a
# addition on a single dimension
# array([0, 2])

b+b
# also common dimensions
# array([[6],
#        [8]])

a+b
# different dimensions with one of common size
# addition will be broadcasted to generate a (2,2) shape
# array([[3, 5],
#        [4, 6]])

Upvotes: 2

Related Questions