Numpy Square Root Tutorial | Numpy.sqrt() | np.sqrt()

Introduction

Numpy is a powerful library in Python that offers support for multi-dimensional arrays and a vast collection of mathematical functions. Among these mathematical functions, the numpy.sqrt() function is very commonly used, as it calculates the square root of every element in the array. In this article, we’ll understand its syntax, usage, and various aspects of the numpy.sqrt() function along with various examples.

Syntax of numpy.sqrt()

The syntax of numpy.sqrt()function is quite simple as explained below.

numpy.sqrt(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj])

  • x: This is the input array, and we want to calculate the square root of every element in this array.
  • out: This is an optional parameter. If provided, the function places the result into this array. It should have the same shape and buffer as the expected output.
  • Other parameters like where, casting, order, dtype, subok, signature, and extobj are optional and rarely used in standard usage.

Examples of Numpy Square Root Function | numpy.sqrt()

Import Library

We begin with importing the Numpy library as shown below.

In [0]:

import numpy as np

Example 1: Simple Usage of np.sqrt() Function on Scalar

In this example, we simply pass a scalar number to np.sqrt() function and print its square root value.

In [1]:

# Create an array
arr = np.array(9)

# Calculate the square root of element
sqrt_arr = np.sqrt(arr)

print(sqrt_arr)
Out[1]:
3.0

Example 2: Using np.sqrt() Function on 1D Array

In this example, we first create a 1-D array and then pass it to the np.sqrt() function.

In [2]:

# Create an array
arr = np.array([1, 4, 9, 16, 25])

# Calculate the square root of each element
sqrt_arr = np.sqrt(arr)

print(sqrt_arr)

Out[2]:

[1. 2. 3. 4. 5.]

Example 3: Using np.sqrt() Function on 2D Array

In this example, we first create a 2-D array and then pass it to the np.sqrt() function.

In [3]:

# Create a 2x3 array
arr = np.array([[1, 4, 9],
                [16, 25, 36]])

# Calculate the square root of each element
sqrt_arr = np.sqrt(arr)

print(sqrt_arr)

Out[3]:

[[1. 2. 3.]
 [4. 5. 6.]]

Example 4: Using Complex Numbers in numpy.sqrt()

The numpy.sqrt() function can also be used to calculate the square root of complex numbers as shown in the below example.

In [4]:

# Create a complex number
complex_num = np.array( [3-9j, -5-4j, 3+5j, 9-2j])

# Calculate the square root of the complex number
sqrt_complex = np.sqrt(complex_num)

print(sqrt_complex)
Out[4]:
[2.49868295-1.80094878j 0.83759305-2.3877944j 2.10130339+1.18973776j 3.01823992-0.33131892j]

Example 5: Using Infinity in numpy.sqrt()

The numpy.sqrt() function can also be used with infinity. In this example, we pass np.inf as an element inside the array and then we calculate the square root. Since the square root of infinity is infinity only, we get the same as output.

In [5]:

# Create an array
arr = np.array([9, np.inf])

# Calculate the square root of each element
sqrt_arr = np.sqrt(arr)

print(sqrt_arr)
Out[5]:
[ 3. inf]

Example 6: Using Negative Numbers in numpy.sqrt()

The square root of a negative number is not possible. Hence when we try to use a negative number inside the array for square root, it returns nan and also prints a warning.

In [6]:

# Create an array
arr = np.array([25,-9])

# Calculate the square root of each element
sqrt_arr = np.sqrt(arr)

print(sqrt_arr)

Out[6]:

[ 5. nan]
<ipython-input-5-660d5e9dba03>:5: RuntimeWarning: invalid value encountered in sqrt
  sqrt_arr = np.sqrt(arr)

Example 7: Using Out Parameter of np.sqrt()

To use the out parameter, we first create a zeros array of the same size as the input array and pass it as an out parameter to np.sqrt() function. The output of the square root is then saved inside this out array.

In [7]:

# Create an array
arr = np.array([1, 4, 9, 16, 25])

# Create an output array
out_arr = np.zeros(5)

# Calculate the square root using 'out' parameter
np.sqrt(arr, out=out_arr)

print(out_arr)

Out[7]:

[1. 2. 3. 4. 5.]

Example 8: Using Where Parameter of np.sqrt()

The ‘where’ parameter is a boolean array with the same shape as the input array. It specifies where the square root function should be applied or not. If the value at a particular index is False, the square root function is not applied, and if its True then it is applied.

In the below example, we create a boolean array of size 3 which is the same as the input array size. The value at the middle index is kept as True and the other two are kept as False. This boolean array is then passed as the ‘where’ parameter. In the output we can see square root is applied only to the middle element because of this ‘where’ parameter.

In [8]:

#Create an array
arr = np.array([4, 25, 9])

#Create Out array
out = np.array([0.0, 0.0, 0.0])
#Create boolean array
where = [False, True, False]

#Calculate Square Root
sqrt_arr = np.sqrt(arr,out=out,where=where)
print(sqrt_arr)
Out[8]:
[0. 5. 0.]

 

Reference: NumPy Documentation

  • MLK

    MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *