

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Cube each element in a Numpy array
To cube each element in an array., element-wise, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents. Since, we want the cube, the exponent is 3.
Raise each base in x1 to the positionally-corresponding power in x2. x1 and x2 must be broadcastable to the same shape. An integer type raised to a negative integer power will raise a ValueError. Negative values raised to a non-integral value will return nan. To get complex results, cast the input to complex, or specify the dtype to be complex.
Steps
At first, import the required library −
import numpy as np
Create an array −
arr = np.array([5, 10, 25, 7, 9])
Display the array −
print("Array...\n", arr)
Get the type of the array −
print("\nOur Array type...\n", arr.dtype)
Get the dimensions of the Array −
print("\nOur Array Dimension...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
To cube each element in an array., element-wise, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents. Since, we want the cube, the exponent is 3 −
print("\nResult...\n",np.power(arr, 3))
Example
import numpy as np # Create an array arr = np.array([5, 10, 25, 7, 9]) # Display the array print("Array...\n", arr) # Get the type of the array print("\nOur Array type...\n", arr.dtype) # Get the dimensions of the Array print("\nOur Array Dimension...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # To cube each element in an array., element-wise, use the numpy.power() method in Python # Here, the 1st parameter is the base and the 2nd exponents # Since, we want the cube, the exponent is 3 print("\nResult...\n",np.power(arr, 3))
Output
Array... [ 5 10 25 7 9] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (5,) Result... [ 125 1000 15625 343 729]
- Related Questions & Answers
- Return each element of the masked array rounded in Numpy
- For each element in a Numpy array, return a copy with the leading characters removed in Numpy
- Python Program to find the cube of each list element
- Add a scalar value with each element of a masked Array in-place in Numpy
- Subtract a scalar value from each element of a Masked Array in-place in Numpy
- Multiply each element of a masked Array by a scalar value in-place in Numpy
- Return the cube-root of an array elementwise in Numpy
- Add a scalar value to each element and return a new masked array in NumPy
- For each element in a Numpy array, return a copy with the leading spaces removed
- For each element in a Numpy array, return a copy with the trailing characters removed
- For each element in a Numpy array, return a copy with the trailing spaces removed
- True Divide each element of a masked Array by a scalar value in-place in Numpy
- Find the length of each string element in the Numpy array in C++
- Raise each and every element of a masked array to a given scalar value in NumPy
- Raise a given scalar value to each and every element of a masked array in NumPy