In this tutorial, we will learn about one of the essential numpy mathematical operations that you generally use in your data science and machine learning project. **Numpy Power** function is one of the advanced mathematical operations, which is very helpful in doing advanced projects. We will understand the syntaxes of power function through various kinds of examples and walk-throughs.

Catalogue

## What is Numpy Power?

Numpy Power Function is a part of arithmetic functions in Numpy. Numpy power() is a function available in numpy in which the first element of the array is the base which is raised to the power element (second array) and finally returns the value.** In layman language, what numpy power does is it calculates the exponentiation of value in Python.**

I am assuming while writing this post that you already know about exponentiation. If not let me quickly explain you.

**According to Wikipedia: **Exponentiation is a mathematical operation, written as bn, involving two numbers, the base b, and the *exponent* or *power* *n*, and pronounced as “*b* raised to the power of *n*“. When *n* is a positive integer, exponentiation corresponds to repeated multiplication of the base: that is, *b*^{n} is the product of multiplying *n* bases.

Now I think you got a glance about exponentiation. So let’s move to our main topic and jump directly to the **syntax’s of numpy.power.**

## Numpy Power Syntax

**Basic High-Level (mostly used) syntax**

`np.power(array_of_base, array_of_exponent)`

This is the basic numpy syntax which is widely used.

However, `numpy.power()`

is a universal function, i.e. it supports a several parameters that allow you to optimise its operation depending on the specifics of the algorithm in which we need it. The actual syntax of numpy.power() is the following.

`numpy.power(arr_of_base, arr_of_exp, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) `

**Parameters** of Numpy Power Function

Let’s move to the parameters of the **numpy power** function.

Parameter | Mandatory or Not |

arr_of_base | Mandatory |

arr_of_exp | Mandatory |

out | Not-Mandatory |

where | Not-Mandatory |

casting | Not-Mandatory |

order | Not-Mandatory |

dtype | Not-Mandatory |

### array_of_base:

These numbers will be utilised as the”foundations” of our exponents. Bear in mind which you can also just supply a single integer! The first parameter of this np.power function is** array-of-bases**.

Note that this is needed. You must provide input here. Also, the item that you provide can take a variety of forms. It is possible to supply a NumPy array, but it is also possible to provide an array-like input.

The array-like inputs which will work here are things such as a Python list, a Python tuple or one of the other Python objects that have array-like properties.

### array_of_exponent:

The second parameter is array-of-exponents, which lets you specify the exponents that you will apply to the bases, **array-of-bases**.

Note that just like the array-of-bases input, this input must be a NumPy array or an array-like object. So here you are able to supply a NumPy array, a Python list, a tuple, or another Python object with array-like properties. You can even provide a single integer!

### out

Out is a ndarray (N- dimension array) and an optional field in **numpy power**. A place the result will be saved in. If given, the shape to which the inputs broadcast has to be in, when a freshly-allocated array is returned unless obtained or None. A tuple (possible as a keyword argument only) should have a length equal to the outputs.

### where

This condition is transmitted over data. The out array will be set to an ufunc result in locations where the condition is True. The outside array will be maintaining its initial interest elsewhere.

Notice when the default out = None produces an uninitialized out list, places within it where the condition is False will stay uninitialized.

## Return Value of Numpy Power

**The Numpy Power functionality treats elements in the very first input selection as a bottom and makes it raised into the power of the corresponding component of the 2nd input array. **

In other words, the NumPy power() function returns an array with components of the first range raised to the second array’s power segment. The result will probably be in integer form.

## Examples to Learn Working of Numpy Power

Before you run the code, you will have to run a little bit of code first.

You essentially have to import NumPy and give it an”alias.”

import numpy as np

### Example 1: Basic Example to Raise Power

To begin with, we are going to work with a really simple illustration.

Here, we are just going to raise an integer into an average power.

To do this, we’ll predict the NumPy power work together with the code np.power(). Then inside of the parenthesis, we’ll supply two arguments. The bottom and the exponent.

```
import numpy as np
y = np.power(4,2)
print(y)
```

**Output:**

`16`

#### Explanation:

This is quite straightforward. It merely computes 4 to the 2nd power that equals 16.

Notice the way the inputs get the job done. The primary enter (4 ) is that the foundation and the next argument (2 ) is that the exponent.

This is precisely the way the remaining cases will do the job.

Let us look at a more complex case.

### Example 2: Calculating Exponents of an Array of Numbers

```
import numpy as np
a = np.array([5,50,100])
print('Original Array is:')
print(a)
print('\n')
print('After Calculating Exponents Array:')
print(np.power(a,2))
```

**Output:**

```
Original Array is:
[ 5 50 100]
After Calculating Exponents Array:
[ 25 2500 10000]
```

**Explanation**

Here in this example we are calculating exponent of an array instead of the base being a single integer. Here array meaning the base will be a group of numbers organized into an array (i.e., a Python list).

So, here in this case the base a simple list of numbers which are **[5, 50, 100]**. In the above example **np.power() **has two inputs **(a,2)**. Therefore, a will be the list of elements which is base **[5, 50, 100]** and **2** will be the power to be raised by elements present in array **‘a’.**

### Example 3: Using Numpy power When Both Base and Exponents are Arrays

Let’s see what will happen when both the base and the exponents are arrays which means instead of one input as array we will take **both of the inputs are arrays**.

Always keep in mind we are using lists because there is nothing like array in Python. Moving forward to the example:

```
import numpy as np
#Declaring a and b
a = [3, 5, 7, 9, 11]
b = [0, 1, 2, 3, 4]
print("Elements of a raised to the power elements of b are: ")
print(np.power(a, b))
```

**Output:**

`[ 1 5 49 729 14641]`

**Explanation**

In the above example 3 we have declared two arrays (lists) naming **‘a’ **and **‘b’**. The array **‘a’** consists of elements **[3, 5, 7, 9, 11]** and array **‘b’** consists of **[0, 1, 2, 3, 4]** respectively. Here the first array **‘a’** is going to be the array of bases, and the second array **‘b’** will be the list of exponents. After that we have calculated the result of **‘a’** to the power **‘b’** with the help of **np.power()** function. So the calculation should go like this:

np.power([[3, 5, 7, 9, 11], [0, 1, 2, 3, 4]) = [ 1 5 49 729 14641]

**Note:** The shape of both the arrays should be same.

### Example 4: Using Numpy Power to calculate exponents with multi-dimensional numpy array

In this example we will learn how to calculate exponents of a two dimensional base array with the help of **np.power()** function. Also the value of exponent will be an array.

Let’s jump directly into the examples and then understand how the things are working. It will be more easy for you to learn through example

```
import numpy as np
a = np.array([[0,1,2,3,4],[4,3,2,1,0],[4,0,1,2,3],[3,4,0,1,2]])
b = [2, 2, 2, 2, 2]
print(np.power(a,b))
```

**Output:**

```
[[ 0 1 4 9 16]
[16 9 4 1 0]
[16 0 1 4 9]
[ 9 16 0 1 4]]
```

**Explanation:**

Let’s breakdown things to make it easy for a beginner. As we know we are using numpy so first we have imported the numpy library as np. After that we have created a 2-D numpy array with the help of **np.array()** function and stored the array in variable **‘a’**.

a = np.array([[0,1,2,3,4],[4,3,2,1,0],[4,0,1,2,3],[3,4,0,1,2]]) print(a)

Let’s print this to make it clear for you guys

```
[[0 1 2 3 4]
[4 3 2 1 0]
[4 0 1 2 3]
[3 4 0 1 2]]
```

This is a fairly simple 2-d NumPy array as you can see.

Now, let’s apply np.power() function on this 2d numpy array with our exponents as [2, 2, 2, 2] and print it out.

`print(np.power(a, [2, 2, 2, 2]))`

**Output:**

```
[[ 0 1 4 9 16]
[16 9 4 1 0]
[16 0 1 4 9]
[ 9 16 0 1 4]]
```

As we know np.power() function takes two arguments the first argument – the array of bases – is a 2-d array. The second argument – the exponents – is a 1-d array. Both have the same number of columns which is **‘5’**. So, what happens here is NumPy power applies the exponents to *every row* and gives us the result.

**This is also known as broadcasting.**

## What Will Happen When an Exponent in Numpy Power is a Negative Number

Keeping it simple **you will get a value error** if the exponent is a negative number

Let’s see it through an example:

```
import numpy as np
#Declaring a and b
a = [3, 5, 7, 9, 11]
b = [0, -1, 2, 3, 4]
print("Elements of a raised to the power elements of b are: ")
print(np.power(a, b))
```

**Output:**

```
print(np.power(a, b))
ValueError: Integers to negative integer powers are not allowed.
```

Here in this example we can see there is a negative number **‘-1’** in the exponent array **b.** So we can’t get our desired result. The Python interpreter will show a value error saying **Integers to negative integer powers are not allowed.**

## Is There a Way to Use Negative Numbers as an Exponent in Python or Numpy Module?

Yes, you can use numpy.float_power() to use negative numbers as an exponent.

## What’s Next?

NumPy is very powerful, and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.

You might like our following tutorials on numpy.

- Numpy Mean: Implementation and Importance
- Using Numpy Random Function to Create Random Data
- NumPy Reshape: Reshaping Arrays With Ease

**Reference:**

## Conclusion

The **numpy power()** function computes exponents in Numpy. It enables us to perform both simple exponentiation like a to the power of b, and can also perform same computation on large numpy arrays also.

If you still have any questions regarding NumPy power function?

Leave your question in the comments below.

*Happy Pythonning!*