请有人检查下面的问题和我的代码,让我知道为什么我没有得到预期的结果?
问题:表按教育显示婚姻状况的偶然性表。采用卡方检验法对教育程度的Homogenity.contingency婚姻状况表进行检验.
通过执行以下命令python查看表
from prettytable import PrettyTable
t = PrettyTable([‘Marital Status’,’Middle school’, ‘High School’,’Bachelor’,’Masters’,’PhD’])
t.add_row([‘Single’,18,36,21,9,6])
t.add_row([‘Married’,12,36,45,36,21])
t.add_row([‘Divorced’,6,9,9,3,3])
t.add_row([‘Widowed’,3,9,9,6,3])
print (t)
exit()假设
零假设:不同类型的教育程度在婚姻状况上的分布没有差异。
另一种假设:有区别
编码
1.import chi2_contingency和from scipy.stats import chi2。
2.按教育程度宣布一个2D数组,其中包含婚姻状况应急表中提到的值。
3.计算和打印
-卡方统计-自由度-P值-提示:使用chi2_contigency()函数4.假定α值为0.0 5
5.将P值与α值进行比较,确定是否拒绝零假设。
-如果被拒绝打印“拒绝零假设”-否则打印“没有拒绝零假设”
样本输出2.33 4.5 8.9拒绝零假设
我的代码:
from scipy.stats import chi2_contingency
from scipy.stats import chi2
table= [ [18,36,21,9,6],[12,36,45,36,21], [6,9,9,3,3],[3,9,9,6,3] ]
stat,p,dof,expected = chi2_contingency(table)
prob = 0.95
critical = chi2.ppf(prob, dof)
if abs(stat) >= critical:
print(stat, dof ,p ,'Reject the Null Hypothesis')
else:
print(stat, dof ,p ,'Failed to reject the Null Hypothesis')谢谢你,Rakesh
发布于 2020-07-28 05:20:59
使用prob = 0.05而不是0.95
谢谢!
发布于 2021-02-11 06:39:13
from scipy.stats import chi2_contingency
from scipy.stats import chi2
def chi_test():
'''
Output
1. stat: Float
2. dof : Integer
3. p_val: Float
4. res: String
'''
#Note: Round off the Float values to 2 decimal places.
table=[[18,36,21,9,6],[12,36,45,36,21],[6,9,9,3,3],[3,9,9,6,3]]
stat,p_val,dof,res=chi2_contingency(table)
prob=0.95
critical=chi2.ppf(prob, dof)
if abs(stat) >= critical:
res = 'Reject the Null Hypothesis'
else:
res= 'Failed to reject the Null Hypothesis'
alpha=1.0-prob
if p_val <= alpha:
res = 'Reject the Null Hypothesis'
else:
res = 'Failed to reject the Null Hypothesis'
stat = round(stat,2)
dof = round(dof,2)
p_val = round(p_val,2)
return stat,dof,p_val,res
if __name__=='__main__':
print(chi_test())发布于 2020-12-30 12:14:56
从scipy.stats导入chi2_contingency从scipy.stats导入chi2
def chi_test():# Notation输出1.stat:Float2.dof:整型3. p_val: Float4.res: String
代码
table=[[18,36,21,9,6],[12,36,45,36,21],[6,9,9,3,3],[3,9,9,6,3]]
stat,dof,p_val,res=chi2_contingency(table)
prob=0.95
critical=chi2.ppf(prob, dof)
if abs(stat) >= critical:
print('Reject the Null Hypothesis')
else:
print('Failed to reject the Null Hypothesis')
alpha=1.0-prob
if p_val <= alpha:
print('Reject the Null Hypothesis')
else:
print('Failed to reject the Null Hypothesis')
return stat,dof,p_val,res 如果name=='main':打印(chi_test())
https://stackoverflow.com/questions/62925425
复制相似问题