Chi-square 用途3

Data Science Day 5:

Chi-Square Application 2:

Test Independence of Two categorical variables, or known asContingency Table.

We use the Chi-Square test of Independence to check if two categorical variables are independent, or have a strong association.

Example 1: Ice-cream Favor VS. Buyer's Gender

We want to see if there is a preference for ice-cream favor based on the gender of people eating it

[table id= 3 \]

H0 (Null Hypothesis):The preference forIce-Cream FavorandBuyer'sGenderareIndependent, (There is no association between Ice-Cream Favor and Gender selection)

Solution:

we will useSciPypackage andchi2_contingencyfunction in Python.

Python Code:

favor_gender=np.array([[60,120,120,80],[120,250,200,50]])

stats.chi2_contingency(favor_gender)

Result:

We see thep-value is 4.3e-08, which is significantly < 0.05. So weRejectthe Null Hypotheses and Conclude the Ice-Cream Favor is dependent on the buyer's Gender.Note:If if the totalcount is <5the result might be biased, and if it is2 x 2 table(2 categorical variables with 2 observations) we will proceed withFisher's ExactTest.

Data Visualization:

From the visualization, we can see the indeed there's more girls prefer Green Tea favor Ice-cream. :)


Source Code:

To be continue....

你可能感兴趣的:(Chi-square 用途3)