Asked 1 month ago by JovianScout317
How can I forward fill rows in a boolean NumPy matrix after the first True value?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by JovianScout317
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have a boolean matrix defined as follows:
PYTHONimport numpy as np A = np.array([ [0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0] ]).astype(bool)
The goal is to modify each row so that once a column has a value of True, all subsequent columns in that row are also True. For example, the desired output is:
PYTHON[0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0]
I am looking for an efficient, NumPy-based solution, preferably a one-liner, that performs this column-wise forward fill based on the first True encountered in each row.
Comments 0
•Answers 4
•Views 84
Answer by TychoAI Agent • 1 month ago
You can achieve this by performing a cumulative "logical or" along each row. In NumPy, this can be done using the function np.logical_or.accumulate along axis 1.
Below is a complete example:
PYTHONimport numpy as np A = np.array([ [0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0] ]).astype(bool) # Perform a cumulative "or" along each row: B = np.logical_or.accumulate(A, axis=1) print(B.astype(int))
This will output:
[[0 0 0 0 1 1 1]
[0 0 0 0 0 0 1]
[1 1 1 1 1 1 1]
[0 0 0 0 0 0 0]]
Each row is modified so that after the first occurrence of a True
, all the following columns are also True
.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by EclipseVoyager489 • 2 months ago
you can do a cumsum.
PYTHONimport numpy as np A = np.array([ [0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0] ]).astype(bool) B = np.cumsum(A, axis=1).astype(bool) # makes copy np.cumsum(A, axis=1, out=A) # in-place no copy
PYTHON[[False False False False True True True] [False False False False False False True] [ True True True True True True True] [False False False False False False False]]
Edit: mozway answer is faster, use it instead.
No comments yet.
Answer by QuantumPioneer253 • 2 months ago
Here is an approach from pandas.DataFrame.cummax
(which is not as efficient as @mozway and @Ahmed AEK's solutions due to the transferring to pandas
operations, instead of staying within numpy
PYTHONimport pandas as pd pd.DataFrame(A).cummax(axis = 1).to_numpy()
which gives
PYTHONarray([[False, False, False, False, True, True, True], [False, False, False, False, False, False, True], [ True, True, True, True, True, True, True], [False, False, False, False, False, False, False]])
No comments yet.
Answer by CosmicVoyager287 • 2 months ago
You could use logical_or
combined with accumulate
:
PYTHONnp.logical_or.accumulate(A, axis=1)
Output:
PYTHONarray([[False, False, False, False, True, True, True], [False, False, False, False, False, False, True], [ True, True, True, True, True, True, True], [False, False, False, False, False, False, False]])
If you want integers, go with maximum
:
PYTHONnp.maximum.accumulate(A.astype(int), axis=1) array([[0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0]])
No comments yet.
No comments yet.