🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.power()

AI & DATA SCIENCE // np-power

np.power() raises each element of one array to the power of the corresponding element in another array (or a scalar exponent), element-wise, and is exactly what the ** operator calls on ndarrays.

Syntax

np.power(x1, x2)
x1 ** x2

Deep Dive Course

np.power(base, exponent) computes base raised to exponent for each pair of elements, supporting a scalar exponent applied to every element, an array of matching shape for per-element exponents, or the usual broadcasting rules for compatible-but-different shapes. Raising a negative base to a fractional exponent, or an integer base array to a negative integer exponent, can produce nan or raise an error depending on dtype, since the mathematical result may not be a real number, or may not fit the array's integer type.

1Understanding np.power()

np.power(base, exponent) computes base raised to exponent for each pair of elements, supporting a scalar exponent applied to every element, an array of matching shape for per-element exponents, or the usual broadcasting rules for compatible-but-different shapes. Raising a negative base to a fractional exponent, or an integer base array to a negative integer exponent, can produce nan or raise an error depending on dtype, since the mathematical result may not be a real number, or may not fit the array's integer type.

💡

Raising an integer array to a negative power raises a ValueError, since NumPy can't represent a fractional result in an integer array — convert to a float dtype first if you need negative or fractional exponents.

editor.html
import numpy as np

base = np.array([1, 2, 3, 4])
print(np.power(base, 2))
localhost:3000

2Practical Example

Here is a real-world application of np.power() showing how it is used in production NumPy code.

editor.html
import numpy as np

base = np.array([2, 3, 4])
exponents = np.array([1, 2, 3])
print(np.power(base, exponents))
localhost:3000

3Best Practices

Follow these guidelines when working with np.power():

1. Convert to a float dtype before using negative or fractional exponents, since integer arrays can't represent the resulting fractional values

2. Use np.sqrt(x) instead of x ** 0.5 for square roots specifically, since it's more explicit about intent and can be marginally faster

3. Watch for nan results when raising negative bases to fractional exponents, since the mathematical result isn't a real number in that case

⚠️

Tip: Raising an integer array to a negative power raises a ValueError, since NumPy can't represent a fractional result in an integer array — convert to a float dtype first if you need negative or fractional exponents.

editor.html
import numpy as np

base = np.array([1, 2, 3, 4])
print(np.power(base, 2))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

base = np.array([1, 2, 3, 4])
print(np.power(base, 2))
Example 02Advanced Example
import numpy as np

base = np.array([2, 3, 4])
exponents = np.array([1, 2, 3])
print(np.power(base, exponents))

Best Practices

  • Convert to a float dtype before using negative or fractional exponents, since integer arrays can't represent the resulting fractional values
  • Use np.sqrt(x) instead of x ** 0.5 for square roots specifically, since it's more explicit about intent and can be marginally faster
  • Watch for nan results when raising negative bases to fractional exponents, since the mathematical result isn't a real number in that case

Interview Question

Why does raising an integer NumPy array to a negative power raise a ValueError instead of returning a fractional result?

Hint: Think about what dtype the result would need to be represented in.

A negative exponent mathematically produces a fractional result, like 2 to the power of -1 being 0.5, but the input array's dtype is a fixed integer type, which can only represent whole numbers. NumPy can't silently change the output's dtype to a float mid-operation for an integer-typed ufunc call, so instead of producing a wrong, truncated answer, it raises a ValueError, requiring you to explicitly convert the array to a float dtype first if you actually need fractional powers.

Exercises

MediumPractice using np.power() in a real scenario.
View Solution
import numpy as np

base = np.array([1, 2, 3, 4])
print(np.power(base, 2))

Frequently Asked Questions

Why does raising an integer NumPy array to a negative power raise a ValueError instead of returning a fractional result?

A negative exponent mathematically produces a fractional result, like 2 to the power of -1 being 0.5, but the input array's dtype is a fixed integer type, which can only represent whole numbers. NumPy can't silently change the output's dtype to a float mid-operation for an integer-typed ufunc call, so instead of producing a wrong, truncated answer, it raises a ValueError, requiring you to explicitly convert the array to a float dtype first if you actually need fractional powers.

Related Functions

np-sqrtnp-multiplyndarray-dtype