.chisquare()
In the random
module of NumPy, the .chisquare()
method generates random samples from a chi-square distribution. It is frequently applied in hypothesis testing to assess whether observed data differ significantly from expected outcomes.
Syntax
numpy.random.chisquare(df, size=None)
Parameters:
df
(float or array-like of floats): Degrees of freedom (must be positive). This parameter determines the shape of the chi-square distribution.size
(Optional): The shape of the output array. If not specified, a single value is returned.
Return value:
In NumPy, the .chisquare()
function returns a randomly drawn sample or an array of samples from the chi-square distribution with df
degrees of freedom.
- If
size
isNone
, a single random value is returned. - If
size
is specified, an array of random values is returned, with the shape of the array determined bysize
.
Example: Generating Random Values from a Chi-Square Distribution in NumPy
The example below shows how to generate random values from a chi-square distribution with 2 degrees of freedom:
import numpy as np# Generate 3 random values from a chi-square distribution with 2 degrees of freedomresult = np.random.chisquare(2, size = 3)print(result)
A possible output of this code can be:
[6.00571639 0.49778027 1.38101813]
This code randomly draws 3 values from a chi-square distribution with 2 degrees of freedom.
Codebyte Example
In this Codebyte example, we sample 3 values from a chi-square distribution with 1 degree of freedom:
Note: The output may differ at each execution, as the selection is random.
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours