Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

NumPy Universal functions to know!

NUMPY UNIVERSAL FUNCTIONS

Hello, readers! In this article, we will be focusing on NumPy Universal functions in Python programming. So, let us get started! 🙂


What do we mean by NumPy Universal Functions?

NumPy Universal functions are in actual the mathematical functions. The NumPy mathematical functions in NumPy are framed as Universal functions. These Universal (mathematical NumPy functions) operate on the NumPy Array and perform element-wise operations on the data values.

The universal NumPy functions belong to the numpy.ufunc class in Python. Some of the basic mathematical operations are called internally when we invoke certain operators. For example, when we frame x + y, it internally invokes the numpy.add() universal function.

We can even create our own universal functions using frompyfunc() method.

Syntax:

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

Example:

In this example, we have converted the function product to a universal function using frompyfunc() method.

Thus, now the product() method behaves like a universal mathematical function and performs element-wise multiplication when arrays are passed to it as parameters.

import numpy as np

def product(a, b):
  return a*b

product = np.frompyfunc(product, 2, 1)

res = product([1, 2, 3, 4], [1,1,1,1])
print(res)

Output:

[1 2 3 4]

1. Universal Trigonometric Functions in NumPy

In the course of this concept, we will now be have a look at some of the Universal Trigonometric Functions in NumPy.

  1. numpy. deg2raf(): This function helps us convert degree value to radians.
  2. numpy.sinh() function: Calculates the hyperbolic sine value.
  3. numpy.sin() function: Calculates the inverse of the sine hyperbolic value.
  4. numpy.hypot() function: Calculates the hypotenuse for the right angled triangle structure.

Example:

import numpy as np

data = np.array([0, 30, 45])

rad = np.deg2rad(data)

# hyperbolic sine value
print('Sine hyperbolic values:')
hy_sin = np.sinh(rad)
print(hy_sin)

# inverse sine hyperbolic
print('Inverse Sine hyperbolic values:')
print(np.sin(hy_sin))

# hypotenuse
b = 3
h = 6
print('hypotenuse value for the right angled triangle:')
print(np.hypot(b, h))

Output:

Sine hyperbolic values:
[0.         0.54785347 0.86867096]
Inverse Sine hyperbolic values:
[0.         0.52085606 0.76347126]
hypotenuse value for the right angled triangle:
6.708203932499369

2. Universal Statistical functions

Apart from Trigonometric functions, Python NumPy also offers us Universal Statistical functions. Some of them are listed below:

  1. numpy.amin() function: Represents the minimum value from the array.
  2. numpy.amax() function: Represents the maximum value from the array.
  3. numpy.ptp() function: It represents the range of values of an array across an axis which is calculated by subtracting the minimum value from the maximum value.
  4. numpy.average() function: It calculates the average of the array elements.

Example:

import numpy as np

data = np.array([10.2,34,56,7.90])

print('Minimum and maximum data values from the array: ')
print(np.amin(data))
print(np.amax(data))

print('Range of the data: ')
print(np.ptp(data))

print('Average data value of the array: ')
print(np.average(data))

Output:

Minimum and maximum data values from the array:
7.9
56.0
Range of the data:
48.1
Average data value of the array:
27.025000000000002

Conclusion

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!! 🙂