## Introduction to NumPy power

In python, we can calculate the exponentiation of variables by using NumPy power; it is a tool that leverages us to have the exponentiation value of array elements. To calculate the exponentiation of variable, we have mainly two terms: base and power; the base is the variable that we want to calculate, and power is the exponent here. In python, we use this NumPy power to calculate the single variable and handle the array with it.

### Syntax of NumPy power

This NumPy power function takes two parameters as the value if we want to deal with the array.

**1. In the case of the array, we can say that this** takes two parameters, one as the base value and another as the exponent value or the power.

**Example:**

`NumPy.power(base_array, exponent_array)`

**Example:**

`NumPy.power(array1, array2)`

**2.** **In the case of a single element:** This will also take two parameters as base and exponent.

**Example:**

`NumPy.power(base, exponent)`

**Example:**

`NumPy.power(2, 8)`

### How do power Function work in NumPy?

In order to calculate the exponentiation value of any element or array, we need to have two parameters in place.

Let’s see how power works in a simple case:

We have one base element and another one as the exponent element, and while calculating the exponentiation value, we multiply the base with exponent times.

Suppose we have 2 as the base and 2 as the exponent so we will be going to calculate this as:

**Example:**

Here 2 base and 2 exponent ; 2*2 =4

In python, we can also calculate the array values.

But how will it work let’s see one example?

**Array Exponentiation Value**

In the python official document, we have two parameters for the NumPy power function, and these parameters are termed as array base value and array exponent value. We can also refer to them as x1 and x2 as per the documentation given.

**Base as array (x1):**In these parameters, we pass an array containing various elements. These elements would be act as the base for the power function whose exponentiation value we want to calculate, and this parameter is a required or mandatory kind of parameter. So we have to provide on input here for the power function.

**array = (1, 2, 3, 4 , 5..so on..)**

**Exponent as array (x2):**In this parameter, we pass an array containing the various elements, and these elements would act as an exponent for the base array. This parameter (x2) is also a required or mandatory kind of parameter. We can pass array, tuple, list anything here. Also, we can pass a single value as an exponent if we want to work with a single element calculation.

**array = (2, 4, 5, ..so on..)**

Both the parameter that we have seen are the positional parameter here. Positional parameters are those parameters that we need to pass in the same position as they are defined, so that means the first parameter would always be the array’s base, and the second one will be the exponent of the array. Both the array have to be of the same structure. Suppose x1[1] would be the base for x2[1], and so on, the calculation will be happening.

**Example:**

```
import numpy as mynum
x1 = [1, 2, 3,4, 5]
x2 = [2, 4, 6, 8, 9]
result = mynum.power(x1, x2)
```

In the above example, we are creating two arrays named as x1 and x2. In order to use the NumPy power, we have to import the NumPy package at the starting of the program. X1 is the base array, and x2 is the exponent array here. At the last line, we are calling the power function and passing both the input params there. So 1 will be calculating against the 2 here and so on.

**Points to be remembered while working with the NumPy power function:**

- These parameters are optional parameters.
- First, param will always be base, and second, will always be an exponent of the base.
- Single parameter values can also be calculated by using this function.

### Examples of NumPy power

Given below are the examples of NumPy power:

#### Example #1

In this example, we calculate the exponentiation value of the array parameters by using the power function.

**Code:**

```
import numpy as mynum
# defining arrays here .. x1 and x2
x1 = [1, 2, 3,4, 5]
x2 = [2, 4, 6, 8, 9]
#array before power function
print("array one is ::" , x1)
print("array two is ::" , x2)
#calling power function here ..
result = mynum.power(x1, x2)
#print result
print("result is ::", result)
```

**Output:**

#### Example #2

In this example, we calculate the exponentiation value of the single element by using the power function.

**Code:**

```
import numpy as mynum
# defining arrays here .. x1 and x2
x1 = 5
x2 = 2
#array before power function
print("array one is ::" , x1)
print("array two is ::" , x2)
#calling power function here ..
result = mynum.power(x1, x2)
#print result
print("result is ::", result)
```

**Output:**

#### Example #3

In this example, we calculate the exponentiation value of the float element of the array by using the power function available inside the NumPy library.

**Code:**

```
import numpy as mynum
# defining arrays here .. x1 and x2
x1 = [1.5, 2.3, 2.2, 5.3, 9]
x2 = [2, 2, 2, 2, 1]
#array before power function
print("array one is ::" , x1)
print("array two is ::" , x2)
#calling power function here ..
result = mynum.power(x1, x2)
#print result
print("result is ::", result)
```

**Output:**

### Conclusion

NumPy library provides us with many different functions to deal with and perform operations on array elements, and the Power function is one of them, which is used to calculate the exponentiation value of single, tuple, list, and array based elements. In this function, both the parameter are mandatory and positional parameters, as we saw. So we can easily calculate the values of the array by just passing the element inside it.

### Recommended Articles

This is a guide to NumPy power. Here we discuss the introduction, how power function works in NumPy? along with examples, respectively. You may also have a look at the following articles to learn more

4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses