Return the cube-root of an array elementwise in Numpy


To return the cube-root of an array, element-wise, use the numpy.cbrt() method in Python Numpy. An array of the same shape as x, containing the cube cube-root of each element in x. If out was provided, y is a reference to it. This is a scalar if x is a scalar.

The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

Create an array using the array() method −

arr = np.array([125, 27, 1000, 100, 841, 225])

Display the array −

print("Array...
", arr)

Get the type of the array −

print("
Our Array type...
", arr.dtype)

Get the dimensions of the Array −

print("
Our Array Dimension...
",arr.ndim)

Get the shape of the Array −

print("
Our Array Shape...
",arr.shape)

To return the cube-root of an array, element-wise, use the numpy.cbrt() method in Python Numpy −

print("
Result...
",np.cbrt(arr))

Example

import numpy as np

# Create an array using the array() method
arr = np.array([125, 27, 1000, 100, 841, 225])

# Display the array
print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # To return the cube-root of an array, element-wise, use the numpy.cbrt() method in Python Numpy print("
Result...
",np.cbrt(arr))

Output

Array...
[ 125 27 1000 100 841 225]

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(6,)

Result...
[ 5. 3. 10. 4.64158883 9.43913068 6.082202 ]

Updated on: 07-Feb-2022

969 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements