Python Scipy:scipy.stats.spearmanr返回nans [英] Python Scipy: scipy.stats.spearmanr returning nans
问题描述
编辑:我认为基本可以解决。
Basically solved I think.
我正在使用scipy.stats中的spearmanr来查找多个不同样本中变量之间的相关性。我大约有2500个变量和36个样本(或观察值)
I am using spearmanr from scipy.stats to find the correlations between variables across a number of different samples. I have around 2500 variables and 36 samples (or 'observations')
如果我使用全部36个样本计算相关性,则spearmanr可以正常工作。如果仅使用前18个样本,它也可以正常工作。但是,如果使用后18个样本,则会出错并返回nans。
If I calculate the correlations using all 36 samples, spearmanr works fine. If I use only the first 18 samples it also works fine. However if I use the latter 18 samples I get an error and nans are returned.
这是错误:
/Home/s1215235/.local/lib/python2.7/site-packages/numpy/lib/function_base.py:1945: RuntimeWarning: invalid value encountered in true_divide
return c / sqrt(multiply.outer(d, d))
/Home/s1215235/.local/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1718: RuntimeWarning: invalid value encountered in greater
cond1 = (scale > 0) & (x > self.a) & (x < self.b)
/Home/s1215235/.local/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1718: RuntimeWarning: invalid value encountered in less
cond1 = (scale > 0) & (x > self.a) & (x < self.b)
/Home/s1215235/.local/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1719: RuntimeWarning: invalid value encountered in less_equal
cond2 = cond0 & (x <= self.a)
这是代码:
populationdata = np.vstack(thing).astype(np.float)
rho, pval = stats.spearmanr(populationdata[:,sampleindexes], axis = 1)
(populationdata是一个充满浮点的numpy数组; [:, sampleindexes]允许
(populationdata is a numpy array full of floats; [:,sampleindexes] allows only a few of the columns to be used.
这就是rho返回的形式:
And this is what rho is returned as:
[[ 1. nan nan ..., 1. -0.05882353
-0.08574929]
[ nan nan nan ..., nan nan
nan]
[ nan nan nan ..., nan nan
nan]
...,
[ 1. nan nan ..., 1. -0.05882353
-0.08574929]
[-0.05882353 nan nan ..., -0.05882353 1. 0.68599434]
[-0.08574929 nan nan ..., -0.08574929 0.68599434 1. ]]
推荐答案
在评论中指出,因此很多$ 0。 因此, populationdata [:,sampleindexes]
可能具有全为0的行。这将导致 spearmanr
生成 nan
。例如,
In a comment it was noted that "There are a lot of 0s though." So populationdata[:,sampleindexes]
probably has rows that are all 0. That will cause spearmanr
to generate nan
. For example,
In [3]: spearmanr([[0, 0, 0], [1, 2, 3]], axis=1)
/Users/warren/anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py:1957: RuntimeWarning: invalid value encountered in true_divide
return c / sqrt(multiply.outer(d, d))
/Users/warren/anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1728: RuntimeWarning: invalid value encountered in greater
cond1 = (scale > 0) & (x > self.a) & (x < self.b)
/Users/warren/anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1728: RuntimeWarning: invalid value encountered in less
cond1 = (scale > 0) & (x > self.a) & (x < self.b)
/Users/warren/anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1729: RuntimeWarning: invalid value encountered in less_equal
cond2 = cond0 & (x <= self.a)
Out[3]: (nan, nan)
这篇关于Python Scipy:scipy.stats.spearmanr返回nans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!