search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

# NumPy | power method

schedule Aug 11, 2023
Last updated
local_offer
PythonNumPy
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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`.

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).

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!