numpy 如何找到列约束和行约束之间的交集?

kzmpq1sx  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(123)

我是一个在Python中使用矩阵的新手。我想问你关于矩阵的事。
输入:

import numpy as np

wow = np.array([[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0*,0],
                [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]])

从上面的矩阵,我想插入1在column,有all zerosrowsum row等于1,如第11列和第2行之间的交叉点。但是,我们需要通过pop矩阵检查一些批准条件,然后将该交集从0更改为1。如果pop矩阵中的交集(*)的相同位置等于1,则我们认可wow矩阵中的交集。如果相同位置的交集(*)等于0,则在wow矩阵中该交集没有变化。
审批条件矩阵:

pop = np.array([[1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0],
                [0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1*,0],
                [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]])

输出:将交点从0更改为1

>>> change_element_in(wow)
>>> wow
[[1 0 0 1 0 0 0 0 0 0 0 0]
 [0 1 1 0 0 0 0 0 0 0 1* 0]
 [0 0 0 0 0 0 0 1 0 0 0 1]]

你有线索吗?谢谢

qlfbtfca

qlfbtfca1#

如果我理解正确的话,你想一次只改变一个数字,它可以是任何符合条件的数字。因此,我建议使用以下算法:
首先,获取必要的输入:

import numpy as np
# Import NDArray to get the type hints in our functions 
# and to make autocomplete work in IDEs
from numpy.typing import NDArray

然后创建一个函数来查找合适的行:

def find_rows(matrix: NDArray):
    rows = []
    # matrix.sum return the list of sums of the elements along each axis
    row_sums = matrix.sum(axis=1)
    # Go through all row sums to find suitable rows
    for row_index, row_sum in enumerate(row_sums):
        if row_sum == 1:
            rows.append(row_index)
    return rows

同样,也要找到合适的列:

def find_columns(matrix: NDArray):
    columns = []
    # Transpose the matrix to inspect through columns
    transposed = matrix.transpose()
    # Check if there is non-zero element for each column
    zero_checks = transposed.any(axis=1)
    # Go through all row sums to find suitable columns
    for column_index, column_check in enumerate(zero_checks):
        if not column_check:
            columns.append(column_index)
    return columns

现在,我们可以实现将一插入矩阵的函数:

def insert_one(matrix: NDArray, approval_matrix: NDArray):
    # Get the suitable rows and columns for matrix
    possible_rows = find_rows(matrix)
    possible_columns = find_columns(matrix)
    # List comprehension to create a list of all possible cells
    possible_cells = [(row, column) for row in possible_rows
                                    for column in possible_columns]
    for cell in possible_cells:
        # Check if the corresponding element is 1 in the approval matrix
        if approval_matrix[cell] == 1:
            matrix[cell] = 1
            # If some element in the matrix is changed, return 0
            return 0
    # If nothing is changed in the matrix, return 1
    return 1

对于您提供的矩阵,我得到了以下结果:

wow = np.array([
    [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
])
pop = np.array([
    [1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0],
    [0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0],
    [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]
])
insert_one(matrix=wow, approval_matrix=pop)
print(wow)

输出(我用*标记修改的元素):

[[1 0 0  1 0 0 0 0 0 0 0 0]
 [0 1 1* 0 0 0 0 0 0 0 0 0]
 [0 0 0  0 0 0 0 1 0 0 0 1]]

如果你需要以这种方式多次更改矩阵,只需在while循环中执行insert_one函数,并跟踪其返回值。当它返回1时,没有什么要改变的。

相关问题