Chi-Square Distribution in NumPy Last Updated : 15 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The Chi-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)df: Degrees of freedom (denoted by k) which affects the shape of the distribution.size: The number of random numbers you want to generate or the shape of the returned array.Example 1: Generate a Single Random NumberTo generate a single random number from a Chi-Square Distribution with df=2 (degrees of freedom): Python import numpy as np random_number = np.random.chisquare(df=2) print(random_number) Output :4.416454073420925Example 2: Generate an Array of Random NumbersTo 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 DistributionVisualizing the generated numbers helps to understand the behavior of the Chi-Square distribution. You can plot a histogram or a density plot using libraries like Matplotlib and Seaborn. Python import numpy as np import matplotlib.pyplot as plt import seaborn as sns df = 1 size = 1000 data = 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: Chi-Square DistributionThe above chart shows the shape of the Chi-Square distribution for df = 1:The x-axis represents the values generated.The y-axis shows the density (how often values occur).With df = 1 the curve is skewed to the right meaning lower values occur more frequently and higher values become rarer. Comment More info J jitender_1998 Follow Improve Article Tags : Python Python-numpy Python numpy-Random Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like