计算列表中每个项目之间的相关性 [英] Calculating correlations between every item in a list

查看:81
本文介绍了计算列表中每个项目之间的相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算列表中每个项目之间的Pearson相关性相关性。我正在尝试获取data [0]和data [1],data [0]和data [2]以及data [1]和data [2]之间的相关性。

I'm trying to calculate the Pearson correlation correlation between every item in my list. I'm trying to get the correlations between data[0] and data[1], data[0] and data[2], and data[1] and data[2].

import scipy
from scipy import stats

data = [[1, 2, 4], [9, 5, 1], [8, 3, 3]]

def pearson(x, y):
    series1 = data[x]
    series2 = data[y]
    if x != y:
        return scipy.stats.pearsonr(series1, series2)

h = [pearson(x,y) for x,y in range(0, len(data))]

这将返回错误 TypeError:'int'对象不可迭代 h 上。有人可以在这里解释错误吗?谢谢。

This returns the error TypeError: 'int' object is not iterable on h. Could someone please explain the error here? Thanks.

推荐答案

范围会在您尝试使用它时返回一个int值列表,就像返回一个元组一样。尝试使用 itertools.combinations

range will return you a list of int values while you are trying to use it like it returning you a tuple. Try itertools.combinations instead:

import scipy
from scipy import stats
from itertools import combinations

data = [[1, 2, 4], [9, 5, 1], [8, 3, 3]]

def pearson(x, y):
    series1 = data[x]
    series2 = data[y]
    if x != y:
        return scipy.stats.pearsonr(series1, series2)

h = [pearson(x,y) for x,y in combinations(len(data), 2)]

或者按照@Marius的建议:

Or as @Marius suggested:

h = [stats.pearsonr(data[x], data[y]) for x,y in combinations(len(data), 2)]

这篇关于计算列表中每个项目之间的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆