- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Power array elements of an array with a given value and display the result in a different type in Numpy
To power array elements of an array with a given value, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents. The dtype parameter is used to set the output datatype.
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.
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.
Steps
At first, import the required library −
import numpy as np
Create an array −
arr = np.array([5, 10, 25, 30, 40, 50])
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)
Set the exponent −
p = 2.5
To power array elements of an array with a given value, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents. The dtype parameter is used to set the output datatype −
print("
Result...
",np.power(arr, p, dtype = complex))
Example
import numpy as np # Create an array arr = np.array([5, 10, 25, 30, 40, 50]) # 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) # Set the exponent p = 2.5 # To power array elements of an array with a given value, use the numpy.power() method in Python # Here, the 1st parameter is the base and the 2nd exponents # The dtype parameter is used to set the output datatype print("
Result...
",np.power(arr, p, dtype = complex))
Output
Array... [ 5 10 25 30 40 50] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (6,) Result... [ 55.90169944+0.j 316.22776602+0.j 3125. +0.j 4929.50301755+0.j 10119.28851254+0.j 17677.66952966+0.j]
- Related Articles
- Return an array of zeroes with the same shape as a given array but with a different type in Numpy
- Return a new array of given shape filled with a fill value and a different output type in Numpy
- Return an array of ones with the same shape and type as a given array in Numpy
- Return an array of zeros with the same shape and type as a given array in Numpy
- Add arguments element-wise and display the result in a different type in Numpy
- Subtract arguments element-wise and display the result in a different type in Numpy
- Divide arguments element-wise and display the result in a different type in Numpy
- Return a new array of given shape and type filled with a fill value in Numpy
- True Divide arguments element-wise and display the result in a different type in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere with a different output type in Numpy
- Return a full array with the same shape and type as a given array in Numpy
- Return a new array with the same shape and type as a given array in Numpy
- Return a new array of given shape and type, filled with array-like in Numpy
- Return a new array with the same shape and type as given array in Numpy
- Mask array elements equal to a given value in Numpy
