如何在scipy中使用pearsonr函数来查找字典值的相关性和P值?

时间:2011-06-21 03:10:04

标签: python dictionary scipy correlation

下面有一些Python代码:

a = dict.values(histodict[str(start)])
b = dict.values(histodict[str(end)])
print pearsonr(a,b)

当指示脚本执行此操作时,变量a和b都将作为列表正确打印,但它们不会在scipy中的pearsonr函数中响应。

我想知道为什么这不起作用。返回的错误是:

Traceback (most recent call last):
File "BlackBox.py", line 32, in <module>
print corr(a,b)
File "/usr/lib/python2.6/dist-packages/scipy/stats/stats.py", line 1596, in pearsonr
mx = x.mean()
TypeError: cannot perform reduce with flexible type

由于当前形式的代码显然不起作用,如何在scipy中使用pearsonr函数来返回字典值的相关性和P值?

1 个答案:

答案 0 :(得分:3)

根据您的评论,您的值不是整数/浮点数:

a = [float(x) for x in histodict[str(start)].itervalues()]
b = [float(x) for x in histodict[str(end)].itervalues()]
print pearsonr(a,b)