# Chi Square Distribution in Python

## In this tutorial you will learn:

• What is Chi Square Distribution?
• Chi Square Distribution Implementation in python
• Visualization of Chi Square Distribution

## Chi Square Distribution

Chi Square Distribution (also known as Chi-Squared Distribution) is a continuous distribution formed by taking the sum of squared standard normal deviates, while a standard normal deviates is the random sample from the normal distribution. As in Chi Square Distribution, the values are squared, so the result produced are always positive. Chi Square distribution is a special case of the gamma distribution. The mean of a Chi Square distribution is its degrees of freedom.

## Square Distribution in Python

Chi Square distribution in python is implemented using an inbuilt function chisquare() which is included in the random module of NumPy library. The chisquare () function takes in two mandatory parameters. First parameter “size” is the size of the output array which could be 1D, 2D, 3D or n-dimensional (depending on the programmer’s requirements). The second parameter “df” is the degree of freedom and it can always be an unsigned integer.
Lets take an example, we will generate a 1D array(1,4) of Chi Square distribution with degree of freedom 3. In the second line of code we are importing random module from NumPy library and in fourth line we are calling the Chi Square function and passing it the size of output array and degree of freedom. In results of this example, you can observe that all the values generated are positive.

`#importing the random modulefrom numpy import random#applying the Chi Square functionres_arr= random. chisquare(df= 2.5, size=4)#printing the resultsprint('1D array of size(1,4) having Chi Square distribution with degree of freedom 3:\n')print(res_arr)`

In this example we will generate a 2D array of Chi Square distribution having size (3,3) with degree of freedom 2.
`#importing the random modulefrom numpy import random#here we are using Chi Square function to generate Chi Square distribution of size 3 x 3 with default scale valueres_arr = random.chisquare(size=(3,3),df=2)print('2D Chi Square Distribution as output from chisquare() function:\n')#printing the resultprint(res_arr)`

Lets take another example, in this example we will generate a 3D array of Chi Square distribution of the size(3,2,6) with degree of freedom 2
`#importing the random modulefrom numpy import random#here we are using Chi Square function to generate Chi Square distribution of size 3 x 2 x 6res = random.chisquare(size=(3,2,6), df=2)print('3D Chi Square Distribution as output from chisquare() function:\n')#printing the resultprint(res)`

## Visualization of Chi Square Distribution

In this example we will visualize the Chi Square Distribution with degree of freedom 4.

`#importing all the required modules and packagesfrom numpy import randomimport matplotlib.pyplot as mplimport seaborn as sb#here we are using Chi Square function to generate distributions of size 1000 with 4 degrees of freedomsb.distplot(random.chisquare(size=1000, df=4), hist=False, label='Chi_Sq Distr')#plotting the graphmpl.show()`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.