Numpy power – Raising another number to the power of

Add A Heading

We are going to solve a very exciting problem today! We will take two arrays of the same length each. For each value of the first array, we will raise the power to the same value as the corresponding value of the second array. Let us understand this one theoretically.

We have two arrays

  • x=[1,2,3,4,5,6,8]
  • y=[5,4,3,2,1,9,3]

Let us get the values of x[i]y[i] for all i in the range 0 to the length of arrays.

  • 15 = 1
  • 24 = 16
  • 33 = 27
  • 42 = 16
  • 51 = 5
  • 69 = 10077696
  • 83 = 512

We want our result or output in an array such as [1, 16, 27, 16, 5, 10077696, 512].

Working with Numpy.power() method

We will move forward to solve the problem by sequencing each step that let you explain as justified. We are going with our google collab for our coding today. You guys must know that google collab provides the best platform to work on python as well. Without getting late. Let us get into it.

Step1: Import numpy

We will import the required module in our first step. As we are going to use the NumPy.power() function we need to import the dependent module which is the Numpy module.

import numpy

Step 2: Creating arrays to raise to power

Let us take or create two arrays as below comprising of some values.

x=[1,2,3,4,5,6,8]
y=[5,4,3,2,1,9,3]

Step 3: Confirm length of arrays is same

In this step, we will check whether the length of two arrays is equal or not, if yes we will further move forward to let our problem be solved. Else if not it will not move further.

if len(x) == len(y):
  #logicc to get our Resulatant array
else :
 print("arrays are not of equal size. Plz review again")

Step 4: Run the numpy.power() method on each item

In this Step, we will create our logic and code. We are taking a loop to solve our problem. Inside the loop, we are using the NumPy.power() method to get our result and get loaded into the z array. Have a quick look for a better understanding.

 for i in x:
    output = numpy.power(i,y[w])
    w = w + 1
    z.append(output)
 print(z)  

The overall code to solve our problem is as below.

import numpy
x=[1,2,3,4,5,6,8]
y=[5,4,3,2,1,9,3]
z=[]
w = 0
if len(x) == len(y):
 for i in x:
    output = numpy.power(i,y[w])
    w = w + 1
    z.append(output)
 print(z)  
else :
 print("arrays are not of equal size. Plz review again")

The above code snippet will give out as below.

[1, 16, 27, 16, 5, 10077696, 512]

This time we will pass two arrays with different shapes or different indices. Let us see the result after implementing the same code.

import numpy
x=[1,2,3,4,5,6,8,8,9,4]  #index = 10
y=[5,4,3,2,1,9,3]          #index = 7
z=[]
w = 0
if len(x) == len(y):
 for i in x:
    output = numpy.power(i,y[w])
    w = w + 1
    z.append(output)
 print(z)  
else :
 print("arrays are not of equal size. Please review")

The above code snippet will give results as follows.

arrays are not of equal size. Please review

Accepting user input to run numpy.power()

In this step, instead of creating our arrays in the program or code snippet, We will let the user input the values for both arrays. First of all, we need to define an empty array. and let the user decide how many inputs he is going to enter. then using the loop he can enter all the required values for the array.

# creating an empty array
x = []
  
# number of elements as input
n = int(input("Enter the number of values you want to enter : "))
  
# using for loop to enter all the values
for i in range(0, n):
    value = int(input())
    x.append(value) 

The above code snippet will let us enter the values into our x array as follow.

Like this, You can enter the values for the y array as well. And finally using these arrays for our implementation.

#importing required module
import numpy
# creating  x array
x = []
n = int(input("Enter the number of values you want to enter : "))
for i in range(0, n):
    value = int(input())
    x.append(value) 

#creating y array
y = []
m = int(input("Enter the number of values you want to enter : "))
for i in range(0, m):
    value = int(input())
    y.append(value) 

#implementing logic
z=[]
w = 0
if len(x) == len(y):
 for i in x:
    output = numpy.power(i,y[w])
    w = w + 1
    z.append(output)
 print(z)  
else :
 print("arrays are not of equal size. Plz review again")

You can see that we had entered the value inputs for each array and used both arrays to get the power of values of 1st array to the power raised equal to the value of the second array.

Summary

In this way, We solved our required problem. Hope you guys must have understood each step sequentially along with our code snippets. We must visit again to solve some exclusive exciting problems for you again. We won’t let you wait. Thank you.