Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn ufunc Summation

In NumPy, summation functions are included in the category of universal functions (ufuncs) and are used to compute the sum of elements in a NumPy array along specified axes. NumPy provides several summation functions to calculate different types of summations:


1. Element-wise Summation (numpy.sum())This function computes the sum of all elements in the input array or along a specified axis.

import numpy as np
arr = np.array([[1, 2], [3, 4]])
# Compute sum of all elements
total_sum = np.sum(arr)
# Compute sum along rows (axis=0)
row_sum = np.sum(arr, axis=0)
# Compute sum along columns (axis=1)
col_sum = np.sum(arr, axis=1)
print("Total sum:", total_sum) # Output: 10
print("Sum along rows:", row_sum) # Output: [4 6]
print("Sum along columns:", col_sum) # Output: [3 7]

Cumulative Summation (numpy.cumsum()): This function computes the cumulative sum of elements in the input array along a specified axis.

import numpy as np
arr = np.array([1, 2, 3, 4])
# Compute cumulative sum
cum_sum = np.cumsum(arr)
print("Cumulative sum:", cum_sum) # Output: [ 1 3 6 10]


Row-wise or Column-wise Summation (numpy.sum() with axis parameter): You can use numpy.sum() with the axis parameter to compute the sum along a specific axis, either row-wise or column-wise.

import numpy as np
arr = np.array([[1, 2], [3, 4]])
# Compute sum along rows (axis=0)
row_sum = np.sum(arr, axis=0)
# Compute sum along columns (axis=1)
col_sum = np.sum(arr, axis=1)
print("Sum along rows:", row_sum) # Output: [4 6]
print("Sum along columns:", col_sum) # Output: [3 7]


These summation functions are useful for various mathematical and statistical computations, such as computing total values, averages, and cumulative sums. They provide efficient and vectorized operations for working with large arrays of data in NumPy.






Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java