python 卡方检验批量筛选_卡方检验(python代码实现)
醫(yī)藥統(tǒng)計項目QQ:231469242
分類變量檢驗方法
卡方分布繪圖
如果多個符合正態(tài)分布的獨立隨機變量z1,z2,z3.....zk,
z1+z2+z3+....z_k呈現(xiàn)卡方分布,自由度k.
有幾個正態(tài)分布相加,就有幾個自由度
# -*- coding: utf-8 -*-
# Toby QQ:231469242
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import seaborn as sns
import math,pylab,matplotlib,numpy
from matplotlib.font_manager import FontProperties
#設(shè)置中文字體
font=FontProperties(fname=r"c:windowsfontssimsun.ttc",size=15)
n=10
#繪制自由度為n的卡方分布圖,n表示生成卡方數(shù)組的個數(shù)
def Get_chisquareDatas(n):
#標(biāo)準(zhǔn)正太分布
normalDistribution=stats.norm(0,1)
list_data=[]
for i in range(n):
normal_data=normalDistribution.rvs(30)
chisquare_data=normal_data**2
list_data.append(chisquare_data)
return list_data
def Plot_chisquare(n):
list_data=Get_chisquareDatas(n)
sum_data=sum(list_data)
plt.hist(sum_data)
Plot_chisquare(2)
Plot_chisquare(3)
Plot_chisquare(10)
官方繪圖代碼
# -*- coding: utf-8 -*-
from scipy.stats import chi2
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 1)
df = 20
mean, var, skew, kurt = chi2.stats(df, moments='mvsk')
#繪制函數(shù)的起始點和終止點
#pdf為概率密度函數(shù)
#百分比函數(shù)(PPF) :the inverse of the CDF. PPF 函數(shù)和連續(xù)分布函數(shù)CDF相逆,
#比如輸入哪一個點,可以得到低于等于20的概率?
#ppf(0.01, df)表示輸入哪個點,得到概率低于0.01
initial=chi2.ppf(0.01, df)
end=chi2.ppf(0.99, df)
x = np.linspace(initial,end, 100)
#概率密度函數(shù)用于繪圖
ax.plot(x, chi2.pdf(x, df), 'r-', lw=5, alpha=0.6, label='chi2 pdf')
plt.title("df is %d"%df)
plt.show()
卡方檢驗代碼
可汗學(xué)院的問題
# -*- coding: utf-8 -*-
'''
卡方公式(o-e)^2 / e
期望值和收集到數(shù)據(jù)不能低于5,o(observed)觀察到的數(shù)據(jù),e(expected)表示期望的數(shù)據(jù)
(o-e)平方,最后除以期望的數(shù)據(jù)e
'''
import numpy as np
from scipy import stats
from scipy.stats import chisquare
list_observe=[30,14,34,45,57,20]
list_expect=[20,20,30,40,60,30]
std=np.std(data,ddof=1)
print(chisquare(f_obs=list_observe, f_exp=list_expect))
p=chisquare(f_obs=list_observe, f_exp=list_expect)[1]
'''
返回NAN,無窮小
'''
if p>0.05 or p=="nan":
print"H0 win,there is no difference"
else:
print"H1 win,there is difference"
contigency table聯(lián)立表
測試數(shù)據(jù)
第一行:草本藥1,草本藥2,安慰劑
第二行:生病和非生病
H0:草本藥和疾病無關(guān)系
H1:草本藥和疾病有關(guān)系
可汗學(xué)院計算出來的卡方值2.53;自由度2,顯著性0.1,的關(guān)鍵值4.6
卡方值2.53
python代碼與可汗學(xué)院算出結(jié)果一致,此版本體現(xiàn)算法推導(dǎo)過程。缺點就是要自己計算出期望值列表
# -*- coding: utf-8 -*-
'''
卡方公式(o-e)^2 / e
期望值和收集到數(shù)據(jù)不能低于5,o(observed)觀察到的數(shù)據(jù),e(expected)表示期望的數(shù)據(jù)
(o-e)平方,最后除以期望的數(shù)據(jù)e
聯(lián)立表contigency table計算
'''
from scipy.stats import chisquare
list_observe=[34,38,28,50]
list_expect=[29.76,42.2,32.24,45.77]
row=2
colume=2
def Contigency_table(row,colume,list_observe,list_expect):
degreeFreedom=(row-1)*(colume-1)
print(chisquare(f_obs=list_observe, f_exp=list_expect,ddof=degreeFreedom))
p=chisquare(f_obs=list_observe, f_exp=list_expect)[1]
if p>0.05 or p=="nan":
print"H0 win,there is no difference"
else:
print"H1 win,there is difference"
Contigency_table(row,colume,list_observe,list_expect)
此版本不用算出期望值,更加方便,參考的是2*2聯(lián)立表,自由度=1,critical value=2.7
# -*- coding: utf-8 -*-
#獨立性檢驗test for independence,也是卡方檢驗chi_square
#前提條件:a,b,c,d 必須大于5
#2.706是判斷標(biāo)準(zhǔn)(90概率),值越大,越有關(guān),值越小,越無關(guān)
def value_independence(a,b,c,d):
if a>=5 and b>=5 and c>=5 and d>=5:
return ((a+b+c+d)*(a*d-b*c)**2)/float((a+b)*(c+d)*(a+c)*(b+d))
#返回True表示有關(guān)
#返回False表示無關(guān)
def judge_independence(num_independence):
if num_independence>2.706:
print ("there is relationship")
return True
else:
print("there is no relationship")
return False
a=34
b=38
c=28
d=50
chi_square=value_independence(a,b,c,d)
relation=judge_independence(chi_square)
python官網(wǎng)版本,更加方便和科學(xué)
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2_contingency.html
import scipy.stats as stats
data = np.array([[43,9],
[44,4]])
V, p, dof, expected = stats.chi2_contingency(data)
print(p)
python機器學(xué)習(xí)-乳腺癌細胞挖掘(博主親自錄制視頻)
總結(jié)
以上是生活随笔為你收集整理的python 卡方检验批量筛选_卡方检验(python代码实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 铃声
- 下一篇: Python实现QQ音乐爬取下载最新可用