gpt4 book ai didi

python - 为什么 scipy.stats.mstats.pearsonr 结果与 scipy.stats.pearsonr 不一致?

转载 作者:行者123 更新时间:2023-11-28 16:37:47 26 4
gpt4 key购买 nike

我预计 scipy.stats.mstats.pearsonr 对于屏蔽数组输入的结果将与 scipy.stats.pearsonr 对于输入数据的 unmasked 值给出相同的结果,但它不会't:

from pylab import randn,rand
from numpy import ma
import scipy.stats

# Normally distributed data with noise
x=ma.masked_array(randn(10000),mask=False)
y=x+randn(10000)*0.6

# Randomly mask one tenth of each of x and y
x[rand(10000)<0.1]=ma.masked
y[rand(10000)<0.1]=ma.masked

# Identify indices for which both data are unmasked
bothok=((~x.mask)*(~y.mask))

# print results of both functions, passing only the data where
# both x and y are good to scipy.stats
print "scipy.stats.mstats.pearsonr:", scipy.stats.mstats.pearsonr(x,y)[0]
print "scipy.stats.pearsonr:", scipy.stats.pearsonr(x[bothok].data,y[bothok].data)[0]

每次执行此操作时,答案都会有所不同,但对我来说,值相差大约 0.1,掩码分数越大,分歧越大。

我注意到如果对 x 和 y 使用相同的掩码,则两个函数的结果是相同的,即:

mask=rand(10000)<0.1
x[mask]=ma.masked
y[mask]=ma.masked
...

这是一个错误,还是我需要预处理输入数据以确保 x 和 y 中的掩码相同(当然不是)?

我使用的是 numpy 版本“1.8.0”和 scipy 版本“0.11.0b1”

最佳答案

这看起来像是 scipy.stats.mstats.pearsonr 中的错误。看起来 xy 中的值应该按索引配对,因此如果一个被屏蔽,另一个应该被忽略。也就是说,如果 xy 看起来像(使用 -- 作为掩码值):

x = [1, --,  3,  4,  5]
y = [9, 8, --, 6, 5]

然后 (--, 8)(3, --) 都被忽略,结果应该和 scipy 一样.stats.pearsonr([1, 4, 5], [9, 6, 5])

the mstats version 中的错误是compute the means of x and y的代码不使用通用掩码。

我在 scipy github 站点上为此创建了一个问题:https://github.com/scipy/scipy/issues/3645

关于python - 为什么 scipy.stats.mstats.pearsonr 结果与 scipy.stats.pearsonr 不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23601150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com