【问题标题】:Chi-Squared test for independence with one data column as integer and other as object?一个数据列作为整数,另一个作为对象的独立性卡方检验?
【发布时间】:2019-10-04 08:35:49
【问题描述】:

我正在尝试在 python 中对独立性进行假设检验,但我的一个数据列(财务)具有浮点数据类型,而另一列(性别)具有对象数据类型。

我提出了以下假设: 何:财务不分性别 哈:财务状况与性别有关

我尝试直接使用输入,但出现以下错误: " 无法将字符串转换为浮点数:'female'"

import pandas      as pd
import numpy       as np
import scipy.stats as stats

test = np.array(df['Gender'],df['Finances'])
chi_sq_Stat, p_value, deg_freedom, exp_freq = stats.chi2_contingency(test)

print('Chi-square statistic %3.5f P value %1.6f Degrees of freedom %d' %(chi_sq_Stat, p_value,deg_freedom))

我期待一些 P 值来验证我的假设。

我附上了一张数据集的图片

【问题讨论】:

    标签: python pandas chi-squared hypothesis-test statistical-test


    【解决方案1】:

    尝试将作为名义变量的性别映射为一组固定的数字,如下所示:

    gender_mapping = {"male":1 ,"female":0}
    df.Gender = df.Gender.map(gender_mapping)
    df.head()
    Gender  Finances
    0   1   1
    1   0   2
    2   1   3
    3   0   2
    4   1   3
    
    

    【讨论】:

      猜你喜欢
      • 2020-10-03
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 2020-10-12
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多