Python:NumPy .power()
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.
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), thenm * n * ksamples are drawn. IfNone(default), a single value is returned ifais a scalar. Otherwise,np.array(a).sizesamples 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 = 2print(np.random.power(2))# Single sample with a = 7print(np.random.power(7))
A possible output of this code is:
0.32798032845995770.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 = 2print(np.random.power(2, 3))# 10 values with a = 10print(np.random.power(10, 10))# 3 values with a very large shape parameterprint(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.984087320.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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
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