How do I use the pearsonr function in scipy to find the correlation and P-Value of dictionary values?
I have some Python code below:
a = dict.values(histodict[str(start)])
b = dict.values(histodict[str(end)])
print pearsonr(a,b)
Both variables a and b will print properly as a list when the script is instructed to do so but they will not respond in the pearsonr function in scipy.
I am wondering why this doesn't work. The error returned is:
Traceback (most recent call last):
File "BlackBox.py", line 32, in <module>
print corr(a,b)
开发者_如何学PythonFile "/usr/lib/python2.6/dist-packages/scipy/stats/stats.py", line 1596, in pearsonr
mx = x.mean()
TypeError: cannot perform reduce with flexible type
And since this code in its current form will obviously not work, how do I use the pearsonr function in scipy to return the correlation and P-value of dictionary values?
From your comment your values are not integers / floats:
a = [float(x) for x in histodict[str(start)].itervalues()]
b = [float(x) for x in histodict[str(end)].itervalues()]
print pearsonr(a,b)
精彩评论