What are unary functions (ufuncs) in NumPy?

NumPy is a third-party, open-source library in Python that supports mathematical, scientific, and Data Science programming.

Numpy provides an n-dimensional arrayan array used to store collections of data. object to work with scientific projects and programming.

NumPy can be imported in Python using importFinds and loads a package, a sub-package, or a module. keyword as follows:

import numpy as np

Unary functions (ufuncs)


  1. np.abs(array): To calculate absolute value of each value of the array.
  2. np.sqrt(array): To calculate square root of each value of the array.
  3. np.exp(array): To calculate exponential value of each element of the array.
  4. np.square(array): To calculate square of each value of the array.
  5. np.sin(array): To find the sine value of each data in the array.
  6. np.cos(array): To find the cos value of each data in the array.
  7. np.tan(array): To find the tan value of each data in the array.

Code

import numpy as np
# Defining a numpy array:
# array 1:
arr = np.array([3,4,-2,5,1,-5,25,-6,9])
# array 2:
arr1= np.array([4,16,64,25])
# Working with some numpy uniary ufuncs:
# 1. abs, fabs:
print(f"Absolute values: {np.abs(arr)}")
# 2. sqrt:
print(f"Square root of each element: {np.sqrt(arr1)}")
# 3. exp:
print(f"Exponential value for each element: {np.exp(arr1)}")
# 4. square:
print(f"Squared values: {np.square(arr)}")
# 5. sin:
print(f"Sine value: {np.sin(arr1)}")
# 6. cos:
print(f"Cos value: {np.cos(arr1)}")
# 7. tan:
print(f"Tan value: {np.tan(arr1)}")
# Similarly other uniary functions can be applied.

Click on the below link to know more about ufuncs:

Free Resources