Warm tip: This article is reproduced from serverfault.com, please click

Set values in row to zero before index value of row [NumPy or Tensorflow]

发布于 2020-11-28 00:17:33

I have an array A with shape (N,). I am taking N=5 for illustration:

A = np.array([0,1,1,0,1])

And I want to transform it to the following NxN matrix B. Solutions in both NumPy and Tensorflow are good but the latter is preferred.

B = np.array([[0,1,1,0,1],
              [0,1,1,0,1],
              [0,1,1,0,1],
              [0,0,0,0,1],
              [0,0,0,0,1]])

One solution can be comprised of following steps:

  1. Repeat array A N times
  2. Loop through each row i. Look for the index of the last zero until the i-th element of that row.
  3. Replace all elements preceding that index with zeros.

Another illustration with N=10:

D = np.array([0,1,1,1,0,0,1,1,0,0])
E = np.array([[0,1,1,1,0,0,1,1,0,0],
              [0,1,1,1,0,0,1,1,0,0],
              [0,1,1,1,0,0,1,1,0,0],
              [0,1,1,1,0,0,1,1,0,0],
              [0,0,0,0,0,0,1,1,0,0],
              [0,0,0,0,0,0,1,1,0,0],
              [0,0,0,0,0,0,1,1,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,0,0,0,0,0,0]])
Questioner
rafiko1
Viewed
0
fdermishin 2020-11-28 18:16:29
A = np.array([0,1,1,0,1])
N = A.shape[0]
column = (A > 0).reshape((N, 1))
mask = np.ones((N, N), dtype=np.bool)
mask = np.where(column, False, np.tril(mask, -1))
mask = np.cumsum(mask, axis=0)
B = np.where(mask, 0, np.tile(A, (N, 1)))
[[0 1 1 0 1]
 [0 1 1 0 1]
 [0 1 1 0 1]
 [0 0 0 0 1]
 [0 0 0 0 1]]

Explanation

  1. Compute lower triangular matrix
[[False False False False False]
 [ True False False False False]
 [ True  True False False False]
 [ True  True  True False False]
 [ True  True  True  True False]]
  1. Find ones in A and fill corresponding rows with False
[[False False False False False]
 [False False False False False]
 [False False False False False]
 [ True  True  True False False]
 [False False False False False]]
  1. Compute cumulative sum to set zeros to all rows below. This is a mask of all elements that should be zeroed out
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [1 1 1 0 0]
 [1 1 1 0 0]]
  1. Repeat array A N times
[[0 1 1 0 1]
 [0 1 1 0 1]
 [0 1 1 0 1]
 [0 1 1 0 1]
 [0 1 1 0 1]]
  1. Mask its elements
[[0 1 1 0 1]
 [0 1 1 0 1]
 [0 1 1 0 1]
 [0 0 0 0 1]
 [0 0 0 0 1]]