Tutorial

numpy.sum() in Python

Published on August 3, 2022
Default avatar

By Pankaj

numpy.sum() in Python

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python numpy sum() function is used to get the sum of array elements over a given axis.

Python numpy sum() function syntax

Python NumPy sum() method syntax is:

sum(array, axis, dtype, out, keepdims, initial)
  • The array elements are used to calculate the sum.
  • If the axis is not provided, the sum of all the elements is returned. If the axis is a tuple of ints, the sum of all the elements in the given axes is returned.
  • We can specify dtype to specify the returned output data type.
  • The out variable is used to specify the array to place the result. It’s an optional parameter.
  • The keepdims is a boolean parameter. If this is set to True, the axes which are reduced are left in the result as dimensions with size one.
  • The initial parameter specifies the starting value for the sum.

Python numpy sum() Examples

Let’s look at some of the examples of numpy sum() function.

1. Sum of All the Elements in the Array

If we pass only the array in the sum() function, it’s flattened and the sum of all the elements is returned.

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4],
     [5, 6]])

total = np.sum(array1)
print(f'Sum of all the elements is {total}')

Output: Sum of all the elements is 21

2. Sum of Array Elements Along the Axis

If we specify the axis value, the sum of elements along that axis is returned. If the array shape is (X, Y) then the sum along 0-axis will be of shape (1, Y). The sum along 1-axis will be of shape (1, X).

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4],
     [5, 6]])

total_0_axis = np.sum(array1, axis=0)
print(f'Sum of elements at 0-axis is {total_0_axis}')

total_1_axis = np.sum(array1, axis=1)
print(f'Sum of elements at 1-axis is {total_1_axis}')

Output:

Sum of elements at 0-axis is [ 9 12]
Sum of elements at 1-axis is [ 3  7 11]

3. Specifying Output Data Type of Sum

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4]])

total_1_axis = np.sum(array1, axis=1, dtype=float)
print(f'Sum of elements at 1-axis is {total_1_axis}')

Output: Sum of elements at 1-axis is [3. 7.]

4. Initial Value for the Sum

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4]])

total_1_axis = np.sum(array1, axis=1, initial=10)
print(f'Sum of elements at 1-axis is {total_1_axis}')

Output: Sum of elements at 1-axis is [13 17] Reference: API Doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 10, 2020

Dear Pankaj, I only want to sum some of the values not all. what should I do in that case? Imagine that It is an array including 0 and 1s and I would like to sum 1s before each zero not to sum all the 1s. I appreciate if you could help me.

- Mona

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 27, 2020

    Hi Pankaj What mean when use “axis=-1” in sum function Thanks…

    - Elaf Ali

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel