# How to calculate the sum of all elements in a numpy matrix ?

Published: January 10, 2024

Tags: Python; Numpy;

## Introduction

Numpy provides efficient methods for performing calculations on multi-dimensional arrays, also known as matrices. One common task when working with matrices is calculating the sum of all elements in a matrix.

Matrices are essentially multi-dimensional arrays, with rows and columns representing different dimensions. In numpy, these matrices can have any number of dimensions, making it a powerful tool for working with complex data. This article will explore the process of calculating the sum of elements in a numpy matrix.

## Creating a numpy matrix

Let's create a numpy matrix, for instance, an array of dimensions (2,3) populated with random integers ranging from 0 to 5:

````import numpy as np`

`np.random.seed(42)`

`data = np.random.randint(0,5,(2,3))`
```

Ouput

````array([[3, 4, 2],`
`       [4, 4, 1]])`
```

Please note that in this instance, I utilized a random seed to consistently generate the same set of random numbers. This was done solely for educational purposes.

## Calculating the sum of all elements

Numpy provides the sum() function, which takes in an array as an argument and returns the sum of all elements in that array. This function works for both one-dimensional and multi-dimensional arrays.

````np.sum(data)`
```

The output of this code will be

````18`
```

, which is the sum of all elements in the matrix.

It's worth noting that using the following syntax

````data.sum()`
```

will yield the same outcome

````    18`
```

You may be wondering why we would need a library like numpy to perform such simple calculations. The answer lies in the efficient implementation of numpy functions. Unlike traditional for loops, numpy uses vectorization and broadcasting techniques to perform calculations on entire arrays at once, making it much faster and less memory-intensive.

Not only that, but numpy also has various built-in functions for statistical operations like mean, median, standard deviation, etc., which can be performed quickly and easily on large datasets.

## Specifying the axis along which we want to calculate the sum

Numpy also allows us to specify the axis along which we want to calculate the sum. For example, if we want to find the sum of elements along the rows, we can use axis=0, and for columns, we can use axis=1.

### Adding of elements along the rows

To calculate the sum of all elements along axis 0, we use the np.sum() function in Numpy and specify the axis as 0. This will return an array with the sum of each column in the matrix. For example:

````np.sum(data,axis=0)`
```

Output

````array([7, 8, 3])`
```

### Adding of elements along the columns

To calculate the sum of all elements along axis 1, we also use the np.sum() function in Numpy and specify the axis as 1. This will return an array with the sum of each rows in the matrix. For example:

````np.sum(data,axis=1)`
```

Output

````array([9, 9])`
```