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.

## Tutorial Overview

This tutorial is divided into three parts; they are:

## NumPy Array With Rows and Columns

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:

…

# define data as a list

data = [[1,2,3], [4,5,6]]

… # define data as a list data = [[1,2,3], [4,5,6]] |

A NumPy array allows us to define and operate upon vectors and matrices of numbers in an efficient manner, e.g. 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:

…

# convert to a numpy array

data = asarray(data)

… # convert to a numpy array data = asarray(data) |

We can print the array directly and expect to see two rows of numbers, where each row has three numbers or columns.

…

# summarize the array content

print(data)

… # summarize the array content print(data) |

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.

…

# summarize the array shape

print(data.shape)

… # summarize the array shape print(data.shape) |

Tying this all together, a complete example is listed below.

# create and summarize a numpy array

from numpy import asarray

# define data as a list

data = [[1,2,3], [4,5,6]]

# convert to a numpy array

data = asarray(data)

# summarize the array content

print(data)

# summarize the array shape

print(data.shape)

# create and summarize a numpy array from numpy import asarray # define data as a list data = [[1,2,3], [4,5,6]] # convert to a numpy array data = asarray(data) # summarize the array content print(data) # summarize the array shape print(data.shape) |

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.

## Rows and Columns of Data in NumPy Arrays

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.g. shape[0]. 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.g. the complete first row in our matrix.

The example below enumerates all rows in the data and prints each in turn.

# enumerate rows in a numpy array

from numpy import asarray

# define data as a list

data = [[1,2,3], [4,5,6]]

# convert to a numpy array

data = asarray(data)

# step through rows

for row in range(data.shape[0]):

print(data[row, :])

# enumerate rows in a numpy array from numpy import asarray # define data as a list data = [[1,2,3], [4,5,6]] # convert to a numpy array data = asarray(data) # step through rows for row in range(data.shape[0]): print(data[row, :]) |

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.g. shape[1].

The example below demonstrates this by enumerating all columns in our matrix.

# enumerate columns in a numpy array

from numpy import asarray

# define data as a list

data = [[1,2,3], [4,5,6]]

# convert to a numpy array

data = asarray(data)

# step through columns

for col in range(data.shape[1]):

print(data[:, col])

# enumerate columns in a numpy array from numpy import asarray # define data as a list data = [[1,2,3], [4,5,6]] # convert to a numpy array data = asarray(data) # step through columns for col in range(data.shape[1]): print(data[:, col]) |

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.

## NumPy Array Operations By Row and Column

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:

- axis=None: Apply operation array-wise.
- axis=0: Apply operation column-wise, across all rows for each column.
- axis=1: Apply operation row-wise, across all columns for each row.

Let’s make this concrete with a worked example.

We will sum values in our array by each of the three axes.

Axis=None Array-Wise Operation

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.

…

# sum data by array

result = data.sum(axis=None)

… # sum data by array result = data.sum(axis=None) |

The example below demonstrates summing all values in an array, e.g. an array-wise operation.

# sum values array-wise

from numpy import asarray

# define data as a list

data = [[1,2,3], [4,5,6]]

# convert to a numpy array

data = asarray(data)

# summarize the array content

print(data)

# sum data by array

result = data.sum(axis=None)

# summarize the result

print(result)

# sum values array-wise from numpy import asarray # define data as a list data = [[1,2,3], [4,5,6]] # convert to a numpy array data = asarray(data) # summarize the array content print(data) # sum data by array result = data.sum(axis=None) # summarize the result print(result) |

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.

Axis=0 Column-Wise Operation

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.

…

# sum data by column

result = data.sum(axis=0)

… # sum data by column result = data.sum(axis=0) |

For example, given our data with two rows and three columns:

Data = [[1, 2, 3],

4, 5, 6]]

Data = [[1, 2, 3], 4, 5, 6]] |

We expect a sum column-wise with axis=0 will result in three values, one for each column, as follows:

- Column 1: 1 + 4 = 5
- Column 2: 2 + 5 = 7
- Column 3: 3 + 6 = 9

The example below demonstrates summing values in the array by column, e.g. a column-wise operation.

# sum values column-wise

from numpy import asarray

# define data as a list

data = [[1,2,3], [4,5,6]]

# convert to a numpy array

data = asarray(data)

# summarize the array content

print(data)

# sum data by column

result = data.sum(axis=0)

# summarize the result

print(result)

# sum values column-wise from numpy import asarray # define data as a list data = [[1,2,3], [4,5,6]] # convert to a numpy array data = asarray(data) # summarize the array content print(data) # sum data by column result = data.sum(axis=0) # summarize the result print(result) |

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.

[[1 2 3]

[4 5 6]]

[5 7 9]

Axis=1 Row-Wise Operation

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.

…

# sum data by row

result = data.sum(axis=1)

… # sum data by row result = data.sum(axis=1) |

For example, given our data with two rows and three columns:

Data = [[1, 2, 3],

4, 5, 6]]

Data = [[1, 2, 3], 4, 5, 6]] |

We expect a sum row-wise with axis=1 will result in two values, one for each row, as follows:

- Row 1: 1 + 2 + 3 = 6
- Row 2: 4 + 5 + 6 = 15

The example below demonstrates summing values in the array by row, e.g. a row-wise operation.

# sum values row-wise

from numpy import asarray

# define data as a list

data = [[1,2,3], [4,5,6]]

# convert to a numpy array

data = asarray(data)

# summarize the array content

print(data)

# sum data by row

result = data.sum(axis=1)

# summarize the result

print(result)

# sum values row-wise from numpy import asarray # define data as a list data = [[1,2,3], [4,5,6]] # convert to a numpy array data = asarray(data) # summarize the array content print(data) # sum data by row result = data.sum(axis=1) # summarize the result print(result) |

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.

[[1 2 3]

[4 5 6]]

[ 6 15]

We now have a concrete idea of how to set axis appropriately when performing operations on our NumPy arrays.

## Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Tutorials

APIs

## Summary

In this tutorial, you discovered how to access and operate on NumPy arrays by row and by column.

Specifically, you learned:

- 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.

Do you have any questions?

Ask your questions in the comments below and I will do my best to answer.

Covid Abruzzo Basilicata Calabria Campania Emilia Romagna Friuli Venezia Giulia Lazio Liguria Lombardia Marche Molise Piemonte Puglia Sardegna Sicilia Toscana Trentino Alto Adige Umbria Valle d’Aosta Veneto Italia Agrigento Alessandria Ancona Aosta Arezzo Ascoli Piceno Asti Avellino Bari Barletta-Andria-Trani Belluno Benevento Bergamo Biella Bologna Bolzano Brescia Brindisi Cagliari Caltanissetta Campobasso Carbonia-Iglesias Caserta Catania Catanzaro Chieti Como Cosenza Cremona Crotone Cuneo Enna Fermo Ferrara Firenze Foggia Forlì-Cesena Frosinone Genova Gorizia Grosseto Imperia Isernia La Spezia L’Aquila Latina Lecce Lecco Livorno Lodi Lucca Macerata Mantova Massa-Carrara Matera Messina Milano Modena Monza e della Brianza Napoli Novara Nuoro Olbia-Tempio Oristano Padova Palermo Parma Pavia Perugia Pesaro e Urbino Pescara Piacenza Pisa Pistoia Pordenone Potenza Prato Ragusa Ravenna Reggio Calabria Reggio Emilia Rieti Rimini Roma Rovigo Salerno Medio Campidano Sassari Savona Siena Siracusa Sondrio Taranto Teramo Terni Torino Ogliastra Trapani Trento Treviso Trieste Udine Varese Venezia Verbano-Cusio-Ossola Vercelli Verona Vibo Valentia Vicenza Viterbo