# NumPy Universal functions to know!

Filed Under: NumPy

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.

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:

```import numpy as np

return a+b

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

Output:

```[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.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-

Example:

```import numpy as np

info = np.array([25, 45, 85])

print('Sine hyperbolic values:')
print(hyper_sin)

print('Inverse Sine hyperbolic values:')
print(np.sin(hyper_sin))

# hypotenuse
b = 3
h = 9
print('hypotenuse value for triangle:')
print(np.hypot(b, h))
```

Output:

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

## 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.

Example:

```import numpy as np

info = np.array([10,4,6,7.9])

print('Minimum and maximum element: ')
print(np.amin(info))
print(np.amax(info))

print('Range : ')
print(np.ptp(info))

print('Average : ')
print(np.average(info))
```

Output:

```Minimum and maximum elements:
4.0
10.0
Range:
6.0
Average:
6.975
```

## 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!! ðŸ™‚

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