Finding Powers Element-wise

Tags: Array, NumPy, power

Write a NumPy program to get the powers of an array values element-wise. Return the array of powers.

Example 1:

Input: np.array([1, 2, 3, 4, 5]), 2 
Output: array([ 1, 4, 9, 16, 25])

Example 2:

Input: np.array([10, 20, 30, 40, 50]), 3 
Output: array([ 1000, 8000, 27000, 64000, 125000])

You can use np.power() function to get the powers of an array values element-wise.

import numpy as np

def find_powers(array, power):
    return np.power(array, power)

print(find_powers(np.array([1, 2, 3, 4, 5]), 2))
print(find_powers(np.array([10, 20, 30, 40, 50]), 3))

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!