Logo

Extra Help

 

An Explanation Of Numpy Axes

 

This lesson will go over NumPy axes.

It will define a NumPy axis. The guide will also clarify how axes work or how to use those with NumPy functions.

Axes in NumPy are comparable to axes in a Cartesian grid.

Axes are used in cartesian grid points, below is a simple Cartesian grid for reference.

 

extra200

Figure E.1. Sample Cartesian Grid.

 

The x - axis and y - axis are the two axes of a simple two-dimensional Cartesian plane.

These axes are primarily just Cartesian space directions (orthogonal directions).

Moreover, the position of a spot in Cartesian space can be ascertained by its position along each one of the axes.

As such, if we have a position (3, 6), we can say that it is 3 units along the x axis and units along the y axis.

 

extras201

Figure E.2. XY Position (3,6) on a Sample Cartesian Grid.

 

Numpy axes are really the directions along the columns and rows; NumPy arrays, such as coordinates, have axes.

 

extras202

Figure E.3. Sample Numpy array coordinate system.

 

The axes in the 2-dimensional NumPy array are the directions of the rows and columns. Axis '0' reflects the row orientation and is the "first" axis in a NumPy array. Axis '0' is the one that runs down along the rows.

 

extras20212

Figure E.4. Axis '0' runs along the rows, downwards.

 

Axis '1' is oriented along the columns and is the "Second" axis among the NumPy axes in NumPy array. Here Axis 1 runs horizontally across the columns.

-

extra20221

Figure E.5. Axis '1' runs along the columns, horizontally.

 

As a result, the array axes throughout NumPy are numbered.

These Axes are numbered from 0 to 1, which is comparable to index values in Python sequences. Some examples like lists as well as tuples, have an index affiliated with them.

An example of python list which includes some few capital letters:

 


List_Items = ['A','B','C','D']
List_Items.index('A')

Table E.1. Items in a list.

 

When we get the index value of first element 'A' we discover that this is at index location 0. The very first item in the list is A, however the index position is 0.

This is how all Python sequences operate. The index in just about any python sequence, such as a list, tuple, or string, begins at 0.

NumPy axis numbering is largely the same. They have been numbered from 0 to 1. As a result, the "1st" axis is actually "axis 0" and "Axis 1" is the "2nd" axis, and so on.

This structure of Numpy Axes is very essential to understand.

Following will be some case studies which are essential as they will assist you gain an understanding of how NumPy axes work once combined with NumPy functions. But remember to include 'import numpy as np'.This adds the NumPy bundle to your work environment.

Concentrate to what the axis parameter controls for every function. Here we will demo with numpy.sum() or np.sum().

 

Table E.2. Demonstration of numpy sum function.


>>> np.sum([0.5, 1.5])
2.0

>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1

>>> np.sum([[0, 1], [0, 5]])
6

>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])

>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])

>>> np.sum([[0, 1], [np.nan, 5]], where=[False, True], axis=1)
array([1., 5.])


Codeblock E.1. Various function executions of numpy sum.

 

The axis parameter in np.sum() specifies which axis would be aggregated.

In other words, the axis parameter determines which axes would be collapsed.. Statistical functions such as sum(), mean(), min(), median(), and the others aggregate your data.

Summation tabulates your relevant data. It combines several values into a single value.

When you use np.sum() on a 2-d array with axis parameter, it will collapse your 2-d array to just a 1-d array.

The information will be collapsed as well as the dimensionality reduced. The axis you stipulate is the one that is broken down.

numpy sum on axis 0 In this case, we'll use the NumPy sum function with axis = 0.

 

 

Codeblock E.2. Numpy sum function with axis=0.

 

Array2D is a 2-dimensional array with values ranging from 0 to 8 in a 2-by-4 format. Then, with axis = 0, let's use NumPy sum function.

Once we established axis to 0, the function summarizes the columns. As a consequence, a new NumPy array with the sum of every column is created.

 

extra205

Figure E.6. Conceptual sum along Axis '0' running along axis 0, Vertical, this corroborates with the code block above.

 

We are not summing all across rows once we set axis = 0. When we set axis = 0, we are adding up the information and collapsing the rows... We compress axis 0.

Now if we use the NumPy sum function on our array with axis = 1, we get the following result.

 

 

Codeblock E.3. Numpy sum function with axis=1.

 

Again, the axis parameter is used with the sum() function to specify which axis is collapsed during the summing up process.

As previously stated in this guide, axis 1 makes reference to the horizontal direction across the columns. That is, during the summation, the code np.sum(Array2D, axis = 1) begins to collapse the columns.

 

extra206

Figure E.7. Conceptual sum along Axis '1' running along axis 1, Horizontal, this corroborates with the code block above.

 

Beginners expect  that just by setting axis = 1, NumPy will sum down the columns, but that isn't the case. Here it collapes axis 1.

Same can be said for numpy mean.

 

 

Codeblock E.4. Numpy mean function demonstration with axis=0,1.

 

Another example can be seen with numpy concatenate for two arrays.


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

>>> Array2 = np.array([[7,8,9],[10,11,12]])

>>>Array1
array([[1,2,3][4,5,6]])

>>>Array2
array([[7,8,9][10,11,12]])


Table E.3. Numpy arrays Array1 and Array2.

 

If we use Numpy concatenate with axis=0.

 

extra207

Figure E.8. Concatenating Array1 and Array2 along axis=0.

 

So, by setting axis = 0, we instruct the concatenate function to stack the two arrays all along rows. We specify that arrays must be concatenated along axis 0.

 

 

Codeblock E.5. Numpy concatenate function demonstration with axis=0.

 

We inform the concatenate function to pile the two arrays along rows by setting axis = 0. We specify that the arrays should be concatenated along axis 0.

 

extra207

Figure E.9. Concatenating Array1 and Array2 along axis=1.

 

If we use Numpy concatenate with axis=1.

 

 

Codeblock E.6. Numpy concatenate function demonstration with axis=1.

 

Because these arrays are two dimensional, they have two axes, axis 0 as well as axis 1. Axis 1 is the axis which runs horizontally from across columns of the NumPy arrays.

If we use NumPy concatenate with axis = 1, we are instructing the concatenate() function to incorporate these arrays along axis 1.

 

extra208

Figure E.10. Concatenating Array1 and Array2 along axis=0.

 

That is, we are instructing concatenate() to merge them horizontally because axis 1 runs horizontally from across columns.

However a moment of warning for since its not same for 1 dimensional arrays. This is due to the fact that they have only one axis which starts with axis 0.

Hence, in  1D array , there only be one axis or axis 0.

 

extra210

Figure E.11. Concatenating 1D arrays 'Array1' ,'Array2' along axis=1 will result in errors.

 

Sample example below has concatenation of 1D arrays.

 


>>> Array1 = np.array([1,2,3])

>>> Array1
array([1,2,3])

>>> Array2 = np.array([4,5,6])


>>> Array2
array([4,5,6])

Table E.4. Numpy arrays Array1 and Array2 which are 1D arrays.

 

Codeblock E.7. Numpy concatenate function demonstration with axis=0.

 

The arrays have been horizontally concatenated. This varies from how the function operates on two-dimensional arrays.

If we use np.concatenate() on 2-dimensional arrays with axis = 0, the arrays are concatenated vertically.

NumPy concatenate joins these arrays along axis 0.

The problem is that in 1-dimensional arrays, axis 0 does not point "downward" as it does in 2-dimensional arrays.

You will get an error if you try to concatenate 1D array with an axis = 1.  If users attempt to concatenate such arrays on axis 1, you'll run into difficulties.

 


>>> np.concatenate([Array1,Array2],axis=1)

# we get the error

IndexError: axis 1 out of bounds [0,1]

Table E.5. Concatenation of arrays 'Array1'  & 'Array2' we get an error .

 

Codeblock E.8. Numpy concatenation function demonstration with axis=1 on a 1D array.

 

Arrays 1 and 2 are one-dimensional arrays thus, as a result, they lack an axis 1.

We're attempting to use np.concatenate() on an axis that isn't present in these arrays. As a result, the script produces an error.

When trying to work with 1-d arrays including using NumPy functions with the axis parameter, the script can produce perplexing results.

If you comprehend how NumPy axes operate, the results make a lot of sense. However, if you don't recognize NumPy array axes, the outcomes will most likely be perplexing.

But, before you begin working with NumPy array axes, make sure you thoroughly understand them!

 

 


________________________________________________________________________________________________________________________________
Footer
________________________________________________________________________________________________________________________________

Copyright © 2022-2023. Anoop Johny. All Rights Reserved.