How to perform Chi Square test in python?
The Chi-Squared test is a applied math hypothesis test that assumes (the null hypothesis) that the determined frequencies for a categorical variable match the expected frequencies for the specific variable. The test calculates a data point that incorporates a chi-squared distribution.
The number of observations for a class might or might not an equivalent. still, we will calculate the expected frequency of observations in every social group and see whether or not the partitioning of interests.
If Statistic >= Critical Value: reject null hypothesis (Ho), model dependent. If Statistic < Critical Value: fail to reject null hypothesis (Ho), model independent.
# chi-squared test with similar proportions from scipy.stats import chi2_contingency from scipy.stats import chi2 import pandas as pd
Creating a sample-2d table to calculate sample stat, p, dof and expected values. Predefining prob as 0.9 to calculate chi values.
# contingency table data = [[37, 73, 102, 400], [10, 45, 200, 300]] print(data) stat, p, dof, expected = chi2_contingency(data) # interpret test-statistic prob = 0.90 chi = chi2.ppf(prob, dof) chi
Now we will compare the chi value to stat value to know whether we reject the null hypothesis or fail to reject the null hypothesis.
if abs(stat) >= chi: print('reject Ho') else: print('fail to reject Ho')