- 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 - Vectorization with Arrays
Vectorization with Arrays in NumPy
Vectorization refers to the process of applying operations directly on entire arrays or vectors, rather than iterating through individual elements. In NumPy, we can achieve this by performing element-wise operations using highly optimized "C" and "Fortran" libraries.
Following are the advantages of using vectorization in NumPy −
- Performance: Vectorized operations are much faster than similar operations implemented with loops in Python.
- Readability: Code that uses vectorization is more concise and easier to understand.
- Scalability: Vectorization allows you to work efficiently with large datasets.
Basic Vectorized Operations
As we know that vectorized operations in NumPy is used to perform element-wise operations on entire arrays without the need for explicit loops. Few common operations which can be applied directly to arrays includes −
- Arithmetic Operations
- Mathematical Functions
- Comparison Operators
Element-wise Arithmetic Operations
Element-wise arithmetic operations in NumPy include addition, subtraction, multiplication, and division. These operations are performed directly on arrays, applying the arithmetic operation to each corresponding element.
For example, adding two arrays will result in a new array where each element is the sum of the corresponding elements in the original arrays.
Example
In the following example, each element of a is added to the corresponding element of b without the need for a loop −
import numpy as np
# Create two arrays
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
# Perform element-wise addition
result = a + b
print("Result of Addition:", result)
Following is the output obtained −
Result of Addition: [11 22 33 44]
Element-wise Mathematical Functions
NumPy provides a wide range of mathematical functions (such as exponential, logarithmic, and trigonometric functions) that perform element-wise operations on arrays.
These functions allow you to perform complex mathematical transformations on arrays by calling a single function.
Example
In this example, we are performing element-wise exponentiation using the np.exp() function. This calculates the exponential for each element in the array −
import numpy as np
# Create an array
a = np.array([1, 2, 3, 4])
# Perform element-wise exponentiation
result = np.exp(a)
print("Exponential of Array:", result)
This will produce the following result −
Exponential of Array: [ 2.71828183 7.3890561 20.08553692 54.59815003]
Comparison Operations
Element-wise comparison operations allow you to compare arrays directly, resulting in a boolean array indicating the result of the comparison for each element. These operations include comparisons like greater than, less than, and equality.
Example
Here, we perform an element-wise comparison to check if each element is greater than 2. This results in a Boolean array indicating True or False for each comparison −
import numpy as np
# Create an array
a = np.array([1, 2, 3, 4])
# Perform element-wise comparison
result = a > 2
print("Comparison Result:", result)
Following is the output of the above code −
Comparison Result: [False False True True]
Vectorizing Custom Functions
Vectorizing custom functions in NumPy allows you to apply a function to an entire array element-wise.
You can use np.vectorize() function to convert a standard Python function into a vectorized function, making it possible to handle arrays directly. This simplifies the code by eliminating the need for explicit loops.
Using np.vectorize() Function
If you have a custom function that operates on individual elements, you can use np.vectorize() function to apply this function to entire arrays in a vectorized manner.
Example
In the example below, we define a custom function to square a number and use the vectorize() function to apply this function element-wise to a NumPy array −
import numpy as np
# Define a custom function
def square(x):
return x ** 2
# Vectorize the custom function
vectorized_square = np.vectorize(square)
# Create an array
a = np.array([1, 2, 3, 4])
# Apply the vectorized function
result = vectorized_square(a)
print("Squared Array:", result)
The output obtained is as shown below −
Squared Array: [ 1 4 9 16]
Limitations of vectorize() Function
While np.vectorize() function makes it easy to apply custom functions to arrays, it is essentially a convenience function and does not provide the same performance benefits as true vectorized operations does.
For optimal performance, it is better to use built-in NumPy functions or rewrite the function to take advantage of vectorization.
Vectorized Operations with Multi-dimensional Arrays
You can apply vectorized operations in NumPy directly to multi-dimensional arrays, helping to perform element-wise calculations across all dimensions without writing explicit loops.
Example
In the following example, we create two 2D arrays and perform matrix multiplication using the np.dot() function −
import numpy as np
# Create two 2D arrays (matrices)
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Perform matrix multiplication using vectorization
result = np.dot(a, b)
print("Matrix Multiplication Result:\n", result)
The result is a new matrix obtained by multiplying the two original matrices −
Matrix Multiplication Result: [[19 22] [43 50]]