search
Search
menu search toc more_vert
Guest 0reps
Thanks for the thanks!
close
account_circle
Profile
exit_to_app
Sign out
help Ask a question
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
A
A
brightness_medium
share
arrow_backShare Twitter Facebook

# NumPy | power method

NumPy
chevron_right
Documentation
schedule Jul 1, 2022
Last updated
local_offer PythonNumPy
Tags
expand_more

NumPy's `power(~)` method is used to compute the power of each number of the input array.

NOTE

NumPy's `power(~)` method uses the same data-type as the input array to perform the calculation; if your input array only contains integers, then returned result will also be of type `int`. On the other hand, `float_power(~)` always uses `float64` for maximum precision.

# Parameters

1. `x1` | `array_like`

The base numbers.

2. `x2` | `array_like`

The exponents.

3. `where` | `array_like` of `boolean` | `optional`

Instead of computing the power of all the numbers, we can choose specific numbers. Values corresponding to `True` will be considered, while those corresponding to `False` values will be ignored.

# Return value

A scalar if `x1` is a scalar, otherwise a NumPy array is returned.

WARNING

Use the ** syntax instead whenever possible. Instead of using `np.power([1,2,3],2)`, simply use `[1,2,3]**2`, which offers a huge performance boost. The only case when you'd want to use this `power(~)` method is when you have multiple exponents.

# Examples

## Using a common exponent

To raise the the numbers by a common exponent, provide a scalar:

``` np.power([1,2,3], 2) array([1, 4, 9]) ```

## Using multiple exponents

You can pass an array as the exponents as well:

``` np.power([1,2,3], [2,3,4]) array([ 1, 8, 81]) ```

What we are doing here is computing `1^2`, `2^3` and `3^4`.

## Using a mask

We can choose which values to take power of by providing a boolean mask, like follows:

``` np.power([2,3,4], 2, where=[False, True, False]) array([1, 9, 3]) ```

Notice how only the values that are flagged as `True` in the boolean masks are considered (i.e. the value 3 in this case).

mail