我有一个形状为 ~ (3000, 5) 的二维数组
mat
。现在我想从其他列中减去每一列并忽略自减。这就是我现在所拥有的并且有效的。我只是想要一种更有效的方法来做到这一点,比如自相关之类的。是否有 numpy 或 scipy 函数可以让我用更少的步骤而不是通常的 for
循环来完成此操作?
agreement = np.full((mat.shape[1], mat.shape[1]), np.nan)
for i in range(agreement.shape[0] - 1):
for j in range(i+1, agreement.shape[1]):
A = mat[:, i]
B = mat[:, j]
diff = A - B
agreement[i][j] = diff
结果是一个二维数组
agreement
,看起来像这样:
array([[nan [......] [......] [......] [......]]
[nan nan [......] [......] [......]]
[nan nan nan [......] [......]]
[nan nan nan nan [......]]
[nan nan nan nan nan]])
每个
[......]
代表那 2 列的 diff
,因此形状为 (3000,1)
,并不是准确的表示,仅举个例子。
np.corrcoef(mat, rowvar=False)
将为您提供 5 列的真实相关矩阵