NumPy arrays provide a fast and efficient way to store and manipulate data in Python.
They are particularly useful for representing data as vectors and matrices in machine learning.
Data in NumPy arrays can be accessed directly via column and row indexes, and this is reasonably straightforward.
Nevertheless, sometimes we must perform operations on arrays of data such as sum or mean of values by row or column and this requires the axis of the operation to be specified.
Unfortunately, the column-wise and row-wise operations on NumPy arrays do not match our intuitions gained from row and column indexing, and this can cause confusion for beginners and seasoned machine learning practitioners alike.
Specifically, operations like sum can be performed column-wise using axis=0 and row-wise using axis=1.
In this tutorial, you will discover how to access and operate on NumPy arrays by row and by column.
After completing this tutorial, you will know:Let’s get started.
How to Set NumPy Axis for Rows and Columns in PythonPhoto by Jonathan Cutrer, some rights reserved.
This tutorial is divided into three parts; they are:Before we dive into the NumPy array axis, let’s refresh our knowledge of NumPy arrays.
Typically in Python, we work with lists of numbers or lists of lists of numbers.
For example, we can define a two-dimensional matrix of two rows of three numbers as a list of numbers as follows:A NumPy array allows us to define and operate upon vectors and matrices of numbers in an efficient manner, e.
a lot more efficient than simply Python lists.
NumPy arrays are called NDArrays and can have virtually any number of dimensions, although, in machine learning, we are most commonly working with 1D and 2D arrays (or 3D arrays for images).
For example, we can convert our list of lists matrix to a NumPy array via the asarray() function:We can print the array directly and expect to see two rows of numbers, where each row has three numbers or columns.
We can summarize the dimensionality of an array by printing the “shape” property, which is a tuple, where the number of values in the tuple defines the number of dimensions, and the integer in each position defines the size of the dimension.
For example, we expect the shape of our array to be (2,3) for two rows and three columns.
Tying this all together, a complete example is listed below.
Running the example defines our data as a list of lists, converts it to a NumPy array, then prints the data and shape.
We can see that when the array is printed, it has the expected shape of two rows with three columns.
We can then see that the printed shape matches our expectations.
For more on the basics of NumPy arrays, see the tutorial:So far, so good.
But how do we access data in the array by row or column? More importantly, how can we perform operations on the array by-row or by-column?Let’s take a closer look at these questions.
The “shape” property summarizes the dimensionality of our data.
Importantly, the first dimension defines the number of rows and the second dimension defines the number of columns.
For example (2,3) defines an array with two rows and three columns, as we saw in the last section.
We can enumerate each row of data in an array by enumerating from index 0 to the first dimension of the array shape, e.
We can access data in the array via the row and column index.
For example, data[0, 0] is the value at the first row and the first column, whereas data[0, :] is the values in the first row and all columns, e.
the complete first row in our matrix.
The example below enumerates all rows in the data and prints each in turn.
As expected, the results show the first row of data, then the second row of data.
We can achieve the same effect for columns.
That is, we can enumerate data by columns.
For example, data[:, 0] accesses all rows for the first column.
We can enumerate all columns from column 0 to the final column defined by the second dimension of the “shape” property, e.
The example below demonstrates this by enumerating all columns in our matrix.
Running the example enumerates and prints each column in the matrix.
Given that the matrix has three columns, we can see that the result is that we print three columns, each as a one-dimensional vector.
That is column 1 (index 0) that has values 1 and 4, column 2 (index 1) that has values 2 and 5, and column 3 (index 2) that has values 3 and 6.
It just looks funny because our columns don’t look like columns; they are turned on their side, rather than vertical.
Now we know how to access data in a numpy array by column and by row.
So far, so good, but what about operations on the array by column and array? That’s next.
We often need to perform operations on NumPy arrays by column or by row.
For example, we may need to sum values or calculate a mean for a matrix of data by row or by column.
This can be achieved by using the sum() or mean() NumPy function and specifying the “axis” on which to perform the operation.
We can specify the axis as the dimension across which the operation is to be performed, and this dimension does not match our intuition based on how we interpret the “shape” of the array and how we index data in the array.
As such, this causes maximum confusion for beginners.
That is, axis=0 will perform the operation column-wise and axis=1 will perform the operation row-wise.
We can also specify the axis as None, which will perform the operation for the entire array.
In summary:Let’s make this concrete with a worked example.
We will sum values in our array by each of the three axes.
Setting the axis=None when performing an operation on a NumPy array will perform the operation for the entire array.
This is often the default for most operations, such as sum, mean, std, and so on.
The example below demonstrates summing all values in an array, e.
an array-wise operation.
Running the example first prints the array, then performs the sum operation array-wise and prints the result.
We can see the array has six values that would sum to 21 if we add them manually and that the result of the sum operation performed array-wise matches this expectation.
Setting the axis=0 when performing an operation on a NumPy array will perform the operation column-wise, that is, across all rows for each column.
For example, given our data with two rows and three columns:We expect a sum column-wise with axis=0 will result in three values, one for each column, as follows:The example below demonstrates summing values in the array by column, e.
a column-wise operation.
Running the example first prints the array, then performs the sum operation column-wise and prints the result.
We can see the array has six values with two rows and three columns as expected; we can then see the column-wise operation result in a vector with three values, one for the sum of each column matching our expectation.
Setting the axis=1 when performing an operation on a NumPy array will perform the operation row-wise, that is across all columns for each row.
For example, given our data with two rows and three columns:We expect a sum row-wise with axis=1 will result in two values, one for each row, as follows:The example below demonstrates summing values in the array by row, e.
a row-wise operation.
Running the example first prints the array, then performs the sum operation row-wise and prints the result.
We can see the array has six values with two rows and three columns as expected; we can then see the row-wise operation result in a vector with two values, one for the sum of each row matching our expectation.
We now have a concrete idea of how to set axis appropriately when performing operations on our NumPy arrays.
This section provides more resources on the topic if you are looking to go deeper.
In this tutorial, you discovered how to access and operate on NumPy arrays by row and by column.
Specifically, you learned:Do you have any questions? Ask your questions in the comments below and I will do my best to answer.
by writing lines of code in pythonDiscover how in my new Ebook: Linear Algebra for Machine LearningIt provides self-study tutorials on topics like: Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more.
Skip the Academics.