correlation for two lists of data

谁说胖子不能爱 提交于 2019-12-11 11:42:48

问题


These two lists contain data something like this:

a = [1 2 1 3 1 2 1 1 1 2 1 1 2 1 4 1 ] 
b = [ 3480. 7080. 10440. 13200. 16800. 20400. 23880. 27480. 30840. 38040. 41520. 44880.  48480. 52080. 55680. 59280.]

How to find correlation using python by importing rpy2, I mean cor function. And the o/p has to lie between -1 and +1.


回答1:


from rpy2.robjects.vectors import FloatVector
from rpy2.robjects.packages import importr

stats = importr('stats')

a=[1, 2, 1, 3, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 4, 1 ]
b=[ 3480, 7080, 10440, 13200, 16800, 20400, 23880,
    27480, 30840, 38040, 41520, 44880, 48480, 52080, 55680, 59280]

result = stats.cor(FloatVector(a), FloatVector(b))

The documentation for rpy2 has many other examples about how to use it.



来源:https://stackoverflow.com/questions/15021403/correlation-for-two-lists-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!