NumPy Universal functions to know!

Filed Under: NumPy
NumPy Universal Functions

Hey, readers! In this article, we will be focusing on NumPy Universal functions, in detail.

So, let us begin!! 馃檪

What are NumPy Universal functions?

We all have already seen in the previous articles, that NumPy is quite a vast and complex module. In this article, let’s see what the universal functions are.

With Universal functions, we can easily perform on the NumPy Array and do element-wise operations on the data elements. At the basic level, the Universal functions are a part of numpy.ufunc class of Python. Also, with universal functions, we can internally invoke the basic mathematical functions during a runtime execution instead of operators.

Example: If we wish to perform a + b, using universal functions, it takes care of these basic operations internally using numpy.add() function.

Along with it, we have the feasibility to create customized universal functions.

Let us now have a look at an example to have a better clearance.

Example: Customized Universal functions

We can easily build customized universal functions that can be framed as a universal function to take care of the mathematical operations. In the below example, we have made use of numpy.ufunc class in such a manner that when we frame a+b, then it actually invokes numpy.add() function at the backend to perform the operation.

The frompyfunc() function enables us to create customized universal functions at ease.


numpy.frompyfunc(function-name, input, output)
  • function-name: name of the function to be framed as a universal function
  • input: The number of input arrays
  • output: The number of output arrays


import numpy as np
def add(a, b):
  return a+b
add = np.frompyfunc(add, 2, 1)
res = add([1, 2, 3, 4], [1,1,1,1])


[2 3 4 5]

1. Universal Trigonometric function

To get to the more granular level, NumPy Universal functions includes various trigonometric functions to perform symmetric operations on the arrays.

  • numpy.deg2rad(): Conversion of degree value to radians.
  • numpy.sin() function: We can easily get the inverse of sine hyperbolic value.
  • numpy.hypot() function: Fetch the hypotenuse for the right angled triangle structure.
  • numpy.sinh() function: Evaluate the hyperbolic sine value.

Let us now have a look at the below example to understand it more better-


import numpy as np
info = np.array([25, 45, 85])
rad = np.deg2rad(info)

print('Sine hyperbolic values:')
hyper_sin = np.sinh(rad)

print('Inverse Sine hyperbolic values:')
# hypotenuse
b = 3
h = 9
print('hypotenuse value for triangle:')
print(np.hypot(b, h))


Sine hyperbolic values:
[0.45030996 0.86867096 2.090822  ]
Inverse Sine hyperbolic values:
[0.43524462 0.76347126 0.86780642]
hypotenuse value for triangle:

2. Universal Statistical functions

We often look at various ways to get basic statistical information from data. At times, the math module works for it. But we can also have all these statistical parameters such as max value, average, etc being implemented as Universal functions.

In the context of this course, we would be exploring the below functions–

  • numpy.amin() function: Outputs the minimum element from the array.
  • numpy.amax() function: Outputs the maximum element from the array.
  • numpy.ptp() function: This function subtracts the minimum value from the maximum value and helps us define a range across the axis.
  • numpy.average() function: Represents the average of the array elements.


import numpy as np
info = np.array([10,4,6,7.9])
print('Minimum and maximum element: ')
print('Range : ')
print('Average : ')


Minimum and maximum elements: 


By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Generic selectors
Exact matches only
Search in title
Search in content