shivdutt Vishwakarma
shivdutt Vishwakarma

Reputation: 11

Why would we expand a numpy array along axis=0?

Can anyone explain the condition in which one would require to reshape along axis=0? Please see the example below, with a given numpy array:

a=np.array([1,2,3,4,5,6])
[1,2,3,4,5,6]

(Reshaping follows below)

a1 = np.expand_dims(a, axis=0)
[[1,2,3,4,5,6]]

Upvotes: 1

Views: 364

Answers (1)

Yang Yushi
Yang Yushi

Reputation: 765

The expansion typically happens when we are using a function, which performs operations on an (m, n) array, to process a special case where m = 1.

If the shape of the given data is (n,) we have to expand_dims along the first axis so that the shape is (1, n).

Some functions are nice enough to take special care of the (n,) situation. But sometimes we have to do the conversion, (n,) → (1, n), ourselves.

Upvotes: 1

Related Questions