Python NumPy – Square Root

Python numpy.sqrt() function is used to return the non-negative square root of an array element-wise (for each element of the array). In this article, I will explain how to use Numpy square root by using numpy.sqrt() function with examples.

1. Quick Examples of Python NumPy Square Root

If you are in a hurry, below are some quick examples of how to use Python NumPy square root function.


# Below are a quick examples

# Example 1: numpy.sqrt() of single element
arr2 = np.sqrt(45)

# Example 2: Use numpy.sqrt() function to square root of numbers
arr = [25, 49, 225, 64, 81, 16]
arr2 = np.sqrt(arr)

# Example 3: Use numpy.sqrt() function with complex numbers
arr = [2+6j, -5-8j, 4-5j, 3+4j]
arr2 = np.sqrt(arr)

# Example 4: Use numpy.sqrt() function with negative and inifite as input values
arr = [-6, np.inf, 25, -15, np.inf]
arr2 = np.sqrt(arr)

# Example 5: Use numpy.sqrt() function to floating-point array
arr = [4.3, 8.5, 15.1, 23.7, 14.2, 7.8]
arr2 = np.sqrt(arr)

# Example 6: Use numpy.sqrt() function to square root from multiple array
arr = np.array([[25, 64, 9, 16], [9, 4, 49, 36]])
arr2 = np.sqrt(arr)

2. Syntax Python NumPy Square Root

Following is the syntax of the numpy.sqrt() function.


# Syntax of python numpy.sqrt() 
numpy.sqrt(arr, out=None, where=True, casting='same_kind', order='K', dtype=None)

2.1 Parameters of sqrt()

  • arr – The values whose square-roots are required. Input array.
  • out – It is ndarray, None, or tuple of ndarray and None, optional. Out will be the location where the result is to be 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 – It is array_like, optional. This condition is broadcast over the input.

2.2 Return Value of sqrt()

Return: It returns an array of the square root of the number in the input array.

3. Usage of NumPy Square Root Function

You can get the square root of the single element of an array using numpy.sqrt() function. You can also get the square values of the NumPy array using numpy.square().


import numpy as np

# Create a single element
arr = np.array(25)

# Use numpy.sqrt() function to get single element
arr2 = np.sqrt(arr)
print(arr2)

# Output:
[5.0]

# get single element square root value
arr2 = np.sqrt(45)
print(arr2)

# Output :
# 6.708203932499369

4. Get the Multiple Square Root Values of NumPy Array

To initialize the array with list of numbers use numpy.array() and calculate the square root of these numbers by using the numpy.sqrt() function. For example,


# Create an input array
arr =np.array([25, 49, 225, 64, 81, 16])

# Use numpy.sqrt() function to square root of numbers
arr2 = np.sqrt(arr)
print(arr2)

# Output
# [ 5.  7. 15.  8.  9.  4.]

5. Get the Square Roots of Complex Numbers

You can use complex numbers as elements of an array to calculate the square roots of these elements using numpy.sqrt(). For example,


# Create an input array
arr =np.array( [2+6j, -5-8j, 4-5j, 3+4j])

# Use numpy.sqrt() function with complex numbers
arr2 = np.sqrt(arr)
print(arr2)

# Output
# [2.04016609+1.47046852j 1.4889562 -2.68644571j 2.28069334-1.09615789j  2.+1.j ]

6. Get The Square Roots of Negative and Infinite Values

Using the numpy.sqrt() function you can also calculate the square root of the negative and Infinite as input values of an array. The square root of a matrix with negative numbers will throw RuntimeWarning and the square root of the element is returned as nan as a result.


# Create an 1D input array
arr =np.array[-6, np.inf, 25, -15, np.inf]

# Use numpy.sqrt() function with negative and infinite 
arr2 = np.sqrt(arr)
print(arr2)

# Output
# [nan inf  5. nan inf]
# RuntimeWarning: invalid value encountered in sqrt

7. Get the Square root of NumPy Array with Float Values

You can find the square root of the float values of array elements by using the numpy.sqrt() function.


# Create an 1D input array
arr = np.array( [4.3, 8.5, 15.1, 23.7, 14.2, 7.8])

# Use numpy.sqrt() function to floating-point array
arr2 = np.sqrt(arr)
print(arr2)

# Output
# [2.07364414 2.91547595 3.88587185 4.86826458 3.76828874 2.79284801]

8. Get the Square Roots of 2-D NumPy Array Values

Let’s calculate the square roots 2-D NumPy array values by using numpy.sqrt() function.


# Create an 2D input array
arr = np.array([[25, 64, 9, 16], [9, 4, 49, 36]])

# Use numpy.sqrt() function to get the 
# square root values of 2-d array
arr2 = np.sqrt(arr)
print(arr2)

# Output
# [[5. 8. 3. 4.]
# [3. 2. 7. 6.]]

9. Conclusion

In this article, I have explained how to use Python numpy.sqrt() function to calculate the square root of every element in the given array with examples.

Happy Learning!!

You May Also Like

References

numpy square root

Leave a Reply

You are currently viewing Python NumPy – Square Root