Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
Universal functions (ufuncs) are core elements in NumPy. They operate element-wise on arrays, providing fast, consistent wrappers for functions that operate on both scalar and array values. This tutorial will guide you through understanding and using ufuncs.
Ufuncs are instances of the numpy.ufunc
class. They can be thought of as fast vectorized wrappers for simple functions that return a fixed number of scalar outputs for scalar inputs.
To begin, you'll want to import NumPy:
import numpy as np
Most of the basic arithmetic operations in NumPy are implemented as ufuncs. Examples include:
a = np.array([1, 2, 3, 4]) b = np.array([2, 3, 4, 5]) print(np.add(a, b)) # Addition: [3 5 7 9] print(np.subtract(b, a)) # Subtraction: [1 1 1 1] print(np.multiply(a, b)) # Multiplication: [2 6 12 20] print(np.divide(b, a)) # Division: [2. 1.5 1.33... 1.25]
angles = np.array([0, np.pi/4, np.pi/2]) print(np.sin(angles)) # [0. 0.707... 1.] print(np.cos(angles)) # [1. 0.707... 0.]
x = np.array([1, 2, 3]) print(np.exp(x)) # Exponential function: [2.71828 7.38905 20.0855] print(np.log(x)) # Natural logarithm: [0. 0.6931 1.0986] print(np.log2(x)) # Base-2 logarithm: [0. 1. 1.5849]
For large calculations, it's useful to be able to specify the array where the result of the calculation will be stored:
x = np.array([1, 2, 3]) y = np.empty(3) np.multiply(x, 10, out=y) print(y) # [10. 20. 30.]
For binary ufuncs, there are interesting aggregates that can be computed directly. For example, if you'd like to reduce an array with a particular operation, you can use the reduce
method:
x = np.array([1, 2, 3, 4]) print(np.add.reduce(x)) # 10 print(np.multiply.reduce(x)) # 24
Using the outer
method of a ufunc, you can compute the output of all pairs of two different inputs:
x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) print(np.multiply.outer(x, y))
Output:
[[ 4 5 6] [ 8 10 12] [12 15 18]]
Ufuncs are faster than Python loops. This is due to the ufunc's ability to push the loop into the compiled layer of NumPy, leading to more efficient operations, especially when working with larger arrays.
Universal functions are a key aspect of NumPy's power, enabling users to perform fast element-wise operations on arrays. By harnessing the capabilities of ufuncs, you can achieve optimized performance in mathematical and array-based computations in Python.
Universal functions (ufuncs) in NumPy are functions that operate element-wise on arrays, allowing efficient computation on large datasets.
import numpy as np # Create a NumPy array array = np.array([1, 2, 3, 4, 5]) # Example ufunc operation (square root) sqrt_result = np.sqrt(array) print("Original Array:") print(array) print("\nSquare Root using ufunc:") print(sqrt_result)
NumPy ufunc operations provide element-wise computations, enhancing array processing capabilities.
# Assuming 'array' is already defined # Example ufunc operation (square root) sqrt_result = np.sqrt(array) print("Original Array:") print(array) print("\nSquare Root using ufunc:") print(sqrt_result)
Explore various examples and use cases of NumPy ufuncs for efficient array operations.
# Assuming 'array' is already defined # Example ufunc operations sqrt_result = np.sqrt(array) exp_result = np.exp(array) sin_result = np.sin(array) print("Original Array:") print(array) print("\nSquare Root using ufunc:") print(sqrt_result) print("\nExponential using ufunc:") print(exp_result) print("\nSine using ufunc:") print(sin_result)
Create and use custom ufuncs in NumPy for specialized array operations.
import numpy as np # Custom ufunc definition def custom_function(x): return x**2 + 2*x + 1 # Convert custom function to ufunc custom_ufunc = np.frompyfunc(custom_function, 1, 1) # Example ufunc operation result = custom_ufunc(np.array([1, 2, 3])) print("Original Array:") print(np.array([1, 2, 3])) print("\nCustom Ufunc Result:") print(result)
Sample code demonstrating various NumPy ufunc operations on arrays.
# Assuming 'array' is already defined # Example ufunc operations sqrt_result = np.sqrt(array) exp_result = np.exp(array) sin_result = np.sin(array) print("Original Array:") print(array) print("\nSquare Root using ufunc:") print(sqrt_result) print("\nExponential using ufunc:") print(exp_result) print("\nSine using ufunc:") print(sin_result)
Explore a list of common NumPy ufuncs and understand their applications in array operations.
np.sqrt
: Square rootnp.exp
: Exponentialnp.sin
: Sine# Assuming 'array' is already defined # Example ufunc operations sqrt_result = np.sqrt(array) exp_result = np.exp(array) sin_result = np.sin(array) print("Original Array:") print(array) print("\nSquare Root using ufunc:") print(sqrt_result) print("\nExponential using ufunc:") print(exp_result) print("\nSine using ufunc:") print(sin_result)
Compare NumPy ufuncs with regular functions for array operations.
# Assuming 'array' is already defined # Example using ufunc ufunc_result = np.sqrt(array) # Example using regular function regular_function_result = [x**0.5 for x in array] print("Original Array:") print(array) print("\nSquare Root using ufunc:") print(ufunc_result) print("\nSquare Root using regular function:") print(regular_function_result)