NumPy sqrt()

The numpy.sqrt() function computes the non-negative square root of each element in an input array.

Syntax

</>
Copy
numpy.sqrt(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeThe values for which square roots are computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the square root.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array containing the square root of each element in the input array. If the input contains negative numbers, the result will be nan unless using a complex data type.


Examples

1. Computing the Square Root of a Single Value

Here, we compute the square root of a single number.

</>
Copy
import numpy as np

# Define a number
num = 16

# Compute the square root
result = np.sqrt(num)

# Print the result
print("Square root of", num, "is:", result)

Output:

Square root of 16 is: 4.0

2. Computing Square Roots for an Array

We compute the square roots of multiple numbers stored in an array.

</>
Copy
import numpy as np

# Define an array of numbers
numbers = np.array([0, 1, 4, 9, 16, 25])

# Compute the square root for each element
sqrt_values = np.sqrt(numbers)

# Print the results
print("Numbers:", numbers)
print("Square root values:", sqrt_values)

Output:

Numbers: [ 0  1  4  9 16 25]
Square root values: [0. 1. 2. 3. 4. 5.]

3. Handling Negative Values

By default, negative values return nan. To compute square roots of negative numbers, use a complex data type.

</>
Copy
import numpy as np

# Define an array with negative values
numbers = np.array([-1, -4, 9, 16])

# Compute square root
sqrt_values = np.sqrt(numbers)

# Print results
print("Square root values:", sqrt_values)

Output:

Square root values: [nan nan  3.  4.]

For negative values, the result is nan since real numbers do not have square roots for negative inputs.

4. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of numbers
numbers = np.array([1, 4, 9, 16])

# Create an output array with the same shape
output_array = np.ndarray(shape=numbers.shape)

# Compute square root and store in output_array
np.sqrt(numbers, out=output_array)

# Print the results
print("Computed square root values:", output_array)

Output:

Computed square root values: [1. 2. 3. 4.]

5. Using the where Parameter

Using a condition to compute square roots only for selected elements.

</>
Copy
import numpy as np

# Define an array of numbers
numbers = np.array([0, 4, 9, 16])

# Define a mask (compute square root only where mask is True)
mask = np.array([False, True, False, True])

# Compute square roots where mask is True
result = np.sqrt(numbers, where=mask)

# Print the results
print("Computed square root values with mask:", result)

Output:

Computed square root values with mask: [ 0.  2.  0.  4.]

The square root values are computed only for elements where mask=True. The other values remain unchanged.