Ben*_*ngt 11 python statistics python-3.x
我需要计算cohen的d来确定实验的效果大小.我可以使用的声音库中是否有任何实现?如果没有,那么什么是好的实施?
sky*_*aut 15
在两组具有相同大小的特殊情况下,上述实现是正确的.基于维基百科和Robert Coe文章中的公式的更一般的解决方案是下面显示的第二种方法.请注意,分母是汇总标准差,通常只有在两组人口标准差相等的情况下才适用:
from numpy import std, mean, sqrt
#correct if the population S.D. is expected to be equal for the two groups.
def cohen_d(x,y):
nx = len(x)
ny = len(y)
dof = nx + ny - 2
return (mean(x) - mean(y)) / sqrt(((nx-1)*std(x, ddof=1) ** 2 + (ny-1)*std(y, ddof=1) ** 2) / dof)
#dummy data
x = [2,4,7,3,7,35,8,9]
y = [i*2 for i in x]
# extra element so that two group sizes are not equal.
x.append(10)
#correct only if nx=ny
d = (mean(x) - mean(y)) / sqrt((std(x, ddof=1) ** 2 + std(y, ddof=1) ** 2) / 2.0)
print ("d by the 1st method = " + str(d))
if (len(x) != len(y)):
print("The first method is incorrect because nx is not equal to ny.")
#correct for more general case including nx !=ny
print ("d by the more general 2nd method = " + str(cohen_d(x,y)))
Run Code Online (Sandbox Code Playgroud)
输出将是:
d by 1st method = -0.559662109472第一种方法不正确,因为nx不等于ny.d由更一般的第二种方法= -0.572015604666
Ben*_*ngt 11
从Python3.4开始,您可以使用该statistics
模块计算扩展和平均指标.有了这个,Cohen的d可以很容易地计算出来:
from statistics import mean, stdev
from math import sqrt
# test conditions
c0 = [2, 4, 7, 3, 7, 35, 8, 9]
c1 = [i * 2 for i in c0]
cohens_d = (mean(c0) - mean(c1)) / (sqrt((stdev(c0) ** 2 + stdev(c1) ** 2) / 2))
print(cohens_d)
Run Code Online (Sandbox Code Playgroud)
输出:
-0.5567679522645598
Run Code Online (Sandbox Code Playgroud)
所以我们观察到中等效果.
在 Python 2.7 中,您可以使用numpy
一些警告,正如我在改编 Python 3.4 中 Bengt 的答案时发现的那样。
from __future__ import division
ddof=1
为std
函数 ,即numpy.std(c0, ddof=1)
。numpy 的标准差默认行为是除以n
,而 withddof=1
它会除以n-1
。代码
from __future__ import division #Ensure division returns float
from numpy import mean, std # version >= 1.7.1
from math import sqrt
import sys
def cohen_d(x,y):
return (mean(x) - mean(y)) / sqrt((std(x, ddof=1) ** 2 + std(y, ddof=1) ** 2) / 2.0)
if __name__ == "__main__":
# test conditions
c0 = [2, 4, 7, 3, 7, 35, 8, 9]
c1 = [i * 2 for i in c0]
print(cohen_d(c0,c1))
Run Code Online (Sandbox Code Playgroud)
输出将是:
-0.556767952265
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8196 次 |
最近记录: |