Tutorials  /  Python

numpy.sum() in Python - Tutorial

Ccentron Redaktion · March 2024 ·3 min read ·Python, Tutorial

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

numpy.sum() in Python syntax

Python NumPy sum() method syntax is:

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

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

numpy.sum() in Python - 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.

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

Python
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

Python
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

Python
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]

The numpy.sum() function is a powerful tool in Python for quickly and efficiently calculating the sum of array elements along a specified axis. It significantly simplifies mathematical computations and data analysis by providing a fast and optimized solution for sum-based operations.

If you want to learn more about efficient data processing and powerful IT solutions, register now in the ccenter or contact our sales team for a personalized consultation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren