Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cumulative logical or within bins

Tags:

python

numpy

Problem

I want to identify when I've encountered a true value and maintain that value for the rest of the array... for a particular bin. From a Numpy perspective it would be like a combination of numpy.logical_or.accumulate and numpy.logical_or.at.

Example

Consider the truth values in a, the bins in b and the expected output in c.
I've used 0 for False and 1 for True then converted to bool in order to align the array values.

a = np.array([0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]).astype(bool)
b = np.array([0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 2, 3, 3, 0, 1, 2, 3])
# zeros       ↕  ↕  ↕              ↕  ↕  ↕           ↕
# ones                 ↕  ↕  ↕  ↕                       ↕
# twos                                      ↕              ↕
# threes                                       ↕  ↕           ↕
c = np.array([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1]).astype(bool)
#             ╰─────╯     ↑           ↑           ↑        ↑
#  zero bin no True yet   │           │           │        two never had a True
#                one bin first True   │     three bin first True
#                           zero bin first True

What I've Tried

I can loop through each value and track whether the associated bin has seen a True value yet.

tracker = np.zeros(4, bool)
result = np.zeros(len(b), bool)

for i, (truth, bin_) in enumerate(zip(a, b)):
    tracker[bin_] |= truth
    result[i] = tracker[bin_]

result * 1

array([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1])

But I was hoping for a O(n) time Numpy solution. I have the option of using a JIT wrapper like Numba but I'd rather keep it just Numpy.

like image 202
piRSquared Avatar asked May 02 '19 15:05

piRSquared


1 Answers

O(n) solution


def cumulative_linear_seen(seen, bins):
    """
    Tracks whether or not a value has been observed as
    True in a 1D array, and marks all future values as
    True for these each individual value.

    Parameters
    ----------
    seen: ndarray
      One-hot array marking an occurence of a value
    bins: ndarray
      Array of bins to which occurences belong

    Returns
    -------
    One-hot array indicating if the corresponding bin has
    been observed at a point in time
    """

    # zero indexing won't work with logical and, need to 1-index
    one_up = bins + 1

    # Next step is finding where each unique value is seen
    occ = np.flatnonzero(a)
    v_obs = one_up[a]

    # We can fill another mapping array with these occurences.
    # then map by corresponding index
    i_obs = np.full(one_up.max() + 1, seen.shape[0] + 1)
    i_obs[v_obs] = occ

    # Finally, we create the map and compare to an array of
    # indices from the original seen array
    seen_idx = i_obs[one_up]

    return (seen_idx <= np.arange(seen_idx.shape[0])).astype(int)

array([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1])

PiR's contribution

Based on insights above

r = np.arange(len(b))
one_hot = np.eye(b.max() + 1, dtype=bool)[b]
np.logical_or.accumulate(one_hot & a[:, None], axis=0)[r, b] * 1

array([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1])

Older attempts

Just to get things started, here is a solution that, while vectorized, is not O(n). I believe an O(n) solution similar to this exists, I'll work on the complexity :-)


Attempt 1

q = b + 1

u = sparse.csr_matrix(
    (a, q, np.arange(a.shape[0] + 1)), (a.shape[0], q.max()+1)
)

m = np.maximum.accumulate(u.A) * np.arange(u.shape[1])
r = np.where(m[:, 1:] == 0, np.nan, m[:, 1:])

(r == q[:, None]).any(1).view(np.int8)

array([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1], dtype=int8)

Attempt 2

q = b + 1
m = np.logical_and(a, q)
r = np.flatnonzero(u)
t = q[m]
f = np.zeros((a.shape[0], q.max()))
f[r, t-1] = 1
v = np.maximum.accumulate(f) * np.arange(1, q.max()+1)
(v == q[:, None]).any(1).view(np.int8)

array([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1], dtype=int8)
like image 144
user3483203 Avatar answered Sep 30 '22 13:09

user3483203