Hello programmers, in today’s article, we will discuss and explain the Numpy axis in python. Understanding the use of axes in a Numpy array is not very simple. But let’s start with this. The Numpy axis is very similar to axes in a cartesian coordinate system. Before we start with how Numpy axes, let me familiarize you with the Numpy axis concept a little more.
Numpy Axis is a type of direction through which the iteration starts. Every operation in numpy has a specific iteration process through which the operation proceeds. Moreover, there are two types of the iteration process: Column order and Fortran order. Column order helps through the column axis, and Fortran order helps through the row axis.
Numpy Axis Directions
Axis 0 (Direction along Rows) – Axis 0 is called the first axis of the Numpy array. This axis 0 runs vertically downward along the rows of Numpy multidimensional arrays, i.e., performs column-wise operations.
Axis 1 (Direction along with columns) – Axis 1 is called the second axis of multidimensional Numpy arrays. As a result, Axis 1 sums horizontally along with the columns of the arrays. It performs row-wise operations.
NOTE: The above Numpy axis description is only for 2D and multidimensional arrays. It works differently for 1D arrays discussed later in this article.
Ways of Implementing Numpy axis in Python
- Rows and columns for Numpy Array.
- Numpy axis for the sum.
- Numpy axis to concatenate.
- ID Array Numpy axis.
Rows and Columns for Numpy Array
From numpy import asarray data = [[1,2,3], [4,5,6]] # convert to a numpy array data = asarray(data) # step through rows for row in range(data.shape): print(data[row, :]) #step through columns for col in range(data.shape): print(data[:, col])
[1 2 3] [4 5 6] [1 4] [2 5] [3 6]
In the above example, we are enumerating each row and column’s data. In other words, we are achieving this by accessing them through their index. The data[0, 0] gives the value at the first row and first column. Moreover, data[0, :] gives the values in the first row and all columns. E.g., the complete first row in our matrix. Similarly, data[:, 0] accesses all rows for the first column. Above all, printing the rows of the array, the Numpy axis is set to 0, i.e., data.shape. Similarly, the Numpy axis is set to 1 while enumerating the columns.
Numpy Axis in Python for Sum
When we use the numpy sum() function on a 2-d array with the
axis parameter, it collapses the 2-d array down to a 1-d array. It collapses the data and reduces the number of dimensions. But which axis will collapse to return the sum depends on whether we set the axis to 0 or 1. Let’s have a look at the following examples for a better understanding.
Numpy sum with axis 0
import numpy as np #creating Numpy Array np_array_2d = np.arange(0, 6).reshape([2,3]) print(np_array_2d) a = np.sum(np_array_2d, axis = 0) print(a)
[[0 1 2] [3 4 5]] array([3, 5, 7])
In the above example, we create an array of size(2,3), i.e., two rows and three columns. When the axis is set to 0. Immediately, the function actually sums down the columns. The result is a new NumPy array that contains the sum of each column. As discussed earlier, Axis 0 is the direction along rows but performs column-wise operations. Axis set to 0 refers to aggregating the data. Therefore we collapse the rows and perform the sum operation column-wise. Thus, the sum() function’s axis parameter represents which axis is to be collapsed.
Numpy Sum With Axis 1
import numpy as np #creating Numpy Array np_array_2d = np.arange(0, 6).reshape([2,3]) print(np_array_2d) a = np.sum(np_array_2d, axis = 1) print(a)
As we know, axis 1, according to the axis convention. For instance, it refers to the direction along columns performing operations over rows. For the sum() function. The axis parameter is the axis to be collapsed. Hence in the above example. For instance, the axis is set to 1 in the sum() function collapses the columns and sums down the rows.
Numpy Axis for Concatenation of two Arrays
axis the parameter we use with the numpy concatenate() function defines the axis along which we stack the arrays. We get different types of concatenated arrays depending upon whether the axis parameter value is set to 0 or 1. In addition, to have a clearer understanding of what is said, refer to the below examples.
Concatenate Np arrays with axis 0
import numpy as np #arrays defined np_array_1 = np.array([[1,1,1],[1,1,1]]) np_array_2 = np.array([[2,2,2],[2,2,2]]) print(np_array_1) print(np_array_2) a = np.concatenate([np_array_1, np_array_2], axis = 0) print(a)
array([[1, 1, 1], [1, 1, 1]]) array([[2, 2, 2], [2, 2, 2]]) array([[1, 1, 1], [1, 1, 1], [2, 2, 2], [2, 2, 2]])
As already mentioned, the axis parameter in the ‘concatenate()’ function implies stacking the arrays. So when we set the axis to 0, the concatenate function stacks the two arrays along the rows. We’re specifying that we want concatenation of the arrays. The concatenation is done along axis 0, i.e., along the rows’ direction. Thus we get the output as an array stacked. And two constituent arrays along rows.
Concatenate Numpy arrays with axis 1
import numpy as np #arrays defined np_array_1 = np.array([[1,1,1],[1,1,1]]) np_array_2 = np.array([[2,2,2],[2,2,2]]) print(np_array_1) print(np_array_2) a = np.concatenate([np_array_1, np_array_2], axis = 1) print(a)
array([[1, 1, 1], [1, 1, 1]]) array([[2, 2, 2], [2, 2, 2]]) array([[1, 1, 1, 2, 2, 2], [1, 1, 1, 2, 2, 2]])
In the above example, the axis parameter is set to 1. For instance, we know, axis 1 specifies the direction along with columns. Above all this implies the numpy concatenate() function to combine two input arrays. After that, the concatenation is done horizontally along with the columns.
1D Array NP Axis in Python – Special Case
The numpy axes work differently for one-dimensional arrays. Most of the discussion we had in this article applies two-dimensional arrays with two axes – rows and columns. 1D arrays are different since it has only one axis. Numpy axes are numbered like Python indexes, i.e., they start at 0. Therefore in a 1D array, the first and only axis is axis 0. If we specify the axis parameter as 1 while working with 1D arrays. In addition, it returns an error.
Example to concatenate two 1D arrays with Np axis in python
import numpy as np #arrays defined np_array_1 = np.array([1,1,1]) np_array_2 = np.array([[2,2,2]) print(np_array_1) print(np_array_2) a = np.concatenate([np_array_1, np_array_2], axis = 0) b = np.concatenate([np_array_1, np_array_2], axis = 1) print(a) print(b)
[1 1 1] [2 2 2] array([1, 1, 1, 2, 2, 2]) IndexError: axis 1 out of bounds [0, 1)
As mentioned above, 1-dimensional arrays only have one axis – Axis 0. The function is working properly when the axis parameter is set to 1. It prints ‘a’ as a combined 1D array of the two input 1D arrays. In 1D arrays, axis 0 doesn’t point along the rows “downward” as it does in a 2-dimensional array. However, when the axis parameter is set to 1, it could not print ‘b’. In conclusion, it raised an index error stating axis 1 is out of bounds for one-dimensional arrays.
In conclusion, we can say in this article, we have looked into Numpy axes in python in great detail. Numpy axis in python is used to implement various row-wise and column-wise operations. Operations like numpy sum(), np mean() and concatenate() are achieved by passing numpy axes as parameters. We can also enumerate data of the arrays through their rows and columns with the numpy axis’s help. Also, the special case of the axis for one-dimensional arrays is highlighted. This must be kept in mind while implementing python programs.
However, if you have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.