ITPub博客

首页 > IT职业 > IT图书 > Chi-square 用途3

Chi-square 用途3

IT图书 作者:sick 时间:2021-09-09 10:08:34 0 删除 编辑

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....



作者:乌然娅措
链接:https://www.jianshu.com/p/83852355a83e


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3199/viewspace-2817298/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    82
  • 访问量
    519