【问题标题】:Compute the dot product of all combinations of two rows in a matrix计算矩阵中两行的所有组合的点积
【发布时间】:2019-11-19 07:13:17
【问题描述】:

我是编程的超级新手,我正在尝试计算 N*3 矩阵中任何两行的所有组合的点积。

例如对于 N = 5 我有矩阵

   [0.64363829, 0.21027068, 0.7358777 ],
   [0.39138384, 0.49072791, 0.7784631 ],
   [0.22952251, 0.90537974, 0.35722115],
   [0.40108871, 0.88992243, 0.21717715],
   [0.06710475, 0.84022499, 0.53806962]

我想计算所有行组合的点积,例如:row1*row2、row1*row3、row1*row4、row1*row5、row2*row3 ... row4*row5。

我不确定如何解决这个问题,所以我尝试了一些方法。到目前为止我有

for i in range(N-1): 
    for l in range(1, N): 
        dotms = (np.dot(nmag[(i),:], nmag[(i+l),:]))
        print(dotms)

nmag 是 5*3 矩阵

输出只有 7 个答案,但有 5 行我正在寻找 10 种不同的组合

[0.9279489, 0.6009753, 0.6050964, 0.615819, 0.8122099, 0.7627538, 0.8574529]

提前感谢您的帮助!

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    我不知道我是否误解了你的意思,但是nmag.dot(nmag.T) 会得到你想要的吗?

    In [5]: nmag.dot(nmag.T)
    Out[5]:
    array([[1.        , 0.92794895, 0.60097537, 0.60509647, 0.6158193 ],
           [0.92794895, 0.99999999, 0.81220999, 0.76275381, 0.85745291],
           [0.60097537, 0.81220999, 1.00000001, 0.9753569 , 0.96833458],
           [0.60509647, 0.76275381, 0.9753569 , 1.        , 0.89150645],
           [0.6158193 , 0.85745291, 0.96833458, 0.89150645, 1.        ]])
    

    如果你只是想得到不同行的点积。

    In [17]: res = nmag.dot(nmag.T)
    
    In [18]: [res[i, j] for i in range(res.shape[0]) for j in range(res.shape[1]) if i<j]
    Out[18]:
    [0.9279489524047824,
     0.6009753676942861,
     0.6050964675806133,
     0.6158193009466447,
     0.8122099927113468,
     0.7627538110746328,
     0.8574529124107328,
     0.9753568970221529,
     0.9683345820770881,
     0.8915064490330812]
    

    【讨论】:

      【解决方案2】:

      您的循环索引不太适合您的任务:

      import numpy as np
      nmag = np.array([[0.64363829, 0.21027068, 0.7358777 ],
                       [0.39138384, 0.49072791, 0.7784631 ],
                       [0.22952251, 0.90537974, 0.35722115], 
                       [0.40108871, 0.88992243, 0.21717715],
                       [0.06710475, 0.84022499, 0.53806962]])
      
      for i in range(N-1):  # iterate over all rows but the last
          for j in range(i+1, N):   # start j from i+1 
              dotms = np.dot(nmag[i, :], nmag[j, :])
              print(dotms)
      

      【讨论】:

        【解决方案3】:

        要在伍兹陈出色的answer 上反弹,还可以使用np.triu()np.tril() 以及它们各自的函数来为给定形状的三角矩阵建立索引:np.triu_indices()np.tril_indices()。如果我们只想提取非冗余部分,这很有用(因为这样的点积矩阵根据定义是对称的)。

        这里是一个使用点积矩阵的上三角部分的例子:

        import numpy as np
        
        mat = np.array([[0.64363829, 0.21027068, 0.7358777 ],
                        [0.39138384, 0.49072791, 0.7784631 ],
                        [0.22952251, 0.90537974, 0.35722115], 
                        [0.40108871, 0.88992243, 0.21717715],
                        [0.06710475, 0.84022499, 0.53806962]])
        
        dp = mat.dot(mat.T) # dp := dot_product matrix
        
        dp_unique_vector_indices = np.triu_indices(mat.shape[0]) # assume square matrix
        dp_unique_vector_indices_without_diagonal = np.triu_indices(mat.shape[0], 1) # skip the diagonal of ones here
        
        dp_unique_vector = np.triu(dp)[dp_unique_vector_indices]
        dp_unique_vector_without_diagonal = np.triu(dp)[dp_unique_vector_indices_without_diagonal]
        

        dp_unique_vector如下:

        array([1.       , 0.927949 , 0.6009754, 0.6050965, 0.6158193, 1.       ,
               0.81221  , 0.7627538, 0.8574529, 1.       , 0.9753569, 0.9683346,
               1.       , 0.8915064, 1.       ])
        

        dp_unique_vector_without_diagonal 是:

        array([0.927949 , 0.6009754, 0.6050965, 0.6158193, 0.81221  , 0.7627538,
               0.8574529, 0.9753569, 0.9683346, 0.8915064])
        

        与全点积矩阵相比:

        array([[1.       , 0.927949 , 0.6009754, 0.6050965, 0.6158193],
               [0.927949 , 1.       , 0.81221  , 0.7627538, 0.8574529],
               [0.6009754, 0.81221  , 1.       , 0.9753569, 0.9683346],
               [0.6050965, 0.7627538, 0.9753569, 1.       , 0.8915064],
               [0.6158193, 0.8574529, 0.9683346, 0.8915064, 1.       ]])
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-11
          • 2022-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多