
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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