Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Numpy.cbrt(): Finding a Cube Root of an Array

  • 28 Jul, 2025
  • Com 0
Numpy.cbrt() Method in Python (np.cbrt())

The numpy.cbrt() method calculates the cube root of each element in an input array element-wise, returning a new array with the same shape and size. If x is an input element, numpy.cbrt(x) returns ∛x.

Cube root of a numpy array

It can handle real numbers (positive, negative, or zero) and returns real results.

Syntax

numpy.cbrt(x, /, 
           out=None, 
           where=True, 
           casting='same_kind', 
           order='K', 
           dtype=None, 
           subok=True)

Parameters

Argument Description
x It represents either an input array or a scalar value.
out It is an optional output array to store the results of cube root values.
where It is a Boolean array with the same shape as input x.

The default value is True for all the elements.

casting It is a casting rule for input data type conversion (default: ‘same_kind’).
order

It represents the memory layout of the output array (‘C’, ‘F’, or ‘K’; default: ‘K’).

dtype It is the desired data type of the output array or scalar value.
subok

If True, subclasses of the output array are passed through (default: True).

Cube root of a numpy array

Let’s define a numpy array and calculate its cube root.

We can import numpy as np and use the np.cbrt() method, passing the array to it.

import numpy as np

arr = np.array([1, 8, 27, 64])

print(np.cbrt(arr))

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

The length of an input array is 4, and the length of an output array is also 4.

​Cube root of a scalar value

Cube root of a scalar value

If the input is a scalar value, the output will also be a scalar value.

import numpy as np

scalar = 125

print(np.cbrt(scalar))

# Output: 5.0

Negative numbers and 0

It does not matter if the values in the array are positive or negative. If it is negative, the output value will also be negative.

Cube root of Negative numbers in Numpy Array

 

import numpy as np

negative_arr = np.array([-1, -64, -216])

print(np.cbrt(negative_arr))

# Output: [-1. -4. -6.]

If the input value is 0, the cube root of it will also be 0.

Cube root of 0

import numpy as np

zero_arr = np.array([0, 0, 0])

print(np.cbrt(zero_arr))

# Output: [0. 0. 0.]

Using an Out Parameter

If you have a pre-allocated array using the np.zeros_like() method, you can save the cube roots in this array using the “out” argument.

import numpy as np

arr = np.array([1, 8, 27])

out_arr = np.zeros_like(arr, dtype=float)

np.cbrt(arr, out=out_arr)

print(out_arr)

# Output: [1. 2. 3.]

Broadcasting with where

Broadcasting is the process that NumPy uses to perform element-wise operations on arrays of different shapes by automatically expanding one or more arrays so their shapes are compatible.

Let’s define an input 2D array with a shape of (2, 3) and a mask with a shape of (3, ).

import numpy as np

arr = np.array([[1, 8, 27], [64, 125, 216]])  # shape: (2, 3)

mask = np.array([True, False, True])         # shape: (3,) - broadcastable

result = np.cbrt(arr, where=mask)

print(result)

# Output:

# [[1. 0. 3.]
#  [4. 0. 6.]]

So, NumPy broadcasts mask from shape (3,) to shape (2, 3):

The shape (3, ) is treated as if it were (1, 3).

NumPy automatically stretches it across the first dimension (2 rows), so it becomes:

[[True, False, True],

 [True, False, True]]

This now matches the shape of arr, which is (2, 3), and the element-wise operation proceeds.

Now, np.cbrt() calculates the cube root only where mask == True.

Where mask == False, the output is left uninitialized (i.e., becomes nan or 0 in some systems) because out= is not provided. For better practice, you should define the out argument.

Comparison with x ** (1/3)

The x**(1/3) yields the same as the np.cbrt() method, but cbrt() is optimized for cube roots and avoids potential floating-point issues in x ** (1/3) for edge cases.

import numpy as np

arr = np.array([8, 27])

print(np.cbrt(arr))
# Output: [2. 3.]

print(arr ** (1/3))
# Output: [2. 3.]

That’s it!

Post Views: 29
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python "-m" flag Example and How to Use It
Numpy.arange() Method

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend