简体   繁体   English

如果 B[i, j] == 1 其中 B 是邻接矩阵,则从 A 生成包含每列 A 行平均值的新矩阵

[英]Generate new matrix from A containing the average value of A rows for each column if B[i, j] == 1 where B is an adjacency matrix

How can we get a new matrix containing the average value of A row for each column if B[i,j] == 1?如果 B[i,j] == 1,我们如何得到一个包含 A 行每列平均值的新矩阵?

Suppose we have a matrix A(3,4) and a matrix B(3,3)假设我们有一个矩阵 A(3,4) 和一个矩阵 B(3,3)

A =  [1      2     3      4
      15     20    7      10 
      0      5     18     12]

And an adjacency matrix和邻接矩阵

 B = [1    0     1   
      0    0     1    
      1    1     1 ]  

Expected output matrix C which takes the average value of the connected pixels in B:预期的 output 矩阵 C 取 B 中连接像素的平均值:

for example [(1+0)/2 (2+5)/2 (3+18)/2 (4+12)/2] so we get [0.5, 3.5 10.5 8] in the first row.例如[(1+0)/2 (2+5)/2 (3+18)/2 (4+12)/2]所以我们在第一行得到[0.5, 3.5 10.5 8]

           C =[0.5     3.5   10.5   8
               0        5     18   12
               5.33    9      9.33   8.66]

To find the neighborhood of each i, I implemented the following code:为了找到每个 i 的邻域,我实现了以下代码:

for i in range(A.shape[0]):
    for j in range(A.shape[0]):
        if (B[i,j] == 1):
            print(j)

You can form the sums you need by matrix multiplying:您可以通过矩阵相乘形成所需的总和:

>>> A = np.array([[1, 2, 3, 4], [15, 20, 7, 10], [0, 5, 18, 12]])
>>> B = np.array([[1, 0, 1], [0, 0, 1], [1, 1, 1]])

>>> summed_groups = B@A
>>> summed_groups
array([[ 1,  7, 21, 16],
       [ 0,  5, 18, 12],
       [16, 27, 28, 26]])

To get the means normalize by the number of terms per group:要通过每组的术语数来标准化均值:

>>> group_sizes = B.sum(axis=1,keepdims=True)
>>> group_sizes
array([[2],
       [1],
       [3]])

>>> summed_groups / group_sizes
array([[ 0.5       ,  3.5       , 10.5       ,  8.        ],
       [ 0.        ,  5.        , 18.        , 12.        ],
       [ 5.33333333,  9.        ,  9.33333333,  8.66666667]])

Side note: you could also get the group sizes by matrix multiplication:旁注:您还可以通过矩阵乘法获得组大小:

>>> group_sizes_alt = B@np.ones((len(A),1))
>>> group_sizes_alt
array([[2.],
       [1.],
       [3.]])

It is convenient to use boolean indexing.使用 boolean 分度很方便。 For example,例如,

>>> A[[True, False, True], :]

array([[ 1,  2,  3,  4],
       [ 0,  5, 18, 12]])

this selects rows 0 and 2 of the A matrix.这将选择A矩阵的第 0 行和第 2 行。 You can loop over the columns of B and construct the C matrix:您可以遍历B的列并构造C矩阵:

A = np.array([[1, 2, 3, 4], [15, 20, 7, 10], [0, 5, 18, 12]])
B = np.array([[1, 0, 1], [0, 0, 1], [1, 1, 1]]).astype(bool)
C = np.array([A[B[:, i], :].mean(axis=0) for i in range(A.shape[0])])
print(np.around(C, 2))

Result:结果:

[[ 0.5   3.5  10.5   8.  ]
 [ 0.    5.   18.   12.  ]
 [ 5.33  9.    9.33  8.67]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 B[i, j] == 1,则从 A 创建包含每列 A 行平均值的新矩阵,其中 B 是邻接矩阵 - Creation of new matrix from A containing the average value of A rows for each column if B[i, j] == 1 where B is an adjacency matrix 从列表生成邻接矩阵,其中邻接意味着相等的元素 - Generate adjacency matrix from a list, where adjacency means equal elements 我如何提取 dataframe 的所有行,其中有一个对称的 [column A = i, column B = j] 和 [column A = j, column B = i] - how can i extract all the rows of the dataframe where there is a symetrical [column A = i, column B = j ] and [column A = j, column B = i] SQL:从表中选择行,其中列的每个元素都是矩阵? - SQL: select rows from table where each element of a column is a matrix? 生成矩阵 NxN,其中行的平均值在行中,列的平均值在列中 - Generate matrix NxN where average of row is in row and avrage of column is in column 如何从python中的字典生成图形的邻接矩阵? - How do I generate an adjacency matrix of a graph from a dictionary in python? 将矩阵 A 与矩阵 B 相加 - Appending matrix A with matrix B Select 行,其中 A 列的值以 B 列的值开始 - Select rows where value of column A starts with value of column B 确定 A 是矩阵 B 的子矩阵的索引 - Determine indexes where A is a submatrix of matrix B 如何删除 Pandas DF 中的行,其中 A 列的值位于 B 列和 C 列匹配 - How to remove rows in Pandas DF where value from Column A is in Column B and Column C Match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM