a = np.array([2,3,1,4])
b = np.array([2,3,7,1])
c = np.zeros((4, 10))
I wanna assign value 1
to some elements in c
. a
and b
define the positions of such elements. a
is the starting column indices of value 1
in each row. And b
represents how many consecutive 1
there are in the row. The output I am expecting is:
array([[ 0., 0., 1., 1., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 1., 1., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
I can use a simple for loop as below:
for i in range(c.shape[0]):
for k in range(a[i], a[i]+b[i]):
c[i,k]=1
But it would be slow for large arrays, is there any faster numpy indexing to do this? Thanks.
You can cast it into a 1D problem
def convert_inds(a,b,array_shape):
nrows,ncols = array_shape
to_take = np.zeros(sum(b))
count = 0
for ind,item in enumerate(b):
start_ind = ind*ncols+a[ind]
to_take[count:count+item] = np.arange(start_ind,start_ind+item)
count += item
return to_take.astype(np.int)
to_take = convert_inds(a,b,c.shape)
c.ravel()[to_take] = 1
In the code above, convert_inds
will convert a
and b
to
array([ 2, 3, 13, 14, 15, 21, 22, 23, 24, 25, 26, 27, 34])
which are indices of 1
s in the flattened c
. By doing this, you only need to iterate through b
in the function convert_inds
.
I implemented next solution without any Python loops, just pure NumPy code. Maybe it is not that simple as python-loop solution, but definitely will be much faster especially for large data.
import numpy as np
def set_val_2d(a, val, starts, lens):
begs = starts + np.arange(a.shape[0]) * a.shape[1]
ends = begs + lens
clens = lens.cumsum()
ix = np.ones((clens[-1],), dtype = np.int64)
ix[0] = begs[0]
ix[clens[:-1]] = begs[1:] - ends[:-1] + 1
ix = ix.cumsum()
a.ravel()[ix] = val
a = np.array([2,3,1,4])
b = np.array([2,3,7,1])
c = np.zeros((4, 10))
set_val_2d(c, 1, a, b)
print(c)
Output:
[[0. 0. 1. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]
If you choose a fancy indexing based approach, the most difficult part is finding indexes of axis 1. This is very similar to:
>>> np.repeat(a, b)
array([2, 2, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 4])
except that each groups of indexes should be incrementing. This fix can be done with this function:
def accumulative_count(counts, initial):
counter = np.ones(np.sum(counts), dtype=int)
marker_idx = np.r_[0, np.cumsum(counts)[:-1]]
subtract_vals = np.r_[1, counts[:-1]]
initial_vals = np.r_[initial[0], np.diff(initial)]
counter[marker_idx] = counter[marker_idx] - subtract_vals + initial_vals
return np.cumsum(counter)
>>> accumulative_count(counts, initial)
array([2, 3, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, 4], dtype=int32)
After all, you're capable to finish it:
c[np.repeat(np.arange(len(c)), b), accumulative_count(b, a)] = 1
c:
array([[0., 0., 1., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 1., 0., 0., 0., 0.],
[0., 1., 1., 1., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.