Open In App

numpy.sqrt() in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

numpy.sqrt(array[, out]) function is used to determine the positive square-root of an array, element-wise.

Syntax: numpy.sqrt() Parameters: array : [array_like] Input values whose square-roots have to be determined. out : [ndarray, optional] Alternate array object in which to put the result; if provided, it must have the same shape as arr. Returns : [ndarray] Returns the square root of the number in an array.

Code #1 : 

Python3




# Python program explaining
# numpy.sqrt() method
 
# importing numpy
import numpy as geek
 
# applying sqrt() method on integer numbers
arr1 = geek.sqrt([1, 4, 9, 16])
arr2 = geek.sqrt([6, 10, 18])
 
print("square-root of an array1  : ", arr1)
print("square-root of an array2  : ", arr2)


  Code #2 : 

Python3




# Python program explaining
# numpy.sqrt() method
 
# importing numpy
import numpy as geek
 
# applying sqrt() method on complex numbers
arr = geek.sqrt([4, -1, -5 + 9J])
 
print("square-root of an array  : ", arr)


  Code #3 : 

Python3




# Python program explaining
# numpy.sqrt() method
 
# importing numpy
import numpy as geek
 
# applying sqrt() method on negative element of real numbers
arr = geek.sqrt([-4, 5, -6])
 
print("square-root of an array  : ", arr)


Here’s an example code for numpy.sqrt() in Python:

Python3




import numpy as np
 
# Create a numpy array
arr = np.array([1, 4, 9, 16, 25])
 
# Calculate the square root of each element in the array
sqrt_arr = np.sqrt(arr)
 
# Print the resulting array
print(sqrt_arr)


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

Advantages:

The numpy.sqrt() function is a fast and efficient way to calculate the square root of an array or a single value in Python.
The numpy.sqrt() function is useful for many mathematical calculations and scientific applications, such as calculating distances, velocities, and accelerations in physics.

Disadvantages:

  1. The numpy.sqrt() function may not be precise enough for certain scientific applications that require high levels of precision.
  2. The numpy.sqrt() function may not be appropriate for all types of data, such as negative or complex numbers.

Important points:

  1. The numpy.sqrt() function returns the square root of an array or a single value.
  2. The numpy.sqrt() function can be used on both real and complex numbers.
  3. The numpy.sqrt() function can be used in combination with other NumPy functions to perform more complex mathematical operations.
  4. The numpy.sqrt() function can be used to normalize data by scaling it to a unit range.

Reference books:

“Python for Data Science Handbook” by Jake VanderPlas covers the NumPy library and its applications in data science in depth, including functions for mathematical operations like numpy.sqrt().
“Numerical Python: A Practical Techniques Approach for Industry” by Robert Johansson covers the NumPy library and its applications in numerical computing and scientific computing in depth, including functions for mathematical operations like numpy.sqrt().
“Python Data Science Essentials” by Alberto Boschetti and Luca Massaron covers the NumPy library and its applications in data science in depth, including functions for mathematical operations like numpy.sqrt().



Last Updated : 29 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads