🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.sqrt()

AI & DATA SCIENCE // np-sqrt

np.sqrt() computes the non-negative square root of each element in an array.

Syntax

np.sqrt(arr)

Deep Dive Course

np.sqrt() applies element-wise, and for a real-valued input array, it produces nan, with a RuntimeWarning, for any negative element, since a negative number has no real square root — NumPy doesn't automatically upgrade the result to complex. If you know an array might contain values with a meaningful negative square root, you'd first convert it to a complex dtype, since np.sqrt() on a complex-typed array does correctly compute complex results.

1Understanding np.sqrt()

np.sqrt() applies element-wise, and for a real-valued input array, it produces nan, with a RuntimeWarning, for any negative element, since a negative number has no real square root — NumPy doesn't automatically upgrade the result to complex. If you know an array might contain values with a meaningful negative square root, you'd first convert it to a complex dtype, since np.sqrt() on a complex-typed array does correctly compute complex results.

💡

np.sqrt() on a real array with negative values silently produces nan for those positions plus a warning, rather than raising an error — check for negative values first, or cast to a complex dtype explicitly, if that's a real possibility in your data.

editor.html
import numpy as np

arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.sqrt() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([4, -1, 9])
print(np.sqrt(arr))
localhost:3000

3Best Practices

Follow these guidelines when working with np.sqrt():

1. Check for negative values before calling np.sqrt() on real-valued data if negatives are a realistic possibility, to avoid silent nan results

2. Cast the array to a complex dtype explicitly if you specifically need complex square roots of negative values

3. Prefer np.sqrt(x) over x ** 0.5 for clarity, since it directly names the mathematical operation being performed

⚠️

Tip: np.sqrt() on a real array with negative values silently produces nan for those positions plus a warning, rather than raising an error — check for negative values first, or cast to a complex dtype explicitly, if that's a real possibility in your data.

editor.html
import numpy as np

arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr))
Example 02Advanced Example
import numpy as np

arr = np.array([4, -1, 9])
print(np.sqrt(arr))

Best Practices

  • Check for negative values before calling np.sqrt() on real-valued data if negatives are a realistic possibility, to avoid silent nan results
  • Cast the array to a complex dtype explicitly if you specifically need complex square roots of negative values
  • Prefer np.sqrt(x) over x ** 0.5 for clarity, since it directly names the mathematical operation being performed

Interview Question

Why does np.sqrt(-1) return nan instead of raising an exception or returning a complex number?

Hint: Think about what dtype the input and output arrays actually are.

np.sqrt() on a real-valued, float, array is designed to also return a real-valued array, and the square root of a negative real number isn't a real number, so there's no valid real value to return for that position — NumPy fills it with nan and raises a RuntimeWarning to flag the invalid operation, rather than crashing the whole computation or silently switching the output's dtype to complex. To get an actual complex result for negative inputs, you need to explicitly work with a complex-typed array from the start.

Exercises

MediumPractice using np.sqrt() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr))

Frequently Asked Questions

Why does np.sqrt(-1) return nan instead of raising an exception or returning a complex number?

np.sqrt() on a real-valued, float, array is designed to also return a real-valued array, and the square root of a negative real number isn't a real number, so there's no valid real value to return for that position — NumPy fills it with nan and raises a RuntimeWarning to flag the invalid operation, rather than crashing the whole computation or silently switching the output's dtype to complex. To get an actual complex result for negative inputs, you need to explicitly work with a complex-typed array from the start.

Related Functions

np-powernp-absolutecomplex-numbers