NumPy ufuncs | Universal functions
Last Updated :
01 Feb, 2024
NumPy Universal functions (ufuncs in short) are simple mathematical functions that operate on ndarray (N-dimensional array) in an element-wise fashion.
It supports array broadcasting, type casting, and several other standard features. NumPy provides various universal functions like standard trigonometric functions, functions for arithmetic operations, handling complex numbers, statistical functions, etc.
Characteristics of NumPy ufuncs
- These functions operate on ndarray (N-dimensional array) i.e. NumPy’s array class.
- It performs fast element-wise array operations.
- It supports various features like array broadcasting, type casting, etc.
- Numpy universal functions are objects that belong to numpy.ufunc class.
- Python functions can also be created as a universal function using the frompyfunc library function.
- Some ufuncs are called automatically when the corresponding arithmetic operator is used on arrays. For example, when the addition of two arrays is performed element-wise using the ‘+’ operator then np.add() is called internally.
Why use ufuncs?
ufunc, or universal functions offer various advantages in NumPy. Some benefits of using ufuncs are:
1. Vectorized Operations
- ufuncs are applied element-wise to all the elements in the ndarray.
- ufuncs are more efficient than loops as they are applied simultaneously to all elements. Vectorization is very useful on large data sets.
2. Type Casting
- Type casting means converting the data type of a variable to perform the necessary operation.
- ufuncs automatically handle type casting and ensure compatible datatypes for calculations.
- This allows code to be concise and reduces the chances of error.
3. Broadcasting
- Broadcasting means to perform arithmetic operations on arrays of different size.
- ufuncs automatically handle broadcasting and avoids the need for manual array shape manipulation.
Basic Universal Functions (ufunc) in NumPy
Here are some of the universal functions (ufunc) in the NumPy Python library:
Trigonometric functions
These functions work on radians, so angles need to be converted to radians by multiplying by pi/180. Only then we can call trigonometric functions. They take an array as input arguments.
It includes functions like:
Function |
Description |
sin, cos, tan |
compute the sine, cosine, and tangent of angles |
arcsin, arccos, arctan |
calculate inverse sine, cosine, and tangent |
hypot |
calculate the hypotenuse of the given right triangle |
sinh, cosh, tanh |
compute hyperbolic sine, cosine, and tangent |
arcsinh, arccosh, arctanh |
compute inverse hyperbolic sine, cosine, and tangent |
deg2rad |
convert degree into radians |
rad2deg |
convert radians into degree |
Example: Using Trigonometric Functions
Python3
import numpy as np
angles = np.array([ 0 , 30 , 45 , 60 , 90 , 180 ])
radians = np.deg2rad(angles)
print ( 'Sine of angles in the array:' )
sine_value = np.sin(radians)
print (np.sin(radians))
print ( 'Inverse Sine of sine values:' )
print (np.rad2deg(np.arcsin(sine_value)))
print ( 'Sine hyperbolic of angles in the array:' )
sineh_value = np.sinh(radians)
print (np.sinh(radians))
print ( 'Inverse Sine hyperbolic:' )
print (np.sin(sineh_value))
base = 4
height = 3
print ( 'hypotenuse of right triangle is:' )
print (np.hypot(base, height))
|
Output
Sine of angles in the array:
[ 0.00000000e+00 5.00000000e-01 7.07106781e-01 8.66025404e-01
1.00000000e+00 1.22464680e-16]
Inverse Sine of sine values:
[ 0.00000000e+00 3.00000000e+01 4.50000000e+01 6.00000000e+01
9.00000000e+01 7.01670930e-15]
Sine hyperbolic of angles in the array:
[ 0. 0.54785347 0.86867096 1.24936705 2.3012989
11.54873936]
Inverse Sine hyperbolic:
[ 0. 0.52085606 0.76347126 0.94878485 0.74483916 -0.85086591]
hypotenuse of right triangle is:
5.0
Statistical functions
These functions calculate the mean, median, variance, minimum, etc. of array elements.
They are used to perform statistical analysis of array elements.
It includes functions like:
Function |
Description |
amin, amax |
returns minimum or maximum of an array or along an axis |
ptp |
returns range of values (maximum-minimum) of an array or along an axis |
percentile(a, p, axis) |
calculate the pth percentile of the array or along a specified axis |
median |
compute the median of data along a specified axis |
mean |
compute the mean of data along a specified axis |
std |
compute the standard deviation of data along a specified axis |
var |
compute the variance of data along a specified axis |
average |
compute the average of data along a specified axis |
Example: Using Statistical functions
Python3
import numpy as np
weight = np.array([ 50.7 , 52.5 , 50 , 58 , 55.63 , 73.25 , 49.5 , 45 ])
print ( 'Minimum and maximum weight of the students: ' )
print (np.amin(weight), np.amax(weight))
print ( 'Range of the weight of the students: ' )
print (np.ptp(weight))
print ( 'Weight below which 70 % student fall: ' )
print (np.percentile(weight, 70 ))
print ( 'Mean weight of the students: ' )
print (np.mean(weight))
print ( 'Median weight of the students: ' )
print (np.median(weight))
print ( 'Standard deviation of weight of the students: ' )
print (np.std(weight))
print ( 'Variance of weight of the students: ' )
print (np.var(weight))
print ( 'Average weight of the students: ' )
print (np.average(weight))
|
Output
Minimum and maximum weight of the students:
45.0 73.25
Range of the weight of the students:
28.25
Weight below which 70 % student fall:
55.317
Mean weight of the students:
54.3225
Median weight of the students:
51.6
Standard deviation of weight of the students:
8.05277397857
Variance of weight of the students:
64.84716875
Average weight of the students:
54.3225
Bit-twiddling functions
These functions accept integer values as input arguments and perform bitwise operations on binary representations of those integers.
It includes functions like:
Function |
Description |
bitwise_and |
performs bitwise and operation on two array elements |
bitwies_or |
performs bitwise or operation on two array elements |
bitwise_xor |
performs bitwise xor operation on two array elements |
invert |
performs bitwise inversion of an array of elements |
left_shift |
shift the bits of elements to the left |
right_shift |
shift the bits of elements to the left |
Example: Using Bit-twiddling functions
Python3
import numpy as np
even = np.array([ 0 , 2 , 4 , 6 , 8 , 16 , 32 ])
odd = np.array([ 1 , 3 , 5 , 7 , 9 , 17 , 33 ])
print ( 'bitwise_and of two arrays: ' )
print (np.bitwise_and(even, odd))
print ( 'bitwise_or of two arrays: ' )
print (np.bitwise_or(even, odd))
print ( 'bitwise_xor of two arrays: ' )
print (np.bitwise_xor(even, odd))
print ( 'inversion of even no. array: ' )
print (np.invert(even))
print ( 'left_shift of even no. array: ' )
print (np.left_shift(even, 1 ))
print ( 'right_shift of even no. array: ' )
print (np.right_shift(even, 1 ))
|
Output
bitwise_and of two arrays:
[ 0 2 4 6 8 16 32]
bitwise_or of two arrays:
[ 1 3 5 7 9 17 33]
bitwise_xor of two arrays:
[1 1 1 1 1 1 1]
inversion of even no. array:
[ -1 -3 -5 -7 -9 -17 -33]
left_shift of even no. array:
[ 0 4 8 12 16 32 64]
right_shift of even no. array:
[ 0 1 2 3 4 8 16]
Conclusion
NumPy ufuncs are also called universal functions in Python. They are very useful for performing operations on ndarray. They offer benefits like automatic vectorization, broadcasting, and type casting.
In this tutorial, we have covered what are ufuncs, their characteristics, and benefits, and also showed some ufuncs with examples. This guide explains ufuncs in easy language, and you can easily use ufuncs in your own Python projects.