Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear authoritative explanation of numpy axis numbers?

Tags:

python

numpy

I am getting confused by contradictory explanations of what exactly the term axis means in numpy and how these constructs are numbered.

Here's one explanation:
Axes are defined for arrays with more than one dimension.
A 2-dimensional array has two corresponding axes:
the first running vertically downwards across rows (axis 0), and
the second running horizontally across columns (axis 1).

So, in this 3x4 matrix ...

>>> b = np.arange(12).reshape(3,4)
>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

(axis 0) is the 3 rows
(axis 1) is the 4 columns

So the rule might be ...

In an MxN matrix, (axis 0) is M and (axis 1) is N.

Is this correct?

So, in a 3 dimensional matrix AxBxC (axis 0) is A
(axis 1) is B
(axis 2) is C

Is this correct?

like image 933
Alex Ryan Avatar asked Feb 04 '16 20:02

Alex Ryan


People also ask

What is axis =- 1 mean in NumPy?

The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

How do axis work in NumPy?

Axes are defined for arrays with more than one dimension. A 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1). Fast element-wise operations, called ufuncs, operate on arrays.

What is squeeze () in Python?

squeeze() function is used when we want to remove single-dimensional entries from the shape of an array. Syntax : numpy.squeeze(arr, axis=None ) Parameters : arr : [array_like] Input array. axis : [None or int or tuple of ints, optional] Selects a subset of the single-dimensional entries in the shape.

What is Axis in NumPy 3D array?

An array with two dimensions has rows and columns. The rows and columns are the two axes of the array. We can ask Numpy to do operations over rows or columns, using the axis keyword argument.


1 Answers

Everything you said is correct, with the exception of

Axes are defined for arrays with more than one dimension.

Axes are also defined for one dimensional arrays - there is just one of them (i.e. axis 0).


One intuitive way to think about axes is to consider what happens when you apply a reduction operation over one axis, such as summation. For example, suppose I have some array x:

x = np.arange(60).reshape(3, 4, 5)

If I compute x.sum(0) I am "collapsing" x over the first dimension (i.e. axis 0), so I end up with a (4, 5) array. Likewise, x.sum(1) gives me a (3, 5) array and x.sum(2) gives me a (3, 4) array.

An integer index into a single axis of x will also give me an output with one fewer axis. For example, x[0, :, :] gives me the first "row" of x, which has shape (4, 5), x[:, 0, :] gives me the first "column" with shape (3, 5), and x[:, :, 0] gives me the first slice in the third dimension of x with shape (3, 4).

like image 199
ali_m Avatar answered Sep 27 '22 23:09

ali_m