How to Set Axis for Rows and Columns in NumPy
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:
- How to define NumPy arrays with rows and columns of data.
- How to access values in NumPy arrays by row and column indexes.
- How to perform operations on NumPy arrays by row and column axis.
Let’s get started.