Numpy Operations – Numpy.sqrt() , Numpy.power() , Numpy.log() , Numpy.exp()

Numpy Operations - Numpy.sqrt() , Numpy.power() , Numpy.log() , Numpy.exp()

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.

Importing Numpy Library

We will start with the import of the numpy library.

In [1]:
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.

Ad
Deep Learning Specialization on Coursera

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

In [3]:
np.sqrt([9,729,10000])
Out[3]:
array([  3.,  27., 100.])

Example 2: np.sqrt() function with imaginary values as input and output

In [4]:
np.sqrt([8, -1, -5+7J])
Out[4]:
array([2.82842712+0.j        , 0.        +1.j        ,
       1.342074  +2.60790388j])

Example 3: np.sqrt() function with negative and inifite as input values

In [5]:
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.
Out[5]:
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.

In [6]:
x1 = range(9)
x1 
Out[6]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
In [7]:
np.power(x1, 4)
Out[7]:
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.

In [8]:
x2 = [1,2,3,4,5,6,7,8,9]
np.power(x1, x2)
Out[9]:
array([        0,         1,         8,        81,      1024,     15625,
          279936,   5764801, 134217728], dtype=int32)

Next in the list of this tutorial is numpy log function.

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

In [10]:
np.log([1])
Out[10]:
array([0.])

Example 2: Finding log of ‘e’

In [11]:
np.log([np.e,np.e**3])
Out[11]:
array([1., 3.])

Example 3: Finding log of ‘0’

In [12]:
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.
Out[12]:
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()

In [13]:
np.exp([4])
Out[13]:
array([54.59815003])

Example 2: Finding exponential value of ‘0’

In [14]:
np.exp([0])
Out[14]:
array([1.])

Example 3: Finding exponential value of negative and infinite values

In [15]:
np.exp([-9])
Out[15]:
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/

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet