
- 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 - Product Universal Function (ufunc)
Product Universal Function (ufunc)
A product universal function (ufunc) in NumPy is a function used to compute the product of elements in an array.
This operation multiplies all the elements together, either for the entire array or along a specific axis (such as rows or columns). The primary product ufunc in NumPy is numpy.prod() function.
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 (e.g., row-wise or column-wise).
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, 6]]) # Compute the product of all elements total_product = np.prod(a) # Compute the product along the columns column_product = np.prod(a, axis=0) # Compute the product along the rows row_product = np.prod(a, axis=1) print("Total product:", total_product) print("Column-wise product:", column_product) print("Row-wise product:", row_product)
Following is the output obtained −
Total product: 720 Column-wise product: [ 4 10 18] Row-wise product: [ 6 120]
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)
This will produce the following result −
Cumulative product: [ 1 2 6 24 120]
NumPy Product with Conditions
The numpy.prod() function can also be used with conditional statements to compute the product of elements that meet a specific condition.
Example
In the following example, we use the numpy.prod() function to calculate the product 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 product of elements greater than 2 conditional_product = np.prod(a[a > 2]) print("Product of elements greater than 2:", conditional_product)
The result produced is as follows −
Product of elements greater than 2: 60
Matrix Product with NumPy ufuncs
The matrix product in NumPy refers to multiplying two matrices together, following the rules of linear algebra. This operation is done using numpy.matmul() function or the @ operator, which computes the dot product of two arrays.
Example
In this example, np.matmul() function performs the matrix multiplication of matrix1 and matrix2, resulting in a new matrix −
import numpy as np # Define two 2D arrays (matrices) matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) # Perform matrix multiplication result = np.matmul(matrix1, matrix2) print(result)
We get the output as shown below −
[[19 22] [43 50]]
NumPy Dot Product and Cross Product
The dot product calculates the sum of the products of corresponding elements in two arrays, while the cross product finds a vector perpendicular to two input vectors in 3D space.
NumPy provides numpy.dot() function for the dot product and numpy.cross() function for the cross product.
Example
In this example, np.dot() function calculates the dot product of the two vectors vector1 and vector2 −
import numpy as np # Define two 1D arrays (vectors) vector1 = np.array([1, 2, 3]) vector2 = np.array([4, 5, 6]) # Compute the dot product dot_result = np.dot(vector1, vector2) print(dot_result)
Following is the output obtained −
32
NumPy Element-wise Product Operations
Element-wise product operations in NumPy involve multiplying corresponding elements of two arrays. This is done using the numpy.multiply() function or the * operator, and it is useful for operations like scaling values in an array.
Example
In the following example, the np.multiply() function multiplies corresponding elements of array1 and array2 element-wise −
import numpy as np # Define two arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Perform element-wise multiplication product = np.multiply(array1, array2) print(product)
This will produce the following result −
[ 4 10 18]