In this tutorial, we will look at how to get the sum of values of a numpy array. We will also look at specific use-cases like summing along an axis for higher dimensional arrays.
How to sum a numpy array?
You can use the numpy sum()
function to sum elements of an array. The following is the syntax for a range of different use-cases:
# arr is a numpy array # sum of all values arr.sum() # sum of each row (for 2D array) arr.sum(axis=1) # sum of each column (for 2D array) arr.sum(axis=0) # sum along a specific axis, n arr.sum(axis=n)
You can also specify the axis to sum the numpy array along with the axis
parameter (see the examples below)
Let’s now look at some of the use-cases of using the numpy sum()
function.
Sum of all elements in the array
Use the numpy sum()
function without any parameters to get the sum total of all values inside the array.
Let’s create a numpy array and illustrate its usage.
import numpy as np # create an array arr = np.array([2, 0, 1, 3]) # sum of array values total = arr.sum() print(total)
Output:
6
We get 6 as the output which is the sum of all values in the above array arr: 2+0+1+3
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
You can use the above syntax to sum values in higher dimensional numpy arrays as well. For example, let’s get the total of all elements in a 2D numpy array –
# create a 2D numpy array arr = np.array([[1, 0, 0], [2, 1, 1]]) # sum of array values total = arr.sum() # display the array and the sum print(arr) print("Sum:", total)
Output:
[[1 0 0] [2 1 1]] Sum: 5
Here, we created a 2D array and then calculated its sum. You can see that we get the sum of all the elements in the above 2D array with the same syntax. This can be extended to higher-dimensional numpy arrays as well.
Sum of every row in a 2D array
To get the sum of each row in a 2D numpy array, pass axis=1
to the sum()
function. This argument tells the function of the axis along which the elements are to be summed. Let’s use it to get the sum of each row in the array arr.
# create a 2D numpy array arr = np.array([[1, 0, 0], [2, 1, 1]]) # sum of each row row_totals = arr.sum(axis=1) # display the array and the sum print(arr) print("Sum of each row:", row_totals)
Output:
[[1 0 0] [2 1 1]] Sum of each row: [1 4]
We get the sum of each row with axis=1. The first row sums to 1 and the second-row sums to 4. The result is returned as a numpy array.
Sum of every column in a 2D array
To get the sum of each column in a 2D numpy array, pass axis=0
to the sum()
function. This argument tells the function of the axis along which the elements are to be summed. Let’s use it to get the sum of each column in the array arr.
# create a 2D numpy array arr = np.array([[1, 0, 0], [2, 1, 1]]) # sum of each column col_totals = arr.sum(axis=0) # display the array and the sum print(arr) print("Sum of each column:", col_totals)
Output:
[[1 0 0] [2 1 1]] Sum of each column: [3 1 1]
The resulting array [3, 1, 1]
contains the sum of values in each column. That is, in the above example – 1+2, 0+1, and 0+1.
The numpy sum() function also has additional parameters, for example, to specify the data type of the output, etc. For more, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on numpy arrays –
- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()