In this post, you will learn about some of the 5 most popular or useful set of unary universal functions (ufuncs) provided by Python Numpy library. As data scientists, it will be useful to learn these unary functions by heart as it will help in performing arithmetic operations on sequential-like objects. These functions can also be termed as vectorized wrapper functions which are used to perform element-wise operations.
The following represents different set of popular functions:
The following are some of the unary functions whichc an be used to perform arithmetic operations:
Here is the sample code demonstrating the usage of the above functions:
import numpy as np
#
# Create an array of 5 numbers between 5 and 10
#
arr = np.linspace(5, 10, 5)
#
# Print array
#
print(arr)
#
# Perform arithmetic operations
#
np.add(arr, 1), np.subtract(arr, 1), np.multiply(arr, 2), np.divide(arr, 2)
Here is how the output would look like:
In case, you want to add all the numbers in a row or column and get the output as matrix, functions such as add.reduce or sum is used with axis. In the code sample given below, a 2 x 5 matrix is reduced by rows (axis=1) and columns (axis=0)
The following are some of the methods which can be used for calculating summary statistics:
Here is the code demonstrating the usage of above functions with SKlearn IRIS dataset.
import numpy as np
from sklearn import datasets
iris = datasets.load_iris()
np.mean(iris.data[:,0]), np.std(iris.data[:,0]), np.var(iris.data[:,0]), np.median(iris.data[:,0])
Here is how the output will look like:
The following represents the Numpy unary functions which can be used for sorting the array:
Here is the code demonstrating the usage of above functions with SKlearn IRIS dataset:
import numpy as np
from sklearn import datasets
iris = datasets.load_iris()
iris.data[0:10, 0]
np.sort(iris.data[0:10, 0])
np.argsort(iris.data[0:10, 0])
Here is how the output will look like:
The following are some of the methods which can be used for finding maximum and minimum value from a data array:
Here is the code demonstrating the usage of above functions with SKlearn IRIS dataset:
import numpy as np
from sklearn import datasets
iris = datasets.load_iris()
iris.data[0:10, 0]
np.min(iris.data[0:10, 0]), np.argmin(iris.data[0:10, 0])
np.max(iris.data[0:10, 0]), np.argmax(iris.data[0:10, 0])
Here is how the output will look like:
Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…
Last updated: 2nd May, 2024 The success of machine learning models often depends on the…
When working on a machine learning project, one of the key challenges faced by data…
Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…
Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…
Last updated: 1st May, 2024 In this post, you will learn the concepts related to…