Numpy power numpy.power()
In this post, you will learn about the Python numpy power function. The numpy.power() is a function in the NumPy library that raises each element in an array to a specified power. Here's the syntax and usage:
numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
- x1: The base array or scalar.
- x2: The exponent array or scalar. x1 and x2 must be broadcastable to the same shape.
- out: [ndarray, None, or tuple of ndarray and None, optional] If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
- where: [optional] This 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.
- casting: [{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional] It controls what kind of data casting may occur.
- order: [{'C', 'F', 'A', 'K'}, optional] It controls the memory layout order of the output.
- dtype: [dtype, optional] It overrides the dtype of the calculation and output arrays.
- subok: [bool, optional] If True, then sub-classes will be passed-through (default). False, the returned array will be forced to be a base-class array.
Examples of numpy.power()
Raising an array to a scalar power
import numpy as np
# Array of numbers
array_numbers = np.array([1, 2, 3, 4, 5])
# Raise each element to the power of 2
squared_array = np.power(array_numbers, 2)
print(squared_array)
Output of the above code:
[ 1 4 9 16 25]
Raising a scalar to an array of powers
This example raises the scalar 2 to the powers [1, 2, 3, 4, 5].
# Base number
base_number = 2
# Exponents
exponents = np.array([1, 2, 3, 4, 5])
# Raise 2 to the power of each element in exponents
powers_array = np.power(base_number, exponents)
print(powers_array)
Output of the above code:
[ 2 4 8 16 32]
Raising arrays element-wise
This example raises each element in base_array to the corresponding element in exponents_array.
# Base array
base_array = np.array([1, 2, 3, 4, 5])
# Exponents array
exponents_array = np.array([5, 4, 3, 2, 1])
# Raise each element in base_array to the power of corresponding element in exponents_array
elementwise_powers = np.power(base_array, exponents_array)
print(elementwise_powers)
Output of the above code:
[ 1 16 27 16 5]
Related Articles
Trigonometric functions Python Numpy
Python convert dataframe to numpy array
Convert binary to decimal in Python
Multiply all elements in list Python
Multiply each element of a list by a number in Python
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
Quick Introduction to Python Pandas
Python requests GET method
Python Convert XML to CSV
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Bouncing ball game code in Python
Remove character from string Python
Python raise keyword