【问题标题】:Calculate all possible columnwise differences in a matrix计算矩阵中所有可能的列差异
【发布时间】:2015-03-16 15:59:23
【问题描述】:

我想计算矩阵列之间所有可能的成对差异(不重复)。什么是有效的/pythonic 方式来做到这一点?

mat = np.random.normal(size=(10, 3))
mat 

array([[ 1.57921282,  0.76743473, -0.46947439],
       [ 0.54256004, -0.46341769, -0.46572975],
       [ 0.24196227, -1.91328024, -1.72491783],
       [-0.56228753, -1.01283112,  0.31424733],
       [-0.90802408, -1.4123037 ,  1.46564877],
       [-0.2257763 ,  0.0675282 , -1.42474819],
       [-0.54438272,  0.11092259, -1.15099358],
       [ 0.37569802, -0.60063869, -0.29169375],
       [-0.60170661,  1.85227818, -0.01349722],
       [-1.05771093,  0.82254491, -1.22084365]])

在这个矩阵中有 3 个成对的差异(N 选择 k 个唯一的组合,其中顺序无关紧要)。

pair_a = mat[:, 0] - mat[:, 1]
pair_b = mat[:, 0] - mat[:, 2]
pair_c = mat[:, 1] - mat[:, 2]

是一种(丑陋的)方式。你可以很容易地想象使用嵌套的for 循环来处理更大的矩阵,但我希望有更好的方法。

我希望结果是另一个矩阵,包含scipy.misc.comb(mat.shape[1], 2) 列和mat.shape[0] 行。

【问题讨论】:

    标签: python numpy scipy


    【解决方案1】:

    可以使用以下技巧找到长度为 2 的组合:

    N = mat.shape[1]
    I, J = np.triu_indices(N, 1)
    result = mat[:,I] - mat[:,J]
    

    【讨论】:

    • 对不起,我花了很长时间才回到这个问题。解决方案效果很好。
    【解决方案2】:
    In [7]: arr = np.arange(m*n).reshape((m, n))
    
    In [8]: arr
    Out[8]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15],
           [16, 17, 18, 19]])
    
    In [9]: from itertools import combinations
    
    In [10]: def diffs(arr):
       ....:     arr = np.asarray(arr)
       ....:     n = arr.shape[1]
       ....:     for i, j in combinations(range(n), 2):
       ....:         yield arr[:, i] - arr[:, j]
       ....:         
    
    In [11]: for x in diffs(arr): print x
    [-1 -1 -1 -1 -1]
    [-2 -2 -2 -2 -2]
    [-3 -3 -3 -3 -3]
    [-1 -1 -1 -1 -1]
    [-2 -2 -2 -2 -2]
    [-1 -1 -1 -1 -1]
    

    如果您需要将它们放在数组中,则只需预先分配数组并根据需要分配行(或列)。

    【讨论】:

      【解决方案3】:

      顺便说一下,这是我想出的解决方案。远不如 moarningsun 的优雅。

      def pair_diffs(mat):
          n_pairs = int(sp.misc.comb(mat.shape[1], 2))
          pairs = np.empty([mat.shape[0], n_pairs])
      
          this_pair = 0
      
          # compute all differences:
          for i in np.arange(mat.shape[1]-1):
              for j in np.arange(i+1, mat.shape[1]):
                  pairs[:, this_pair] = mat[:, i] - mat[:, j]
                  this_pair += 1
          return pairs
      

      【讨论】:

        猜你喜欢
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-30
        • 1970-01-01
        相关资源
        最近更新 更多