Python:NumPy .power()

emna.akremi's avatar
Published Jul 18, 2025

The .power() function, also known as the power function distribution, is part of the NumPy random module. It draws samples from a power distribution with positive exponent a - 1 in the range [0, 1]. This function is often used in statistical simulations, hypothesis testing, and generating synthetic data for machine learning.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of the world's fastest growing and most popular programming language used by software engineers, analysts, data scientists, and machine learning engineers alike.
    • Beginner Friendly.
      17 hours

Syntax

random.power(a, size=None)

Parameters:

  • a (float or array_like of floats): The shape parameter of the distribution. It must be positive.
  • size (int or tuple of ints, optional): Specifies the output shape. If specified as (m, n, k), then m * n * k samples are drawn. If None (default), a single value is returned if a is a scalar. Otherwise, np.array(a).size samples are drawn.

Return value:

An ndarray of random floats drawn from a power distribution over the interval [0, 1), with shape determined by size.

Example 1: Drawing a Single Value (size=None)

When size=None (default), np.random.power(a) returns a single float from the power distribution over the range [0, 1):

import numpy as np
# Single sample with shape parameter a = 2
print(np.random.power(2))
# Single sample with a = 7
print(np.random.power(7))

A possible output of this code is:

0.3279803284599577
0.9487782510180921

When no size is specified, a single float is returned.

Example 2: Drawing Multiple Values Using an Integer size

When size is an integer, it specifies how many random values to draw from the power distribution:

import numpy as np
# 3 values with a = 2
print(np.random.power(2, 3))
# 10 values with a = 10
print(np.random.power(10, 10))
# 3 values with a very large shape parameter
print(np.random.power(1000, 3))

The possible output of this code is:

[0.71399645 0.67523938 0.59913375]
[0.89043235 0.94651471 0.74650338 0.97812045 0.73832165 0.98408732
0.94639479 0.89403161 0.95649183 0.91259268]
[0.99993867 0.99989337 0.99973659]

Codebyte Example

This codebyte example generates multi-dimensional arrays by passing a tuple as the size argument:

Code
Output

All contributors

Learn Python:NumPy on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of the world's fastest growing and most popular programming language used by software engineers, analysts, data scientists, and machine learning engineers alike.
    • Beginner Friendly.
      17 hours