Introduction
In this tutorial, we will learn about numpy mathematical operations that you generally use in your data science and machine learning project. Numpy functions like numpy sqrt, numpy power, numpy exp, and numpy log are advanced mathematical operations. We will understand the syntaxes of these functions through various kinds of examples.
- Also Read – Numpy Operations – numpy.sum() , numpy.subtract() , numpy.multiply() , numpy.dot() , numpy.divide()
Importing Numpy Library
We will start with the import of the numpy library.
import numpy as np
Beginning this article with sqrt function.
Numpy Sqrt : np.sqrt()
Numpy sqrt function helps in calculating square root of values provided as input.
We will now see the syntax of np.sqrt() function to understand the parameters and their usage.
Syntax
np.sqrt(x,out=some_value,where=some_value,kwargs)
x : array_like – This is the array which is passed to the function
out : ndarray, None, or tuple of ndarray and None (optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
where : array_like (optional) – This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.
kwargs – For other keyword-only arguments.
The final result for np.sqrt() function is an array of the same shape as x, containing the positive square-root of each element in x. If any element in x is complex, a complex array is returned (and the square-roots of negative reals are calculated). If all of the elements in x are real, so is y, with negative elements returning nan
Example 1 : Basic example of np.sqrt() function
np.sqrt([9,729,10000])
array([ 3., 27., 100.])
Example 2: np.sqrt() function with imaginary values as input and output
np.sqrt([8, -1, -5+7J])
array([2.82842712+0.j , 0. +1.j , 1.342074 +2.60790388j])
Example 3: np.sqrt() function with negative and inifite as input values
np.sqrt([16, -3, np.inf])
H:\Anaconda\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt """Entry point for launching an IPython kernel.
array([ 4., nan, inf])
Now let us see the numpy power function.
Numpy Power : np.power()
Numpy power function helps in calculating the results when a number is raised to a power.
We will now look at the syntax of np.power() function to know about this function and its usage.
Syntax
np.power(x1,x2,out=some_value,where=some_value,kwargs)
x1 : array_like – This is the array which consits base values.
x2 : array_like – This is the array which consits exponent values.
out : ndarray, None, or tuple of ndarray and None (optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
where : array_like (optional) – This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.
kwargs – For other keyword-only arguments.
The final result obtained is an array with bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars.
Example 1: Understanding power function by raising the bases to fourth power
Here we have generated base values using range() function. These base values are raised to the fourth power.
x1 = range(9)
x1
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
np.power(x1, 4)
array([ 0, 1, 16, 81, 256, 625, 1296, 2401, 4096], dtype=int32)
Example 2: Raising the bases to different exponents
Here x2 array is used as exponents for bases present in x1 array.
x2 = [1,2,3,4,5,6,7,8,9]
np.power(x1, x2)
array([ 0, 1, 8, 81, 1024, 15625, 279936, 5764801, 134217728], dtype=int32)
Next in the list of this tutorial is numpy log function.
[adrotate banner=”3″]
Numpy Log : np.log()
Numpy log function helps in calculating the logarithmic value of the input.
Let’s understand the syntax of np.log() function to find about the parameters along with other details.
Syntax
np.log(x,out=some_value,where=some_value,kwargs)
x : array_like – This is the array which is passed to the function
out : ndarray, None, or tuple of ndarray and None (optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
where : array_like (optional) – This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.
kwargs – For other keyword-only arguments.
The output of np.log() is an array with Natural logarithm, element-wise. The natural logarithm is logarithm in base e.
Example 1: Finding log of ‘1’ using np.log() function
np.log([1])
array([0.])
Example 2: Finding log of ‘e’
np.log([np.e,np.e**3])
array([1., 3.])
Example 3: Finding log of ‘0’
np.log([0])
H:\Anaconda\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log """Entry point for launching an IPython kernel.
array([-inf])
Reaching the end of this tutorial, we will learn about numpy exp function
Numpy Exp : np.exp()
Numpy exp function helps in calculating the exponential value of the input.
Understanding about np.exp() function to learn about the intricacies of the arguments.
Syntax
np.exp(x,out=some_value,where=some_value,kwargs)
x : array_like – This is the array which is passed to the function
out : ndarray, None, or tuple of ndarray and None (optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
where : array_like (optional) – This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.
kwargs – For other keyword-only arguments.
As a final output of numpy exp function, we get output array, with element-wise exponential of x.
Example 1: Finding exponential of whole numbers using np.exp()
np.exp([4])
array([54.59815003])
Example 2: Finding exponential value of ‘0’
np.exp([0])
array([1.])
Example 3: Finding exponential value of negative and infinite values
np.exp([-9])
array([0.00012341])
Conclusion
Concluding this tutorial, we have learned how we can use np.sqrt(), np.power(), np.log() and np.exp() to execute and produce advanced mathematical operations. We looked at syntax and examples of each of the functions in this tutorial.
Click Here To Download This Tutorial in Interactive Jupyter Notebook
Reference- https://numpy.org/doc/
- Also Read – Python Numpy Array – A Gentle Introduction to beginners
- Also Read – Tutorial – numpy.arange() , numpy.linspace() , numpy.logspace() in Python
- Also Read – Complete Numpy Random Tutorial – Rand, Randn, Randint, Normal
- Also Read – Tutorial – Numpy Shape, Numpy Reshape and Numpy Transpose in Python