Numpy ufunc | Universal functions
Universal functions in Numpy are simple mathematical functions. It is just a term that we gave to mathematical functions in the Numpy library. Numpy provides various universal functions that cover a wide variety of operations.
These functions include standard trigonometric functions, functions for arithmetic operations, handling complex numbers, statistical functions, etc. Universal functions have various characteristics which are as follows-
- These functions operates 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 those belongs to numpy.ufunc class.
- Python functions can also be created as a universal function using frompyfunc library function.
- Some ufuncs are called automatically when the corresponding arithmetic operator is used on arrays. For example when addition of two array is performed element-wise using ‘+’ operator then np.add() is called internally.
Some of the basic universal functions in Numpy are-
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-
|sin, cos, tan||compute sine, cosine and tangent of angles|
|arcsin, arccos, arctan||calculate inverse sine, cosine and tangent|
|hypot||calculate hypotenuse of 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|
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
These functions are used to calculate mean, median, variance, minimum 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 pth percentile of array or along specified axis median compute median of data along specified axis mean compute mean of data along specified axis std compute standard deviation of data along specified axis var compute variance of data along specified axis average compute average of data along specified axis
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
These functions accept integer values as input arguments and perform bitwise operations on binary representations of those integers. It include 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 elements left_shift shift the bits of elements to left right_shift shift the bits of elements to left
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]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course