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

Python 如何使用scipy中的pearsonr函数来查找字典值的相关性和P值?,python,dictionary,scipy,correlation,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,

下面是一些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
回溯(最近一次呼叫最后一次):
文件“BlackBox.py”,第32行,在
打印更正(a、b)
pearsonr中的文件“/usr/lib/python2.6/dist packages/scipy/stats/stats.py”,第1596行
mx=x.平均值()
TypeError:无法使用灵活类型执行reduce

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

从您的注释中可以看出,您的值不是整数/浮点数:

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

根据您的注释,您的值不是整数/浮点数:

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

您只需使用
histdict[str(start)].values()
,而不用复制字典。你能给我们看看
a
b
的内容吗?a是一个包含值['17633200','214.0','214.50','212.38','212.38','212.01','213.43']的列表,b是一个包含值['17633200','214.0','214.50','212.38','214.01','213.43']的列表,你可以使用
histdict[str(start)].values()
而不是复制字典。你能给我们看一下
a
b
的内容吗?a是一个包含值['17633200','214.0','214.50','212.38','212.50','214.50','212.50','212.50','212.50','212.50','212.50','212.50','212.43']的列表,b是一个包含值['17633200','214 214.0','214.50','213.43'的列表,现在它返回错误消息:回溯(最近一次调用):文件“BlackBox.py第28行,在a=[int(x)for x in histordict[str(start)].itervalues()]ValueError:int()的文本无效,基数为10:'214.0'@BlackBoxTrader:哦,对不起,我编辑了我的答案,请将int更改为floatOK,因此当我运行此代码时,与浮点类型之前的a和b值相比,由于浮点类型的性质,浮点值往往略有不同。这会对我的计算产生什么影响?它应该不会有太大影响pearson correlation正在寻找两个列表之间的线性关联。现在它返回错误消息:回溯(最近一次调用):文件“BlackBox.py”,第28行,在a=[int(x)for x in histordict[str(start)]。itervalues()]ValueError:以10为基数的int()的文本无效:'214.0'@BlackBoxTrader:哦,对不起,我编辑了我的答案,请将int更改为floatOkay,这样当我运行此代码时,与浮点类型之前的a和b值相比,由于浮点类型的性质,浮点值往往会略有不同。这会对我的计算产生什么影响?它应该不会有太大的影响。皮尔逊相关性正在寻找两个列表之间的线性相关性。