Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Binomial Distribution in NumPy
Next article icon

TheChi-Square Distribution is used in statistics when we add up the squares of independent random numbers that follow a standard normal distribution. It is used in hypothesis testing to check whether observed data fits a particular distribution or not. In Python you can use the numpy.random.chisquare() function to generate random numbers that follow Chi-Square Distribution.

Syntax: numpy.random.chisquare(df, size=None)

Example 1: Generate a Single Random Number

To generate a single random number from a Chi-Square Distribution with df=2 (degrees of freedom):

Python
importnumpyasnprandom_number=np.random.chisquare(df=2)print(random_number)

Output :

4.416454073420925

Example 2: Generate an Array of Random Numbers

To generate multiple random numbers:

Python
random_numbers=np.random.chisquare(df=2,size=5)print(random_numbers)

Output :

[0.66656494 3.55985755 1.78678662 1.53405371 4.61716372]

Visualizing the Chi-Square Distribution

Visualizing the generated numbers helps to understand the behavior of the Chi-Square distribution. You can plot a histogram or a density plot using libraries likeMatplotlib and Seaborn.

Python
importnumpyasnpimportmatplotlib.pyplotaspltimportseabornassnsdf=1size=1000data=np.random.chisquare(df=df,size=size)sns.displot(data,kind="kde",color='purple',label=f'Chi-Square (df={df})')plt.title(f"Chi-Square Distribution (df={df})")plt.xlabel("Value")plt.ylabel("Density")plt.legend()plt.grid(True)plt.show()

Output:

ChiSquare-Distribution
Chi-Square Distribution

The above chart shows theshape of the Chi-Square distribution fordf = 1:

  • Thex-axis represents the values generated.
  • They-axis shows thedensity (how often values occur).
  • Withdf = 1 the curve isskewed to the rightmeaning lower values occur more frequently and higher values become rarer.

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp