我需要计算置换的总体(数组)中的方差,即
假设我有以下排列的数组:
import numpy as np
import scipy.stats as stats
a = np.matrix([[1,2,3,4,5,6], [2,3,4,6,1,5], [6,3,1,2,5,4]])
# distance between a[0] and a[1]
distance = stats.kendalltau(a[0], a[1])[0]
那么,如何计算(在Python中)此数组的方差,即,如何测量这些排列彼此之间的距离?
问候
艾美
ps:我用kendalltau指标定义了两个置换之间的距离
我不确定这是否是你要寻找的数学结果。你可以stats.kendalltau
用来计算所有可能对的距离,然后从所得向量中获取方差。
为了获得距离的向量,我(a, a-shifted)
使用np.roll
以下命令遍历压缩列表:
dist = []
for x1, x2 in zip(a, np.roll(a, shift=1, axis=0)):
dist.append(kendalltau(x1, x2)[0])
取所有距离的方差:
np.std(dist)
或者,如果你正在寻找方差(如此处讨论),则采用距离向量的范数:
np.linalg.norm(dist)
请注意,我使用的a
是np.array
,而不是np.matrix
:
a = np.array([[1,2,3,4,5,6], [2,3,4,6,1,5], [6,3,1,2,5,4]])