Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Summation Universal Function (ufunc)



Summation Universal Function (ufunc)

A summation universal function (ufunc) in NumPy is a function used to compute the sum of elements in an array.

This operation can be performed on the entire array or along a specific axis (such as rows or columns). The primary summation ufunc in NumPy is numpy.sum() function.

NumPy Summation

The numpy.sum() function is used to compute the sum of array elements over a specified axis. It can sum all elements in an array or sum elements along a specific axis (e.g., row-wise or column-wise).

Example

In the following example, we use the numpy.sum() function to calculate the sum of elements in an array −

import numpy as np

# Define an array
a = np.array([[1, 2, 3], [4, 5, 6]])

# Compute the sum of all elements
total_sum = np.sum(a)

# Compute the sum along the columns
column_sum = np.sum(a, axis=0)

# Compute the sum along the rows
row_sum = np.sum(a, axis=1)

print("Total sum:", total_sum)
print("Column-wise sum:", column_sum)
print("Row-wise sum:", row_sum)

Following is the output obtained −

Total sum: 21
Column-wise sum: [5 7 9]
Row-wise sum: [ 6 15]

NumPy Cumulative Sum

The numpy.cumsum() function is used to compute the cumulative sum of array elements along a specified axis. It returns an array where each element is the cumulative sum of the previous elements.

Example

In the following example, we use the numpy.cumsum() function to calculate the cumulative sum of elements in an array −

import numpy as np

# Define an array
a = np.array([1, 2, 3, 4, 5])

# Compute the cumulative sum
cumulative_sum = np.cumsum(a)

print("Cumulative sum:", cumulative_sum)

This will produce the following result −

Cumulative sum: [ 1  3  6 10 15]

NumPy Cumulative Product

The numpy.cumprod() function is used to compute the cumulative product of array elements along a specified axis. It returns an array where each element is the cumulative product of the previous elements.

Example

In the following example, we use the numpy.cumprod() function to calculate the cumulative product of elements in an array −

import numpy as np

# Define an array
a = np.array([1, 2, 3, 4, 5])

# Compute the cumulative product
cumulative_product = np.cumprod(a)

print("Cumulative product:", cumulative_product)

Following is the output of the above code −

Cumulative product: [  1   2   6  24 120]

NumPy Summation with Conditions

The numpy.sum() function can also be used with conditional statements to sum elements that meet a specific condition.

Example

In the following example, we use the numpy.sum() function to calculate the sum of elements that are greater than a specified value −

import numpy as np

# Define an array
a = np.array([1, 2, 3, 4, 5])

# Compute the sum of elements greater than 2
conditional_sum = np.sum(a[a > 2])

print("Sum of elements greater than 2:", conditional_sum)

The result produced is as follows −

Sum of elements greater than 2: 12

NumPy Product

The numpy.prod() function is used to compute the product of array elements over a specified axis. It can compute the product of all elements in an array or along a specific axis.

Example

In the following example, we use the numpy.prod() function to calculate the product of elements in an array −

import numpy as np

# Define an array
a = np.array([1, 2, 3, 4, 5])

# Compute the product of all elements
total_product = np.prod(a)

print("Total product:", total_product)

This will produce the following result −

Total product: 120
Advertisements