View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we are going to see how to Perform a Chi-Square Goodness of Fit Test in Python
The Chi-Square Goodness of fit test is a non-parametric statistical hypothesis test that’s used to determine how considerably the observed value of an event differs from the expected value. it helps us check whether a variable comes from a certain distribution or if a sample represents a population. The observed probability distribution is compared with the expected probability distribution.
null hypothesis: A variable has a predetermined distribution.
Alternative hypotheses: A variable deviates from the expected distribution.
Example 1: Using stats.chisquare[] function
In this approach we use stats.chisquare[] method from the scipy.stats module which helps us determine chi-square goodness of fit statistic and p-value.
Syntax: stats.chisquare[f_obs, f_exp]
parameters:
- f_obs : this parameter contains an array of observed values.
- f_exp : this parameter contains an array of expected values.
In the below example we also use the stats.ppf[] method which takes the parameters level of significance and degrees of freedom as input and gives us the value of chi-square critical value. if chi_square_ value > critical value, the null hypothesis is rejected. if chi_square_ value